这道题目直接被猜中了贪心的策略= =。就是对于每只羊的t,d如果d/t越大,那么就应该先被送回羊圈。证明的话比较复杂一点,discuss里面有。
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<map>
#include<set>
#include<bitset>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,num) scanf("%d%d%d",&a,&b,&num)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
#define ll __int64
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
//#pragma comment(linker,"/STACK:1024000000,1024000000")
int n,m;
#define M 110
#define N 1000010
#define Mod 258280327
#define p(x,y) make_pair(x,y)
const int MAX_len=550;
struct Node{
ll t,d;
double rte;
bool operator < (const Node &x)const{
return rte>x.rte;
}
}a[100010];
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
while(sf(n)!=EOF){
ll sum = 0;
for(int i=0;i<n;i++){
scanf("%I64d%I64d",&a[i].t,&a[i].d);
a[i].rte = a[i].d*1.0/a[i].t;
sum += a[i].d;
}
sort(a,a+n);
ll ans=0;
for(int i=0;i<n;i++){
sum -= a[i].d;
ans += 2*a[i].t*sum;
}
pfI(ans);
}
return 0;
}