# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1112886 | luvna | Bank (IZhO14_bank) | C++14 | 78 ms | 10712 KiB |
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 all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int N = 21;
template<class T> bool maximize(T& a, T b) {if(a < b) return a = b, true; return false;}
int n, m;
int a[N], b[N];
int dp[(1 << 21)+15];
int sum[(1 << 21)+15];
void solve(){
cin >> n >> m;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < m; i++) cin >> b[i];
dp[0] = 1;
for(int mask = 0; mask < (1 << m); mask++){
if(!dp[mask]) continue; //useless
int target = a[dp[mask]-1];
for(int i = 0; i < m; i++){
if(mask >> i & 1) continue;
int nxt_mask = mask + (1 << i);
if(sum[mask] + b[i] == target){//can satisfy dp[mask]-1 customer
if(maximize(dp[nxt_mask], dp[mask]+1)) sum[nxt_mask] = 0;
}
else{
if(maximize(dp[nxt_mask], dp[mask])) sum[nxt_mask] = sum[mask] + b[i];
}
}
}
cout << (dp[(1 << m)-1] == n+1 ? "YES" : "NO") << endl;
}
signed main(){
ios_base::sync_with_stdio(NULL);
cin.tie(0); cout.tie(0);
#define task "task"
if(fopen(task".INP", "r")){
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
int t; t = 1; //cin >> t;
while(t--) solve();
}
Compilation message (stderr)
# | 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... |