#include #include #include using namespace std; using u64 = long long; int main() { u64 seg_count; scanf("%llu\n", &seg_count); u64 result = numeric_limits::max(); for (u64 i = 0; i < seg_count; i++) { u64 a, b, c, d, e; scanf("%llu %llu %llu %llu %llu", &a, &b, &c, &d, &e); u64 bottom = min(e, d); u64 top = min(a, b); u64 bottom_aval = min(min(a - top, e - bottom), c); u64 top_aval = min(min(d - bottom, b - top), c); u64 total = bottom + top + max(bottom_aval, top_aval); result = min(result, total); } printf("%llu\n", result); }