
10 4
2 1
3 3
4 5
7 9


#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int m,n,w[40],c[40],maxn;
//cur->当前第cur件物品,c_w->当前总重量,value->当前总价值
void dfs(int cur,int c_w,int value){
// cout<<c_w<<" "<<value<<endl;
if(c_w>m) return;
if(cur>n) return;
maxn=max(maxn,value);
dfs(cur+1,c_w+w[cur],value+c[cur]);
dfs(cur+1,c_w,value);
}
void solve(){
cin>>m>>n;
for(int i=0;i<n;i++){
cin>>w[i]>>c[i];
}
dfs(0,0,0);
cout<<maxn;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}