#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); set>> data; 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]); } data.insert({B[i],{A[i],B[i]}}); } ll ans = numeric_limits::max(); while(true){ ans = min(ans,data.rbegin()->first - data.begin()->first); auto p = *data.rbegin(); if (p.first == p.second.first){ break; }else if (p.first == (p.second.first + p.second.second)/2){ data.insert({p.second.first,p.second}); }else{ data.insert({(p.second.first + p.second.second)/2,p.second}); } data.erase(p); } cout << ans << endl; return 0; } int main() { // ll T; // cin >> T; // while (T--) // { solve(); // } // cout << flush; return 0; }