-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathANARC05B.cpp
34 lines (33 loc) · 921 Bytes
/
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
#include <cstdio>
#define gc getchar_unlocked
#define len 10017
int arr1[len];
int arr2[len];
inline int fast(){int n=0,flag=0;char ch=gc();while(ch<48 && ch!='-')ch=gc(); if(ch=='-'){
flag=1;ch=gc();}
while(ch>47){n=(n<<1)+(n<<3)+ch-48;ch=gc();} if(flag)n=n*-1;return n;}
int main(){
int N,M,i,j,k;
long long sum1,sum2,max;
while(true){
N=fast();
if(N==0) break;
for(i=0;i<N;i++)arr1[i]=fast();
M=fast();
for(i=0;i<M;i++) arr2[i]=fast();
k=M>N?N:M;
sum1=0;sum2=0;
for(i=0,j=0;i<k && j<M;){
if(arr1[i]>arr2[j])sum2+=arr2[j++];
else if(arr1[i] < arr2[j]) sum1+=arr1[i++];
else{
max=sum1>sum2?sum1:sum2;
sum1=arr1[i++]+max;
sum2=arr2[j++]+max;
}}
for(;i<N;i++)sum1+=arr1[i];
for(;j<M;j++)sum2+=arr2[j];
printf("%lld\n",sum1>sum2?sum1:sum2);
}
return 0;
}