# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1047699 | manhlinh1501 | Bank (IZhO14_bank) | C++17 | 5 ms | 45404 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>
using namespace std;
const int MAXN = 21;
#define ALL(a) (a).begin(), (a).end()
int N, M;
int a[MAXN];
int b[MAXN];
bool dp[MAXN][1 << MAXN];
int sum[1 << MAXN];
namespace subtask {
void solution() {
for(int mask = 0; mask < (1 << M); mask++) {
for(int j = 0; j < M; j++) {
if(mask >> j & 1)
sum[mask] += b[j];
}
}
if(N == 1) {
for(int mask = 0; mask < (1 << M); mask++) {
if(a[N] == sum[mask]) return cout << "YES", void(0);
}
cout << "NO";
exit(0);
}
dp[0][0] = true;
for(int i = 1; i <= N; i++) {
for(int mask = 0; mask < (1 << M); mask++) {
int nmask = ((1 << M) - 1) ^ mask;
for(int smask = nmask; smask > 0; smask = (nmask & (smask - 1))) {
if(sum[smask] == a[i])
dp[i][mask ^ smask] |= dp[i - 1][mask];
}
}
}
for(int mask = 0; mask < (1 << M); mask++) {
if(dp[N][mask]) return cout << "YES", void(0);
}
cout << "NO";
exit(0);
}
}
bool calc(int mask, int p, int g, int s) {
bool &res = dp[p][mask];
if(res != -1) return res;
if(p == M) return res = false;
if(g == N + 1) return res = true;
if(s == a[g]) return calc(0, 0, g + 1, 0);
res = 0;
for(int i = p; i < M; i++) {
if((mask >> i & 1) == 0 and s + b[i] <= a[g])
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 = 1; i <= N; i++) cin >> a[i];
for(int i = 0; i < M; i++) cin >> b[i];
if(accumulate(a + 1, a + N + 1, 0) > accumulate(b, b + M, 0))
return cout << "NO", 0;
memset(dp, -1, sizeof dp);
cout << (calc(0, 0, 1, 0) ? "YES" : "NO");
return (0 ^ 0);
}
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... |