Greedy - though simple, but fun!
#include <vector> #include <iostream> using namespace std; int main(){ int n1; int n2; int n3; cin >> n1 >> n2 >> n3; long long l1 = 0, l2 = 0, l3 = 0; vector<int> h1(n1); for(int h1_i = 0;h1_i < n1;h1_i++){ cin >> h1[h1_i]; l1 += h1[h1_i]; } vector<int> h2(n2); for(int h2_i = 0;h2_i < n2;h2_i++){ cin >> h2[h2_i]; l2 += h2[h2_i]; } vector<int> h3(n3); for(int h3_i = 0;h3_i < n3;h3_i++){ cin >> h3[h3_i]; l3 += h3[h3_i]; } int i1 = 0, i2 = 0, i3 = 0; while(!(l1 == l2 && l2 == l3)) { // which stack is highest now? int maxi = 1; int maxl = l1; if(maxl < l2) { maxi = 2; maxl = l2; } if(maxl < l3) { maxi = 3; maxl = l3; } // switch(maxi) { case 1: l1 -= h1[i1++]; break; case 2: l2 -= h2[i2++]; break; case 3: l3 -= h3[i3++]; break; } if(!l1 || !l2 || !l3) { l1 = l2 = l3 = 0; } } cout << l1 << endl; return 0; }