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;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using indexedset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
pair<int, int> dp[1<<20];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
int a[n];
int b[m];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
for (int i = 0; i < (1<<m); i++){
dp[i] = {-1, 0};
}
dp[0].first = 0;
for (int i = 0; i < (1<<m); i++){
for (int j = 0; j < m; j++){
if (dp[i].first == n){
cout << "YES" << endl;
return 0;
}
if (dp[i].first == -1) continue;
if (i&(1<<j)) continue;
if (dp[i^(1<<j)].first != -1) continue;
if ((dp[i].second+b[j]) <= a[dp[i].first]){
dp[i^(1<<j)].first = dp[i].first;
dp[i^(1<<j)].second = dp[i].second+b[j];
if (dp[i^(1<<j)].second == a[dp[i].first]){
dp[i^(1<<j)].first++;
dp[i^(1<<j)].second = 0;
}
}
}
}
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... |