There are n individual, when the first standing position j i individuals will produce a value ai * (j-1) + bi (nj), j = 1,2, ..., n, to ask how to arrange such that the sum of n a minimum.
The deformed equation to give (ai - bi) * j + bi * n - ai;
seen everyone will have a value of bi * n - ai, the value of this person to stand position independent.
The ai bi is known, the problem is transformed into a very familiar problem of greed
#include<bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i = 0;i<int(n);i++)
typedef long long LL;
int n;
int a[101010],b[101010],c[101010];
LL ans;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
cin>>n;
for(int i = 0;i<n;i++) {
cin>>a[i]>>b[i];
ans += (LL)b[i] * n - a[i];
c[i] = a[i] - b[i];
}
sort(c,c+n);
for(int i = 0 ;i<n;i++){
ans += (LL)c[n-1-i] * (i+1);
}
cout<<ans;
return 0;
}