File tree 1 file changed +69
-0
lines changed
1 file changed +69
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < vector>
3
+ #include < algorithm>
4
+ using namespace std ;
5
+
6
+ int largestsum (vector <int >,vector <int >);
7
+ int main ()
8
+ {
9
+ int a,b;
10
+ cin>>a;
11
+ while (a!=0 )
12
+ {
13
+ vector <int > a1 (a);
14
+ for (int i=0 ;i<a;i++)
15
+ cin>>a1[i];
16
+ cin>>b;
17
+ vector <int > b1 (b);
18
+ for (int i=0 ;i<b;i++)
19
+ cin>>b1[i];
20
+ cout<<largestsum (a1,b1)<<endl;
21
+ cin>>a;
22
+ }
23
+ return 0 ;
24
+ }
25
+
26
+ int largestsum (vector <int > a,vector <int > b)
27
+ {
28
+ int res=0 ;
29
+ int s1,s2,k=0 ;
30
+ int ele=0 ;
31
+ s1=s2=0 ;
32
+ for (int i=0 ;i<a.size ();i++)
33
+ {
34
+ s2=0 ;
35
+ s1+=a.at (i);
36
+ for (int j=k;j<b.size ();j++)
37
+ {
38
+ s2+=b.at (j);
39
+ if (a.at (i)==b.at (j))
40
+ {
41
+ ele=a[i];
42
+ k=j+1 ;
43
+ // cout<<k<<" "<<s1<<" "<<s2<<endl;
44
+ /* if(s1>s2)
45
+ res+=s1;
46
+ else
47
+ res+=s2;*/
48
+ res=res+max (s1,s2);
49
+ s1=0 ;
50
+ break ;
51
+ }
52
+ }
53
+ }
54
+ if (a[a.size ()-1 ]!=b[b.size ()-1 ])
55
+ {
56
+ int i=find (b.begin (),b.end (),ele)-b.begin ();
57
+ if (i<b.size ()-1 )
58
+ {
59
+ s2=0 ;
60
+ i++;
61
+ for (;i<b.size ();i++)
62
+ s2+=b[i];
63
+ }
64
+ // cout<<s1<<" V"<<s2<<endl;
65
+ res=res+max (s1,s2);
66
+ }
67
+ return res;
68
+ }
69
+
You can’t perform that action at this time.
0 commit comments