#include<bits/stdc++.h> #define FOR(i,s,e) for(int i=(s);i<=(e);i++) #define FORD(i,s,e) for(int i=(s);i>=(e);i--) #define FOREACH(i,c) for( __typeof((c).begin()) i=(c).begin();i!=(c).end();++i) #define ALL(k) (k).begin(),(k).end() #define e1 first #define e2 second #define mp make_pair #define pb push_back #define eb emplace_back using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; const bool print=false; const int MAXNBR=5111; LL dp[MAXNBR][MAXNBR]; const int MAXN=1000111; PLL t[MAXN]; main(){ int n;scanf("%d",&n); FOR(i,1,n){ LL a,b;scanf("%lld%lld",&b,&a); t[i]=mp(b,a); } sort(t+1,t+n+1); if(n<MAXNBR){ FOR(k,1,n){ FOR(i,1,n){ dp[k][i]=max(dp[k][i-1],dp[k-1][i-1]+t[i].e1*(k-1)+t[i].e2); } printf("%lld\n",dp[k][n]); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include<bits/stdc++.h> #define FOR(i,s,e) for(int i=(s);i<=(e);i++) #define FORD(i,s,e) for(int i=(s);i>=(e);i--) #define FOREACH(i,c) for( __typeof((c).begin()) i=(c).begin();i!=(c).end();++i) #define ALL(k) (k).begin(),(k).end() #define e1 first #define e2 second #define mp make_pair #define pb push_back #define eb emplace_back using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; const bool print=false; const int MAXNBR=5111; LL dp[MAXNBR][MAXNBR]; const int MAXN=1000111; PLL t[MAXN]; main(){ int n;scanf("%d",&n); FOR(i,1,n){ LL a,b;scanf("%lld%lld",&b,&a); t[i]=mp(b,a); } sort(t+1,t+n+1); if(n<MAXNBR){ FOR(k,1,n){ FOR(i,1,n){ dp[k][i]=max(dp[k][i-1],dp[k-1][i-1]+t[i].e1*(k-1)+t[i].e2); } printf("%lld\n",dp[k][n]); } } } |