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>
using namespace std;
int n, m, a[20], b[20];
int curr[20];
bool solve(int idx, int mask) {
if(n - __builtin_popcount(mask) > m - idx) return false;
if(mask == (1 << n) - 1) return true;
if(solve(idx + 1, mask)) return true;
for(int i = 0; i < n; i++) if(!(mask >> i & 1)) {
bool res = false;
curr[i] += b[idx];
if(curr[i] == a[i]) res |= solve(idx + 1, mask | 1 << i);
else res |= solve(idx + 1, mask);
curr[i] -= b[idx];
if(res) return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < m; i++) cin >> b[i];
if(solve(0, 0)) cout << "YES" << endl;
else cout << "NO" << endl;
}
# | 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... |