#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using namespace atcoder; ll value(vector &A,vector &B,ll maxC) { ll N = A.size(); ll minC = numeric_limits::max(); for(int i = 0;i < N;i++){ if (B[i] <= maxC){ minC = min(minC,B[i]); continue; } if ( (A[i] + B[i])/2 <= maxC){ minC = min(minC,(A[i] + B[i])/2); continue; } if (A[i] <= maxC){ minC = min(minC,A[i]); continue; } return numeric_limits::max(); } return maxC - minC; } ll three_search(vector &A,vector &B,ll l,ll r) { // cout << l << "~" << r << endl; if (l + 2 >= r) { vector> values; for (int i = l; i < r; i++) { values.push_back({value(A,B, i), i}); } sort(values.begin(), values.end()); return values.front().second; } else { ll t1 = (2 * l + r) / 3; ll t2 = (l + 2 * r) / 3; ll t1_value = value(A,B, t1); ll t2_value = value(A,B, t2); if (t1_value <= t2_value) { return three_search(A,B, l, t2); } else { return three_search(A,B, t1, r); } } } int solve() { ll N; cin >> N; vector A(N); vector B(N); for(int i = 0;i < N;i++){ cin >> A[i]; } for(int i = 0;i < N;i++){ cin >> B[i]; if (A[i] > B[i]){ swap(A[i],B[i]); } } ll maxC = three_search(A,B,0,1e10); cout << value(A,B,maxC) << endl; return 0; } int main() { // ll T; // cin >> T; // while (T--) // { solve(); // } // cout << flush; return 0; }