#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
template<class X, class Y> bool maximize (X &x, const Y &y) {return x < y ? x = y, 1 : 0;}
template<class X, class Y> bool minimize (X &x, const Y &y) {return x > y ? x = y, 1 : 0;}
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int n, m;
int salary[20], notes[20];
pair<int, int> dp[1 << 20];
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n >> m;
for (int i = 0; i < n; ++i) cin >> salary[i];
for (int i = 0; i < m; ++i) cin >> notes[i];
for (int i = 0; i < (1 << m); ++i) dp[i] = {-1, -1};
dp[0] = {0, 0};
for (int i = 0; i < (1 << m); ++i) {
for (int j = 0; j < m; ++j) {
if (i >> j & 1) {
pair<int, int> t = dp[i^(1<<j)];
if (t.fi == -1) continue;
if (notes[j] + t.se == salary[t.fi]) {
dp[i] = {t.fi + 1, 0};
}
else if (notes[j] + t.se < salary[t.fi]) {
dp[i] = {t.fi, t.se + notes[j]};
}
}
}
if (dp[i].fi == n) {
cout << "YES";
return 0;
}
}
cout << "NO";
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... |