This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int n,m;
vector<int> salaries;
vector<int> billets;
#define BS bitset
bool mem[22][(1<<21)];
void initMem(){
for(int i=0;i<22;i++){
for(int j=0;j<(1<<21);j++){
mem[i][j]=false;
}
}
}
bool solved(int pos,BS<20> bs){
if(mem[pos][bs.to_ulong()]){
return false;
}
mem[pos][bs.to_ulong()]=true;
if(pos==n)return true;
vector<int> index;
for(int i=0;i<m;i++){
if(!bs[i]){
index.push_back(i);
}
}
const int S = int(index.size());
for(int i=0;i<(1<<S);i++){
BS<20> bs2 = i;
int sum=0;
for(int j=0;j<S;j++){
if(bs2[j]){
sum+=billets[index[j]];
}
if(sum>salaries[pos])break;
}
if(sum==salaries[pos]){
BS<20> cbs=bs;
for(int j=0;j<S;j++){
if(bs2[j]){
cbs[index[j]]=1;
}
}
if(solved(pos+1,cbs)){
return true;
}
}
}
return false;
}
signed main(){
initMem();
cin>>n>>m;
salaries.resize(n);
billets.resize(m);
for(int i=0;i<n;i++){
cin>>salaries[i];
}
for(int i=0;i<m;i++){
cin>>billets[i];
}
sort(billets.begin(),billets.end());
if(solved(0,0)){
cout<<"YES\n";
}else{
cout<<"NO\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |