-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathsolution.cpp
65 lines (63 loc) · 1.41 KB
/
solution.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
#include<bits/stdc++.h> //This header file imports everything we need
using namespace std ;
vector<int> temp ;
void get_stack(stack<int>& h) // will give us the stack in a required way
{
reverse(temp.begin(), temp.end()) ;
for(auto ele : temp)
{
h.push(ele) ;
}
}
int main()
{
int n1, n2, n3 ;
cin >> n1 >> n2 >> n3 ;
stack<int> h1, h2, h3 ; // Our Stack of Cylinders
long long sum1 = 0 , sum2 = 0, sum3 = 0 ; // will hold the sum of the respective cyclinder
for(int i=0 ; i<n1 ; i++) // Taking the first cylinder as input
{
int x ;
cin >> x ;
sum1 += x ;
temp.push_back(x) ;
}
get_stack(h1) ;
temp.clear();
for(int i=0 ; i<n2 ; i++) // Taking the second cyclinder as input
{
int x ;
cin >> x ;
sum2 += x ;
temp.push_back(x) ;
}
get_stack(h2) ;
temp.clear() ;
for(int i=0 ; i<n3 ; i++) // Taking the third cyclinder as input
{
int x ;
cin >> x ;
sum3 += x ;
temp.push_back(x) ;
}
get_stack(h3) ;
while((sum1 != sum2) || (sum2 != sum3)) // we will loop unless all of three sum becomes equal
{
if((sum1 >= sum2) && (sum1 >= sum3))
{
sum1 -= h1.top() ;
h1.pop() ;
}
else if((sum2 >= sum1) && (sum2 >= sum3))
{
sum2 -= h2.top() ;
h2.pop() ;
}
else if((sum3 >= sum1) && (sum3 >= sum2))
{
sum3 -= h3.top() ;
h3.pop() ;
}
}
cout << sum1 ; // printing any of the sums as all are equal now
}