#include #include int main() { int segnum; std::cin >> segnum; std::vector A; std::vector B; std::vector C; std::vector D; std::vector E; for(int i = 0; i < segnum; i++) { int curr; std::cin >> curr; A.push_back(curr); std::cin >> curr; B.push_back(curr); std::cin >> curr; C.push_back(curr); std::cin >> curr; D.push_back(curr); std::cin >> curr; E.push_back(curr); } int currUp, currDown, currMid; currMid = A[0] + D[0]; for(int i = 0; i < segnum; ++i) { currUp = std::min(A[i], std::min(B[i], currMid)); currMid -= currUp; currDown = std::min(D[i], std::min(E[i], currMid)); currMid -= currDown; // '=' aby se to nekam poslalo if( std::min( A[i]-currUp, std::min(C[i], E[i]-currDown )) >= std::min( D[i]-currDown, std::min(C[i], B[i]-currUp )) ) { currUp += std::min(currMid, std::min( A[i]-currUp, std::min(C[i], E[i]-currDown ))); } else { currDown += std::min(currMid, std::min( D[i]-currDown, std::min(C[i], B[i]-currUp ))); } currMid = currUp + currDown; } std::cout << currMid << std::flush; return 0; }