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 int long long
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
vector <int> a(n), vm(m), b((1 << m)), dp((1 << m), -1);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
for(int j = 0; j < m; j++){
cin >> vm[j];
}
dp[0] = 0;
b[0] = 0;
for (int s = 1; s < (1 << m); s++) {
for (int i = 0; i < m; i++){
if ((s & (1 << i))) {
int pr = s - (1 << i);
if (dp[pr] == n) {
dp[s] = n;
break;
}
if (dp[pr] == -1) {
continue;
}
if (b[pr] + vm[i] < a[dp[pr]]) {
dp[s] = dp[pr];
b[s] = b[pr] + vm[i];
break;
}
if (b[pr] + vm[i] == a[dp[pr]]) {
dp[s] = dp[pr] + 1;
b[s] = 0;
break;
}
}
}
}
if (dp[(1 << m) - 1] == n) {
cout << "YES\n";
}
else {
cout << "NO\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
/*
3
0 0 0
9 8 5 7
*/
# | 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... |