#include #define maxs 2020202 #define inf 0xfffffffffffff #define mme(i,j) memset(i,j,sizeof(i)) using namespace std; int hp[100005]; int de[100005]; int mp[10005]; int injure[10005]; long long dp[10005][15]; int cost[1005]; //int cmpmon(node a,node b) //{ // return a.de<=b.de; //} // //int cmpkill(nnode a,nnode b){ // return a.injure<=b.injure; //} int main() { int n,m; while(~scanf("%d%d",&n,&m)) { for(int i=0;i<1007;i++) cost[i] = 100000; for(int i=0;i<1011;i++) { for(int j=0;j<20;j++) { dp[i][j]=inf; } } int mxde=-1; for(int i=1;i<=n;i++){ scanf("%d%d",&hp[i],&de[i]); if(mxde=0) { dp[i][j] = min(dp[i][j],dp[i-hp][j]+cost[injure]); } } } } for(int i=1009;i>=0;i--) { for(int j=0;j<=10;j++) dp[i][j]=min(dp[i][j],dp[i+1][j]); } long long tp; int fl=0; long long ans=0; for(int i=1;i<=n;i++) { tp = inf; for(int j=0;j<=11;j++) { if(j>=de[i]){ tp=min( tp,dp[ hp[i] ][ j ] ); } } if(tp == inf ) { ans=-1; break; } ans+=tp; } printf("%I64d\n",ans); } return 0; }