-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFG Greedy-11 GeekBalls.cpp
77 lines (56 loc) · 1.18 KB
/
GFG Greedy-11 GeekBalls.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <map>
#include <bits/stdc++.h>
using namespace std;
int maxballs(int a[], int b[], int n,int m)
{
int i=0,j=0,s1=0,s2=0,ans=0;
while(i<n && j<m)
{
if (a[i]>b[j])
s2+=b[j++];
else if (a[i]<b[j])
s1+=a[i++];
else
{
s1+=a[i];
s2+=b[j];
if (s1>s2)
{
ans+=s1;
while (i+1<n && a[i]==a[i+1])
ans+=a[i++];
}
if (s1<s2)
{ ans+=s2;
while (j<=m && b[j]==b[j+1])
ans+=b[j++];
}
i++; j++;
s1=s2=0;
}
}
while(i<n)
s1+=a[i++];
while(j<m)
s2+=b[j++];
ans+=max(s1,s2);
cout << "Answer is :" << ans << endl;
return ans;
}
int main()
{
int n,m;
cin >> n >> m;
int a[n], b[m];
int i,ans;
for (i=0;i<n;i++)
cin >> a[i];
for (i=0;i<m;i++)
cin >> b[i];
ans = maxballs(a,b,n,m);
cout << "Answer is :" << ans << endl;
return 0;
}