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 <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <cmath>
#include <bitset>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()
constexpr int MAXN = 1 << 20;
void solve() {
int n, m;
cin >> n >> m;
vector<int> a(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
vector<int> b(m);
for (int &x : b) {
cin >> x;
}
// suppongo di aggiustare una persona alla volta
int mx = 1 << m;
vector<bitset<MAXN>> dp(n + 1);
dp[0][0] = true;
vector<vector<int>> good_mask(n + 1);
// per ogni persona vedo che mask va bene
for (int i = 1; i <= n; ++i) {
// itero su tutte le submask
for (int mask = 0; mask < mx; ++mask) {
int sum = 0;
for (int j = 0; j < mx; ++j) {
if (mask & (1 << j)) {
sum += b[i];
}
}
if (sum == a[i]) {
good_mask[i].push_back(mask);
}
}
}
for (int i = 0; i < n; ++i) {
for (int mask = 0; mask < mx; ++mask) {
if (!dp[i][mask]) {
continue;
}
int cur = __builtin_popcount(mask);
for (int new_mask : good_mask[i + 1]) {
int cur_add = __builtin_popcount(new_mask);
int msk = mask | new_mask;
if (__builtin_popcount(msk) == cur + cur_add) { // e' un set indipendente
dp[i + 1][msk] = true;
}
}
}
}
for (int mask = 0; mask < mx; ++mask) {
if (dp[n][mask]) {
cout << "YES\n";
return;
}
}
cout << "NO\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1; // cin >> tt;
while (tt--) {
solve();
}
return 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... |