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;
typedef long long ll;
#define MOD 1000000007LL
int N, M;
int a[20], b[20];
pair<int, int> dp[1<<20];
int main() {
//freopen("feast.in", "r", stdin);
//freopen("feast.out", "w", stdout);
//ios_base::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];
dp[0] = {0, 0};
for (int bm = 1; bm < (1 << M); bm++) {
dp[bm] = {0, 0};
for (int i = 0; i < M; i++) {
if (bm & (1<<i)) {
int nbm = bm^(1<<i);
pair<int, int> pp = dp[nbm];
if (pp.second + b[i] < a[pp.first]) {
// cout << "add " << b[i] << " to " << pp.second << " (a[pp.first] = " << a[pp.first] << ")\n";
pp.second += b[i];
}
else if (pp.second+b[i] == a[pp.first]) {
// cout << "add " << b[i] << " to " << pp.second << " (a[pp.first] = " << a[pp.first] << ")\n";
pp.first++, pp.second = 0;
}
if (dp[bm] < pp) {
// cout << dp[bm].first << " " << dp[bm].second << "\n";
// cout << pp.first << " " << pp.second << "\n\n";
}
if (dp[bm].first < pp.first || dp[bm].second == 0) dp[bm] = max(dp[bm], pp);
// if (dp[bm] < pp) cout << "Life is a lie\n";
if (pp.first == N) {
cout << "YES" << endl;
return 0;
}
//cout << bitset<5>(nbm) << " : " << bitset<5>(bm) << " - " << pp.first << " " << pp.second << endl;
}
}
}
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... |