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;
const int MAXN = 21;
#define ALL(a) (a).begin(), (a).end()
int N, M;
int a[MAXN];
int b[MAXN];
int dp[MAXN][1 << MAXN];
int calc(int mask, int p, int g, int s) {
int &res = dp[p][mask];
if(res != -1) return res;
if(g == N) return res = true;
if(s == a[g]) return res = calc(mask, 0, g + 1, 0);
if(p == M) return res = false;
res = false;
for(int i = p; i < M; i++) {
if((mask >> i & 1) == 0 and s + b[i] <= a[g]) {
// printf("%d %d %d %d -> %d %d %d %d", mask, p, g, s, mask | (1 << i), i + 1, g, s + b[i]);
res |= calc(mask | (1 << i), i + 1, g, s + b[i]);
}
}
return res;
}
signed main() {
#define TASK "code"
if (fopen(TASK ".inp", "r")) {
freopen(TASK ".inp", "r", stdin);
freopen(TASK ".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
for(int i = 0; i < N; i++) cin >> a[i];
for(int i = 0; i < M; i++) cin >> b[i];
memset(dp, -1, sizeof dp);
cout << (calc(0, 0, 0, 0) ? "YES" : "NO");
return (0 ^ 0);
}
Compilation message (stderr)
bank.cpp: In function 'int main()':
bank.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen(TASK ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
bank.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen(TASK ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |