-
Notifications
You must be signed in to change notification settings - Fork 6
/
Chocolate.cpp
52 lines (47 loc) · 903 Bytes
/
Chocolate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
long long T;
cin >> T;
while(T--) {
long long N, M;
cin >> N >> M;
N--, M--;
vector<long long> x(N), y(M);
for(long long i = 0; i < N; i++)
cin >> x[i];
for(long long i = 0; i < M; i++)
cin >> y[i];
sort(x.rbegin(), x.rend());
sort(y.rbegin(), y.rend());
long long nH = 1, nV = 1, i = 0, j = 0, ans = 0;
while(i < N && j < M) {
if(x[i] > y[j]) {
ans += x[i]*nV;
nH++;
i++;
}
else {
ans += y[j]*nH;
nV++;
j++;
}
}
if(i < N) {
long long sum = 0;
while(i < N)
sum += x[i++];
ans += sum*nV;
}
else {
long long sum = 0;
while(j < M)
sum += y[j++];
ans += sum*nH;
}
cout << ans << endl;
}
return 0;
}