参考:https://www.luogu.com.cn/blog/leleleeee/solution-cf1003d
https://blog.csdn.net/sinat_37158899/article/details/80969585
#include<bits/stdc++.h> using namespace std; typedef long long ll; int n,q,b; int temp; map<int,int>mp; int main() { cin>>n>>q; for(int i=0;i<n;i++) { cin>>temp; mp[temp]++; } map<int,int>::iterator it; for(int i=1;i<=q;i++) { int ans=0; cin>>b; it=mp.end(); while(1) { it--; int minn=min(b/(it->first),it->second); b-=minn*(it->first); ans+=minn; if(it==mp.begin()&&b!=0) { cout<<"-1"<<endl; break; } else if(b==0) { cout<<ans<<endl; break; } } } return 0; }View Code