#include using namespace std; using vi = vector; #define A segment[0] #define B segment[1] #define C segment[2] #define D segment[3] #define E segment[4] size_t step(vector & segment, size_t initial){ size_t up = A; size_t down = D; size_t result = 0; result += min(A, B) + min(D, E); up = A - min(A, B); down = D - min(D, E); B -= min(A, B); E -= min(D, E); result += max( min(up, min(C, E)), min(down, min(C, B)) ); return min(initial, result); } void solve(){ size_t N; cin >> N; size_t result = UINT64_MAX; vector> segments(N, {0,0,0,0,0}); for(size_t i = 0; i < N; i++){ for(int j = 0; j < 5; j++){ cin >> segments[i][j]; } } for(size_t i = 0; i < N; i++){ result = step(segments[i], result); } cout << result << endl; } int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int t = 1; //cin >> t; while (t--){ solve(); } return 0; }