This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// oh, these hills, they burn so bright / oh, these hills, they bring me life
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define all(x) begin(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)(x.size())
#define inf 1000000010
#define linf 0x3f3f3f3f3f3f3f3f
#define mp make_pair
#define f first
#define s second
#define pi pair<int, int>
#ifdef __INTELLISENSE__
#include "/mnt/c/yukon/pp.hpp"
#endif
#ifndef LOCAL
#define endl "\n"
#else
#include "/mnt/c/yukon/pp.hpp"
#endif
int main() {
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 i = 0; i < m; i++) {
cin >> b[i];
}
vector<vector<vector<int>>> combos(n + 1);
for (int i = 1; i <= n; i++) {
vector<vector<vector<int>>> pp(a[i] + 1);
pp[0].push_back(vector<int>());
for (int bank = 0; bank < m; bank++) {
for (int val = 1; val <= a[i]; val++) {
if (val - b[bank] >= 0) {
int bruhmoment = sz(pp[val]);
pp[val].insert(pp[val].end(), pp[val - b[bank]].begin(), pp[val - b[bank]].end());
for (int l = bruhmoment; l < sz(pp[val]); l++) {
pp[val][l].push_back(b[bank]);
}
}
}
}
combos[i] = pp[a[i]];
}
vector<vector<bool>> dp(n + 1, vector<bool>(1 << m, false));
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (auto &k : combos[i]) {
for (int mask = 0; mask < (1 << m); mask++) {
bool good = 1;
int before = mask;
for (int &l : k) {
if (mask & (1 << l)) {
good = 0;
break;
}
before ^= (1 << l);
}
if (!good)
continue;
dp[i][mask] = dp[i - 1][before];
if (i == n && dp[i][mask]) {
cout << "YES" << endl;
return 0;
}
}
}
}
cout << "NO" << endl;
}
// don't get stuck on one approach
// question bounds
// flesh out your approach before implementing o.o
// math it out
// ok well X is always possible, how about X + 1 (etc.)
# | 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... |