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;
#define what(a) cout << (#a) << " is " << (a) << '\n';
const int N = 20;
string s;
int n , m , a[N] , b[N];
pair<int , int> memo[1 << N];
bool visited[1 << N];
pair<int , int> dp(int mask) {
if (mask == 0) {
return make_pair(0 , 0);
}
if (visited[mask])
return memo[mask];
pair<int , int> res = make_pair(-1 , -1);
for (int i = 0; i < m; i++) if(mask & (1 << i)) {
auto prev = dp(mask - (1 << i));
if(prev.first == -1 || prev.second == -1) continue;
int pos = prev.first;
if(prev.second + b[i] < a[pos]) res = max(res , make_pair(prev.first , prev.second + b[i]));
if(prev.second + b[i] == a[pos]) res = max(res , make_pair(prev.first + 1 , 0));
}
visited[mask] = true;
memo[mask] = res;
if(res.first == n) {
cout << "YES";
exit(0);
}
return res;
}
void solve() {
cin >> n >> m;
for(int i = 0 ; i < n ; i++) cin >> a[i];
for(int i = 0 ; i < m ; i++) cin >> b[i];
dp((1 << m) - 1);
cout << "NO";
return;
}
int main() {
//freopen("bank.in" , "r" , stdin);
//freopen("bank.out" , "w" , stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
}
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... |