#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = (ll)(1e18);
struct Event {
int typ; // 1 - otwarcie, -1 - zamkniecie
int poz;
};
bool comp(Event a, Event b) {
if (a.poz != b.poz) return a.poz < b.poz;
return a.poz > b.poz;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<Event> events;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
events.push_back({1, a});
events.push_back({-1, b});
}
sort(events.begin(), events.end(), comp);
int X = 0;
int Y = n;
ll still_not_open_sum = 0LL;
ll max_time = events.back().poz;
ll already_closed_sum = 0LL;
for (int i = 0; i < events.size(); i++) {
if (events[i].typ == 1) {
still_not_open_sum += max_time - events[i].poz;
}
}
ll ans = INF;
int meeting_time = -1;
for (int i = 0; i < events.size(); i++) {
int typ = events[i].typ;
ll poz = events[i].poz;
if (typ == 1) {
Y--;
still_not_open_sum -= max_time - poz;
} else {
X++;
already_closed_sum += poz;
}
ll cost_left = X*poz - already_closed_sum;
ll cost_right = Y*(max_time - poz) - still_not_open_sum;
if (ans > cost_left + cost_right) {
ans = cost_left + cost_right;
meeting_time = poz;
}
}
cout << meeting_time << ' ' << ans;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKCmNvbnN0IGxsIElORiA9IChsbCkoMWUxOCk7CgpzdHJ1Y3QgRXZlbnQgewogICAgaW50IHR5cDsgICAvLyAxIC0gb3R3YXJjaWUsIC0xIC0gemFta25pZWNpZQogICAgaW50IHBvejsKfTsKCmJvb2wgY29tcChFdmVudCBhLCBFdmVudCBiKSB7CiAgICBpZiAoYS5wb3ogIT0gYi5wb3opIHJldHVybiBhLnBveiA8IGIucG96OwoKICAgIHJldHVybiBhLnBveiA+IGIucG96Owp9CgppbnQgbWFpbigpIHsKCWlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CgljaW4udGllKDApOwoKICAgIGludCBuOwogICAgY2luID4+IG47CgogICAgdmVjdG9yPEV2ZW50PiBldmVudHM7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGludCBhLCBiOwogICAgICAgIGNpbiA+PiBhID4+IGI7CgogICAgICAgIGV2ZW50cy5wdXNoX2JhY2soezEsIGF9KTsKICAgICAgICBldmVudHMucHVzaF9iYWNrKHstMSwgYn0pOwogICAgfQoKICAgIHNvcnQoZXZlbnRzLmJlZ2luKCksIGV2ZW50cy5lbmQoKSwgY29tcCk7CgogICAgaW50IFggPSAwOwogICAgaW50IFkgPSBuOwogICAgbGwgc3RpbGxfbm90X29wZW5fc3VtID0gMExMOwogICAgbGwgbWF4X3RpbWUgPSBldmVudHMuYmFjaygpLnBvejsKICAgIGxsIGFscmVhZHlfY2xvc2VkX3N1bSA9IDBMTDsKCiAgICBmb3IgKGludCBpID0gMDsgaSA8IGV2ZW50cy5zaXplKCk7IGkrKykgewogICAgICAgIGlmIChldmVudHNbaV0udHlwID09IDEpIHsKICAgICAgICAgICAgc3RpbGxfbm90X29wZW5fc3VtICs9IG1heF90aW1lIC0gZXZlbnRzW2ldLnBvejsKICAgICAgICB9CiAgICB9CgogICAgbGwgYW5zID0gSU5GOwogICAgaW50IG1lZXRpbmdfdGltZSA9IC0xOwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgZXZlbnRzLnNpemUoKTsgaSsrKSB7CiAgICAgICAgaW50IHR5cCA9IGV2ZW50c1tpXS50eXA7CiAgICAgICAgbGwgcG96ID0gZXZlbnRzW2ldLnBvejsKCiAgICAgICAgaWYgKHR5cCA9PSAxKSB7CiAgICAgICAgICAgIFktLTsKICAgICAgICAgICAgc3RpbGxfbm90X29wZW5fc3VtIC09IG1heF90aW1lIC0gcG96OwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIFgrKzsKICAgICAgICAgICAgYWxyZWFkeV9jbG9zZWRfc3VtICs9IHBvejsKICAgICAgICB9CgogICAgICAgIGxsIGNvc3RfbGVmdCA9IFgqcG96IC0gYWxyZWFkeV9jbG9zZWRfc3VtOwogICAgICAgIGxsIGNvc3RfcmlnaHQgPSBZKihtYXhfdGltZSAtIHBveikgLSBzdGlsbF9ub3Rfb3Blbl9zdW07CgogICAgICAgIGlmIChhbnMgPiBjb3N0X2xlZnQgKyBjb3N0X3JpZ2h0KSB7CiAgICAgICAgICAgIGFucyA9IGNvc3RfbGVmdCArIGNvc3RfcmlnaHQ7CiAgICAgICAgICAgIG1lZXRpbmdfdGltZSA9IHBvejsKICAgICAgICB9CiAgICB9CgogICAgY291dCA8PCBtZWV0aW5nX3RpbWUgPDwgJyAnIDw8IGFuczsKCiAgICByZXR1cm4gMDsKfQo=