https://www.luogu.com.cn/problem/P1616

#include<bits/stdc++.h>
#define lc p<<1
#define rc p<<1|1
#define INF 2e9
using namespace std;

#define endl '\n'
using ll = long long;
using pii = pair<ll, ll>;
const double PI = acos(-1);
const int N=1e4+10;
int t[N],w[N];
ll f[10000010];
void solve(){
	int T,n;cin>>T>>n;
	for(int i=1;i<=n;i++){
		cin>>t[i]>>w[i];
	}
	for(int i=1;i<=n;i++)
		for(int j=t[i];j<=T;j++){
			f[j]=max(f[j],f[j-t[i]]+w[i]);
		}
	cout<<f[T];
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	
	int T = 1;
//	cin>>T;
	while (T--) {
		solve();
	}
	
	return 0;
}