度度熊与邪恶大魔王
Accepts: 2395
Submissions: 14900
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
618
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; typedef long long LL; const LL mmm=1e18; LL a[100005],b[100005]; LL dp[1005][15]; LL p[1005],k[1005]; int main() { int n,m,i,j,t; LL ma,mb,mp,hurt,ans; while(cin>>n>>m) { ma=0;mb=0;mp=0; memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) { cin>>a[i]>>b[i]; ma=max(ma,a[i]); mb=max(mb,b[i]); } for(i=1;i<=m;i++) { cin>>k[i]>>p[i]; mp=max(mp,p[i]); } if(mp<=mb) { cout<<"-1"<<endl; continue; } for(i=1;i<=ma;i++) { for(j=0;j<=mb;j++) { dp[i][j]=mmm; for(t=1;t<=m;t++) { hurt=p[t]-j; if(hurt>i) { dp[i][j]=min(dp[i][j],k[t]); } else if(hurt>0) { dp[i][j]=min(dp[i][j],dp[i-hurt][j]+k[t]); } } } } ans=0; for(i=1;i<=n;i++) { ans=ans+dp[a[i]][b[i]]; } cout<<ans<<endl; } return 0; }