/* compile sheep.cpp && ./sheep < sheepset/1.in */ using namespace std; #include #include int calculate (int nodes, int sheeps) { int A, B, C, D, E; int sheeps2 = 0; cin >> A >> B >> C >> D >> E; if ( B >= A ) { sheeps -= A; sheeps2 += (sheeps >= 0) ? A : (A + sheeps); if(sheeps < 0) sheeps = 0; B -= A; A = 0; } else { sheeps -= B; sheeps2 += (sheeps >= 0) ? B : (B + sheeps); if(sheeps < 0) sheeps = 0; A -= B; B = 0; } if ( E >= D ) { sheeps -= D; sheeps2 += (sheeps >= 0) ? D : (D + sheeps); if(sheeps < 0) sheeps = 0; E -= D; D = 0; } else { sheeps -= E; sheeps2 += (sheeps >= 0) ? E : (E + sheeps); if(sheeps < 0) sheeps = 0; D -= E; E = 0; } int m1 = min(A, min(C,E)); int m2 = min(D, min(C,B)); int maximum = max(m1,m2); sheeps -= maximum; sheeps2 += (sheeps >= 0) ? maximum : (maximum + sheeps); if (nodes <= 1) return sheeps2; return calculate(nodes-1, sheeps2); } int main() { int nodes; cin >> nodes; cout << calculate(nodes, 2 * pow(10,8) + 1); return 0; }