이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
//#define int ll
int n, m, a[21], b[21];
bool v[20010];
bool vm[21][(1ll<<20)-1 ];
vector<int> msk[21];
vector<pii> g[20010];
int sum[21];
void knapsack_graph(){
v[0]=true;
for(int i=1; i<=m; i++){
for(int sum=0; sum<=(20000-b[i]); sum++){
if( (v[sum]==true)){
g[sum].pb({sum+b[i], i});
v[sum+b[i]]=true;
}
}
}
return;
}
void dfs(int s, int ac, int pt){
if(s==sum[pt]){
if(vm[pt][ac]!=true){
vm[pt][ac]=true;
msk[pt].pb(ac);
}
return;
}
for(auto pr:g[s]){
int u=pr.ft, tp=pr.sc;
if( ((ac&(1ll<<(tp-1) ))==0) && (u<=sum[pt]) ){
dfs(u, (ac|(1ll<<(tp-1) )), pt);
}
}
return;
}
void search_graph(){
msk[0].pb(0);
for(int i=0; i<n; i++){
for(int ac:msk[i]){
dfs(sum[i], ac, i+1);
}
}
return;
}
int32_t main(){
INIT
cin>>n>>m;
for(int i=1; i<=n; i++){
cin>>a[i];
sum[i]=sum[i-1];
sum[i]+=a[i];
}
for(int i=1; i<=m; i++){
cin>>b[i];
}
knapsack_graph();
search_graph();
/*
for(int ac:msk[n]){
cout<<ac<<"\n";
}
*/
if(msk[n].size()>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... |