#include #include using namespace std; const int N=100010,M=1010,inf=~0U>>1; long long ans; int n,m,i,j,B,f[M],g[11][M]; struct P{int x,y;}a[N],b[M]; inline void up(int&a,int b){a>b?(a=b):0;} int main(){ while(~scanf("%d%d",&n,&m)){ for(i=0;iB)up(f[min(i+b[j].y-B,1000)],f[i]+b[j].x); for(i=999;i;i--)up(f[i],f[i+1]); for(i=1;i<=1000;i++)g[B][i]=f[i]; } ans=0; for(i=0;i