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>
#define fi first
#define se second
#define getbit(x, i) (((x) >> (i)) & 1)
#define all(x) x.begin(), x.end()
#define TASK ""
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const long long INF = 1e18;
const int mod = 1e9 + 7;
int dp[1 << 20], leftover[1 << 20];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector <int> a(n), b(m);
for (int &i : a) cin >> i;
for (int &i : b) cin >> i;
for (int i = 0; i < (1 << 20); ++i) leftover[i] = dp[i] = -1;
leftover[0] = dp[0] = 0;
for (int mask = 0; mask < (1 << m); ++mask)
for (int i = 0; i < m; ++i) {
if (!getbit(mask, i)) continue;
int prev = mask ^ (1 << i);
if (dp[prev] == -1) continue;
int new_amt = leftover[prev] + b[i];
int cur_target = a[dp[prev]];
if (new_amt < cur_target) {
dp[mask] = dp[prev];
leftover[mask] = new_amt;
} else if (new_amt == cur_target) {
dp[mask] = dp[prev] + 1;
leftover[mask] = 0;
}
if (dp[mask] == n) return cout << "YES", 0;
}
cout << "NO\n";
}
# | 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... |