ANARC05B - The Double HeLiX
#include <iostream>
#include <cstring>
using namespace std;
int a[10001];
int b[10001];
int main()
{
int n, m;
while(true)
{
cin >> n;
if(n == 0)
break;
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
cin >> m;
for(int i = 0; i < m; i++)
{
cin >> b[i];
}
int i, j;
i = j = 0;
long int sum1, sum2, maxSum;
sum1 = sum2 = maxSum = 0;
while(i < n && j < m)
{
if(a[i] < b[j])
{
sum1 += a[i];
i++;
}
else if(b[j] < a[i])
{
sum2 += b[j];
j++;
}
else
{
maxSum += max(sum1, sum2) + a[i];
sum1 = sum2 = 0;
i++, j++;
}
}
if(i < n)
{
while(i < n)
{
sum1 += a[i];
i++;
}
}
if(j < m)
{
while(j < m)
{
sum2 += b[j];
j++;
}
}
maxSum += max(sum1, sum2);
cout << maxSum << endl;
}
return 0;
}
Comments
Post a Comment