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 main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,m;cin>>n>>m;
vector<int>arr(n);
for (int i = 0;i<n;++i)cin>>arr[i];
vector<int>brr(m);
for (int i = 0 ;i<m;++i)cin>>brr[i];
sort(brr.begin(),brr.end());
vector<vector<int>>pos(n);
for (int i = 0;i < (1<<m);++i){
vector<int>cur;
for (int j = 0;j<m;++j){
if (i & (1<<j)){
cur.push_back(brr[j]);
}
}
set<int>c;
c.insert(0);
for (auto x:cur){
set<int>nex;
for (auto y:c){
if (y + x <=1000){
nex.insert(y + x);
}
}
swap(nex,c);
}
for (int j = 0;j<n;++j){
if (c.find(arr[j])!=c.end()){
pos[j].push_back(i);
}
}
}
vector<vector<int>>dp(n,vector<int>((1<<20),-1));
function<bool(int,int)>solve = [&](int u,int mask){
if (u == n)return 1;
if (dp[u][mask]!=-1)return dp[u][mask];
int ans = 0;
for (auto x:pos[u]){
if ((mask & x))continue;
ans|=solve(u + 1,(mask | x));
}
return dp[u][mask] = ans;
};
bool ans = solve(0,0);
if (ans){
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... |