#include<bits/stdc++.h>
#define ll long long
#define ldb long double
#define fi first
#define se second
#define sza(a) (int)a.size()
#define pir pair<int,int>
#define pirll pair<ll,ll>
using namespace std;
const int maxn = 5e5 + 5;
pir a[maxn];
multiset <int> s;
ll solve(int n){
sort(a + 1,a + 1 + n);
ll res = 0,cur = a[1].fi;
int p = 1;
while (p <= n){
int nxt = p;
cur = max(cur,(ll)a[p].fi);
while (nxt <= n && a[nxt].fi == a[p].fi) nxt++;
for (int i = p ; i < nxt ; i++)
s.insert(a[i].se);
if (nxt > n) break;
int money = a[nxt].fi - a[p].fi;
while (money > 0 && s.size()){
int t = *s.begin();
s.erase(s.begin());
int M = min(t,money);
t -= M;
money -= M;
cur += M;
if (t > 0) s.insert(t);
else res += cur;
}
p = nxt;
}
for (auto it = s.begin() ; it != s.end() ; it++){
cur += *it;
res += cur;
}
return res;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n;
cin >> n;
for (int i = 1 ; i <= n ; i++) cin >> a[i].fi >> a[i].se;
cout << solve(n);
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBsZGIgbG9uZyBkb3VibGUKI2RlZmluZSBmaSBmaXJzdAojZGVmaW5lIHNlIHNlY29uZAojZGVmaW5lIHN6YShhKSAoaW50KWEuc2l6ZSgpCiNkZWZpbmUgcGlyIHBhaXI8aW50LGludD4KI2RlZmluZSBwaXJsbCBwYWlyPGxsLGxsPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjb25zdCBpbnQgbWF4biA9IDVlNSArIDU7CgpwaXIgYVttYXhuXTsKbXVsdGlzZXQgPGludD4gczsKCmxsIHNvbHZlKGludCBuKXsKCXNvcnQoYSArIDEsYSArIDEgKyBuKTsKCWxsIHJlcyA9IDAsY3VyID0gYVsxXS5maTsKCQoJaW50IHAgPSAxOwoJd2hpbGUgKHAgPD0gbil7CgkJaW50IG54dCA9IHA7Cg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