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>
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ff first
#define int long long
#define ss second
#define pb push_back
#define y1 zildjian
using namespace std;
const int N = 2e6+100;
const int INF = 1e18;
const int mod = 1e9+7;
const int dx[] = {-1,-1,0,1,1,1,0,-1,0};
const int dy[] = {0,1,1,1,0,-1,-1,-1,0};
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int binpow (int a, int n){
if (n == 0)
return 1;
if (n % 2 == 1)
return (binpow (a, n-1)%mod * a%mod)%mod;
else {
int b = binpow (a, n/2) % mod;
return (b%mod * b%mod)%mod;
}
}
int n,m;
int a[N],b[N];
vector<int> ms[N];
vector<int> path[N];
bool go(int mask, int cur){
if(mask&cur) return 0;
if(path[mask].empty()) return 1;
bool check = 0;
for(auto x:path[mask]){
check |= go(x,mask);
}
return check;
}
void solve(){
cin>>n>>m;
for(int i = 1;i<=n;i++){
cin>>a[i];
}
for(int i = 1;i<=m;i++){
cin>>b[i];
}
int cnt = 0;
ms[0].pb(0);
for(int i = 1;i<=n;i++){
bool f = 0;
for(int mask = 0;mask<(1<<m);mask++){
int sum = 0;
for(int j = 1;j<=m;j++){
if(1&(mask>>(j-1))) sum+=b[j];
}
if(sum == a[i]){
bool ok = 0;
vector<int> mem;
for(auto prevmask:ms[i-1]){
if(go(prevmask,mask)){
ok = 1;
mem.pb(prevmask);
}
}
if(ok){
f = 1;
ms[i].pb(mask);
for(auto x:mem) path[mask].pb(x);
}
}
}
if(!f){
no;
return;
}
}
yes;
}
signed main(){
// freopen("floyd.in","r",stdin);
// freopen("floyd.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
// cout.tie(nullptr);
int T = 1;
// cin>>T;
for(int i = 1;i<=T;i++){
// cout<<"Case "<<i<<": ";
solve();
}
}
Compilation message (stderr)
bank.cpp: In function 'void solve()':
bank.cpp:53:9: warning: unused variable 'cnt' [-Wunused-variable]
53 | int cnt = 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... |