rt,WA on #12 #17。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2<<18;
int dp[N],n,m,t[25],w[25],V[N],W[N];
int low(int x){
return x&(-x);
}
signed main(){
cin>>m>>n;
for(int i=1;i<=n;i++){
cin>>t[i]>>w[i];
W[1<<(i-1)]=t[i];
V[1<<(i-1)]=w[i];
}
for(int i=1;i<(1<<n);i++){
V[i]=V[i^low(i)]+V[low(i)];
W[i]=max(W[i^low(i)],W[low(i)]);
}
memset(dp,0x3f,sizeof dp);
dp[0]=0;
for(int i=0;i<(1<<n);i++){
if(W[i]<=m)
dp[i]=V[i];
for(int j=i;j;j=i&(j-1))
if(V[j]<=m)
dp[i]=min(dp[i],dp[i^j]+W[j]);
}
cout<<dp[(1<<n)-1];
return 0;
}