forked from t3nsor/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
anarc05b.cpp
40 lines (40 loc) · 1.01 KB
/
anarc05b.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
// 2014-04-25
#include <iostream>
using namespace std;
int a[10000], b[10000];
int main() {
ios::sync_with_stdio(false);
for (;;) {
int m, n;
cin >> m;
if (m==0) return 0;
for (int i = 0; i < m; i++) {
cin >> a[i];
}
cin >> n;
for (int i = 0; i < n; i++) {
cin >> b[i];
}
int sum1 = 0, sum2 = 0;
int i = 0, j = 0;
int res = 0;
while (i < m || j < n) {
if (i == m) {
while (j < n) { sum2 += b[j++]; }
} else if (j == n) {
while (i < m) { sum1 += a[i++]; }
} else if (a[i] < b[j]) {
sum1 += a[i++];
} else if (a[i] > b[j]) {
sum2 += b[j++];
} else {
// intersection point!
res += max(sum1, sum2);
sum1 = sum2 = a[i];
i++; j++;
}
}
res += max(sum1, sum2);
printf("%d\n", res);
}
}