Language: C++

#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <numeric>
#include <cctype>
#include <bitset>
#include <cassert>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define df(x) int x = in()
#define dame { puts("0"); return 0;}
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,vector<T>,greater<T> >
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() { int x; scanf("%d",&x); return x;}
inline void priv(vi a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');}
template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(vector<T>&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
const int MX = 5005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;


int main() {
  int n;
  scanf("%d",&n);
  if (n == 1) dame;
  vp p(n);
  rep(i,n) scanf("%d%d",&p[i].fi,&p[i].se);
  sort(rng(p),[&](P a, P b){
    return a.fi+a.se > b.fi+b.se;
  });
  vl dp(1,LINF);
  dp[0] = 0;
  vl d;
  rep(k,n) {
    d = vl(k+2,LINF);
    swap(dp,d);
    ll l = p[k].fi, r = p[k].se, w = r+l;
    rep(j,k+1) {
      int g = k-j;
      mins(dp[j], d[j]+r+w*g);
      mins(dp[j+1], d[j]+l+w*j);
    }
  }
  ll ans = LINF;
  rep(i,n+1) mins(ans,dp[i]);
  rep(i,n) {
    ll l = p.back().fi, r = p.back().se;
    int j = n-1-i;
    mins(ans, d[i]+r*i+l*j);
  }
  cout<<ans<<endl;
  return 0;
}





























#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <numeric>
#include <cctype>
#include <bitset>
#include <cassert>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define df(x) int x = in()
#define dame { puts("0"); return 0;}
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,vector<T>,greater<T> >
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() { int x; scanf("%d",&x); return x;}
inline void priv(vi a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');}
template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(vector<T>&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
const int MX = 5005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;


int main() {
  int n;
  scanf("%d",&n);
  if (n == 1) dame;
  vp p(n);
  rep(i,n) scanf("%d%d",&p[i].fi,&p[i].se);
  sort(rng(p),[&](P a, P b){
    return a.fi+a.se > b.fi+b.se;
  });
  vl dp(1,LINF);
  dp[0] = 0;
  vl d;
  rep(k,n) {
    d = vl(k+2,LINF);
    swap(dp,d);
    ll l = p[k].fi, r = p[k].se, w = r+l;
    rep(j,k+1) {
      int g = k-j;
      mins(dp[j], d[j]+r+w*g);
      mins(dp[j+1], d[j]+l+w*j);
    }
  }
  ll ans = LINF;
  rep(i,n+1) mins(ans,dp[i]);
  rep(i,n) {
    ll l = p.back().fi, r = p.back().se;
    int j = n-1-i;
    mins(ans, d[i]+r*i+l*j);
  }
  cout<<ans<<endl;
  return 0;
}































Compiler output:
a.cc:169:12: error: redefinition of 'int in()'
  169 | inline int in() { int x; scanf("%d",&x); return x;}
      |            ^~
a.cc:46:12: note: 'int in()' previously defined here
   46 | inline int in() { int x; scanf("%d",&x); return x;}
      |            ^~
a.cc:170:13: error: redefinition of 'void priv(vi)'
  170 | inline void priv(vi a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');}
      |             ^~~~
a.cc:47:13: note: 'void priv(vi)' previously defined here
   47 | inline void priv(vi a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');}
      |             ^~~~
a.cc:171:30: error: redefinition of 'template<class T> std::istream& operator>>(std::istream&, std::vector<_Tp>&)'
  171 | template<typename T>istream& operator>>(istream&i,vector<T>&v)
      |                              ^~~~~~~~
a.cc:48:30: note: 'template<class T> std::istream& operator>>(std::istream&, std::vector<_Tp>&)' previously declared here
   48 | template<typename T>istream& operator>>(istream&i,vector<T>&v)
      |                              ^~~~~~~~
a.cc:173:28: error: redefinition of 'template<class T> std::string join(std::vector<_Tp>&)'
  173 | template<typename T>string join(vector<T>&v)
      |                            ^~~~
a.cc:50:28: note: 'template<class T> std::string join(std::vector<_Tp>&)' previously declared here
   50 | template<typename T>string join(vector<T>&v)
      |                            ^~~~
a.cc:175:30: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, std::vector<_Tp>&)'
  175 | template<typename T>ostream& operator<<(ostream&o,vector<T>&v)
      |                              ^~~~~~~~
a.cc:52:30: note: 'template<class T> std::ostream& operator<<(std::ostream&, std::vector<_Tp>&)' previously declared here
   52 | template<typename T>ostream& operator<<(ostream&o,vector<T>&v)
      |                              ^~~~~~~~
a.cc:177:43: error: redefinition of 'template<class T1, class T2> std::istream& operator>>(std::istream&, std::pair<_T1, _T2>&)'
  177 | template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v)
      |                                           ^~~~~~~~
a.cc:54:43: note: 'template<class T1, class T2> std::istream& operator>>(std::istream&, std::pair<_T1, _T2>&)' previously declared here
   54 | template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v)
      |                                           ^~~~~~~~
a.cc:179:43: error: redefinition of 'template<class T1, class T2> std::ostream& operator<<(std::ostream&, std::pair<_T1, _T2>&)'
  179 | template<typename T1,typename T2>ostream& operator<<(ostream&o,pair<T1,T2>&v)
      |                                           ^~~~~~~~
a.cc:56:43: note: 'template<class T1, class T2> std::ostream& operator<<(std::ostream&, std::pair<_T1, _T2>&)' previously declared here
   56 | template<typename T1,typename T2>ostream& operator<<(ostream&o,pair<T1,T2>&v)
      |                                           ^~~~~~~~
a.cc:181:11: error: redefinition of 'const int MX'
  181 | const int MX = 5005, INF = 1001001001;
      |           ^~
a.cc:58:11: note: 'const int MX' previously defined here
   58 | const int MX = 5005, INF = 1001001001;
      |           ^~
a.cc:181:22: error: redefinition of 'const int INF'
  181 | const int MX = 5005, INF = 1001001001;
      |                      ^~~
a.cc:58:22: note: 'const int INF' previously defined here
   58 | const int MX = 5005, INF = 1001001001;
      |                      ^~~
a.cc:182:10: error: redefinition of 'const ll LINF'
  182 | const ll LINF = 1e18;
      |          ^~~~
a.cc:59:10: note: 'const ll LINF' previously defined here
   59 | const ll LINF = 1e18;
      |          ^~~~
a.cc:183:14: error: redefinition of 'const double eps'
  183 | const double eps = 1e-10;
      |              ^~~
a.cc:60:14: note: 'const double eps' previously defined here
   60 | const double eps = 1e-10;
      |              ^~~
a.cc:186:5: error: redefinition of 'int main()'
  186 | int main() {
      |     ^~~~
a.cc:63:5: note: 'int main()' previously defined here
   63 | int main() {
      |     ^~~~