#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define ll long long int ll const MOD = 1000000007; ll const INF = (long long int)1 << 61; ll mypow(ll x,ll n){ ll ret = 1; while(n > 0){ if(n&1){ ret = (ret*x)%MOD; } x = (x*x)%MOD; n >>= 1; } return ret; } int main(){ cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; vector a(n),b(n); for(int i = 0; i < n; i++){ cin >> a[i] >> b[i]; } vector t; ll z = 0; bool flag = true; for(int i = 1; i <= n; i++){ if(i % 3 == 0){ t.push_back(i/3 + (i/3 - 1)*2); } } vector used(n,false); for(int i = 0; i < t.size(); i++){ ll m = 0; ll midx = 0; for(int j = 0; j < n; j++){ if(!used[j]){ if(a[j] + t[i]*b[j] > m){ m = a[j] + t[i]*b[j]; midx = j; } } } used[midx] = true; } vector> s; for(int i = 0; i < n; i++){ if(!used[i]){ s.push_back(make_pair(b[i],a[i])); } } sort(s.begin(),s.end(),greater>()); ll ans = 0; for(int i = 0; i < s.size(); i++){ ans += s[i].first*i + s[i].second; } cout << ans << endl; return 0; }