请各位大佬帮忙看看是否还可以优化 QAQ!
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
const int N=1e3+5;
int n,m,k,cnt,t,tot,f[500005],money[20000],maxn;
int main()
{
//freopen("watch.in","r",stdin);
//freopen("watch.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&k,&cnt);
cnt=min(cnt,500000/k);
for(int j=1;j<=cnt;j*=2) money[++tot]=k*j,cnt-=j;
if(cnt) money[++tot]=k*cnt;
}
sort(money+1,money+1+tot),f[0]=1;
for(int i=1;i<=tot;i++)
{
int temp=min(maxn,500000-money[i]);
for(int j=temp;j>=0;j--)
f[j+money[i]]|=f[j],maxn=max(maxn,j+money[i]);
}
for(int i=1;i<=m;i++) scanf("%d",&t),printf(f[t]?"Yes\n":"No\n");
return 0;
}