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>
#pragma GCC optimize("03,unroll-loops")
#pragma GCC target("avx2,bmi2,bmi,lzcnt,popcnt")
#define endl "\n"
#define rs resize
#define pb push_back
#define gcd __gcd
using namespace std;
vector<vector<int>> vv;
int 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(22);
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].first == n){
cout << "YES";
return 0;
}
int curperson = dp[mask].first;
for (int j = 0; j < m; j++){
int newmask = mask | (1 << j);
if (mask == newmask) continue;
if (dp[mask].second + b[j] > a[curperson]) continue;
visited[newmask] = 1;
dp[newmask] = {curperson, dp[mask].second + b[j]};
if (a[curperson] == dp[mask].second + b[j]){
dp[newmask] = {curperson+1, 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... |