#include #include #include #include int main() { int n; std::cin >> n; std::vector count(n); int now = 0; std::vector a(n), b(n); for (int i = 0; i < n; i++) std::cin >> a[i]; for (int i = 0; i < n; i++) std::cin >> b[i]; std::vector> event(3 * n); for (int i = 0; i < n; i++) { event[3 * i] = {a[i], i}; event[3 * i + 1] = {b[i], i}; event[3 * i + 2] = {(a[i] + b[i]) / 2, i}; } std::sort(event.begin(), event.end()); auto add = [&](int i) { count[event[i].second]++; if (count[event[i].second] == 1) { now++; } }; auto erase = [&](int i) { count[event[i].second]--; if (count[event[i].second] == 0) { now--; } }; auto check = [&]() { return now == n; }; int r = 0; int ans = 1e9; for (int l = 0; l < 3 * n; l++) { if (r < l) r = l; for (; r < 3 * n && !check();) { add(r++); } if (check()) { ans = std::min(ans, event[r - 1].first - event[l].first); } if (l < r) erase(l); } std::cout << ans << '\n'; }