# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
664646 | longvu | Bank (IZhO14_bank) | C++14 | 40 ms | 1472 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.
/**
* author: longvu
* created: 11/27/22 14:55:09
**/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
const int INF = numeric_limits<int>::max();
const int naxn = (int)(21);
const int naxm = (int)(19001);
const int mod = 1e9 + 7;
template<class X, class Y>
bool maximize(X& x, const Y y) {
if (y > x) {x = y; return true;}
return false;
}
template<class X, class Y>
bool minimize(X& x, const Y y) {
if (y < x) {x = y; return true;}
return false;
}
int a[naxn], b[naxn], cnt[(1 << naxn)];
vector<int> memo[naxm];
bitset<(1 << naxn)> vis[naxn], dp[naxn];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
freopen("bank.in","r",stdin);freopen("bank.out","w",stdout);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < m; ++i) {
cin >> b[i];
}
for (int mask = 0; mask < (1 << m); ++mask) {
int sum = 0;
for (int i = 0; i < m; ++i) {
if (mask & (1 << i)) {
sum += b[i];
}
}
cnt[mask] = __builtin_popcount(mask);
memo[sum].push_back(mask);
}
function<bool(int, int)> solve = [&](int i, int mask) {
if (i == n) {
return true;
}
if (vis[i][mask]) {
return (bool)(dp[i][mask]);
}
bool res = false;
for (auto z : memo[a[i]]) {
if (!(mask & z) && n - cnt[mask] + cnt[z] >= n - 1 - i && solve(i + 1, mask | z)) {
res = true;
break;
}
}
vis[i][mask] = true;
dp[i][mask] = res;
return res;
};
cout << (solve(0, 0) ? "YES" : "NO") << '\n';
return 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... |