WA on #5,#6,#7,#9
#include<bits/stdc++.h>
using namespace std;
struct s
{
int p;
int t;
}a[1010];
int n,k,f[1010];
bool cmp(struct s q,struct s h)
{
return q.p<h.p;
}
int main()
{
cin>>n>>k;
for(int i=1;i<=k;i++)
{
cin>>a[i].p>>a[i].t;
if(a[i].p+a[i].t-1>n) a[i].t=n+1-a[i].p;
}
for(int i=1;i<=n;i++)f[i]=10001;
sort(a+1,a+1+k,cmp);
for(int i=1;i<=k;i++)
{
for(int j=a[i].p-1;j>=0&&f[j]!=10002;j--)
{
if(f[j]==10001) continue;
f[a[i].p+a[i].t-1]=min(f[a[i].p+a[i].t-1],f[j]+a[i].t);
if(a[i].p!=a[i+1].p||i+1>n) f[j]=10002;
}
}
int ans=10003;
for(int i=n;i>=0&&f[i]!=10002;i--) ans=min(ans,f[i]);
cout<<n-ans;
}