#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 = 1e5+5;
const int MOD = 1e9+7;
int n , m , a[N] , b[N];
set<int> dp[N];
vector<vector<int>> v[N];
void rec(int x , vector<int> g , int i){
if (x == 0){
v[i].pb(g);
return;
}
for (auto to : dp[x]){
g.pb(to);
rec(x - to , g , i);
g.pop_back();
}
}
void get(int i , multiset<int>s){
if (i == n + 1){
cout << "YES";
exit(0);
}
for (auto g : v[i]){
multiset<int>nw = s;
bool ok = 0;
for (auto to : g){
auto it = nw.find(to);
if (it != nw.end()) nw.erase(it);
else {
ok = 1;
break;
}
}
if (ok) continue;
get(i + 1 , nw);
}
}
void easy(){
cin >> n >> m;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
multiset<int> st;
for (int i = 1; i <= m; i++){
cin >> b[i];
st.insert(b[i]);
}
dp[0].insert(0);
for (int k = 1; k <= n; k++){
for (int j = 1; j <= 1000; j++){
dp[j].clear();
}
for (int i = 1; i <= m; i++){
for (int j = a[k]; j >= b[i]; j--){
if (dp[j - b[i]].size() == 0) continue;
dp[j].insert(b[i]);
}
}
if (dp[a[k]].size() == 0){
cout << "NO";
return;
}
rec(a[k] , {} , k);
}
get(1 , st);
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... |