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 endl '\n'
#define int long long
#define fi first
#define se second
#define pb push_back
using namespace std;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
int a[n], b[m];
for (int i = 0; i < n; i++){
cin >> a[i];
}
for (int i = 0; i < m; i++){
cin >> b[i];
}
pair<int, int> dp[(1 << m)];
int visited[(1 << m)] = {0};
dp[0] = {0, 0};
visited[0] = 1;
vector<vector<int>> masks(20);
for (int i = 0; i < (1 << m); i++){
masks[__builtin_popcount(i)].push_back(i);
}
for (int i = 0; i <= m; i++){
for (int mask: masks[i]){
if (visited[mask] == 0) continue;
if (dp[mask].fi == n){
cout << "YES";
return 0;
}
int curperson = dp[mask].fi;
for (int j = 0; j < m; j++){
int newmask = mask | (1 << j);
if (mask == newmask) continue;
if (dp[mask].se + b[j] > a[curperson]) continue;
visited[newmask] = 1;
dp[newmask] = {curperson, dp[mask].se + b[j]};
if (a[curperson] == dp[mask].se + b[j]){
dp[newmask] = {curperson+1, 0};
}
}
}
}
cout << "NO";
}
# | 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... |