#include <bits/stdc++.h>
#define ll long long
#define ent '\n'
#define pb push_back
#define all(x) x.begin(),x.end()
#define PRaim_bek_abi ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
const int N = 2e1+5;
const int MOD = 1e9+7;
int n , m , a[N] , b[N];
set<int> dp[1005][N];
multiset<int>s;
void get(int i , int x){
if (i == n + 1){
cout << "YES";
exit(0);
}
if (x == 0){
get(i + 1 , a[i + 1]);
return;
}
for (auto to : dp[x][i]){
auto it = s.find(to);
if (it == s.end()) continue;
s.erase(it);
get(i , x - to);
s.insert(to);
}
}
void easy(){
cin >> n >> m;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
for (int i = 1; i <= m; i++){
cin >> b[i];
s.insert(b[i]);
}
sort(b + 1 , b + 1 + m);
for (int k = 1; k <= n; k++){
dp[0][k].insert(0);
for (int i = 1; i <= m; i++){
for (int j = a[k]; j >= b[i]; j--){
if (dp[j - b[i]][k].size() == 0) continue;
dp[j][k].insert(b[i]);
}
}
if (dp[a[k]][k].size() == 0){
cout << "NO";
return;
}
}
get(1 , a[1]);
cout << "NO";
}
signed main(){
PRaim_bek_abi
int t=1;
//cin>>t;
while(t--) easy();
}
| # | 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... |