#include <bits/stdc++.h>
#define int long long
using namespace std;
const int sz = 25;
int n, m;
int a[sz], b[sz];
vector<bool> used(sz, false);
unordered_map<int,int> mp;
bool dfs(int idx){
if(idx == n) return true;
int need = a[idx];
for(int mask = 1; mask < (1 << m); mask++){
int s = 0, y = 0;
bool flag = true;
for(int i = 0; i < m; i++){
if(mask & (1<<i)){
if(used[i]){
flag=false; break;
}
s += b[i];
y |= (1<<i);
}
}
if(flag && s == need){
for(int i = 0; i < m; i++)
if(y & (1<<i)) used[i] = true;
if(dfs(idx+1)) return true;
for(int i = 0; i < m; i++){
if(y & (1<<i)) used[i] = false;
}
}
}
return false;
}
signed main(){
cin >> n >> m;
for(int i = 0; i < n; i++){
cin >> a[i];
mp[a[i]]++;
}
for(int i = 0; i < m; i++){
cin >> b[i];
}
sort(a, a+n);
if(dfs(0)) cout << "YES\n";
else cout << "NO\n";
}
# | 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... |