#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define ff first
#define ss second
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ar array
const int MOD = 1e9 + 7,INF = 1e18, N = 2e5 + 5;
/*
5 2
1 2 2
2 3 2
3 4 2
4 5 2
*/
int n , m;
vector <int> a , b;
map <int,vector <int>> mp;
int f = 0;
vector <int> bad;
void ans(int i, int use){
if(bad[i]) return;
if(f == 1) return;
for(int j : mp[a[i]]){
if((j & use) == 0){
if(i == n - 1){
f = 1;
return;
}
ans(i + 1, use | j);
if(f == 1){
return;
}
}
}
bad[use] = 1;
}
void solve(){
cin >> n >> m;
a.resize(n);
b.resize(m);
bad.resize(1 << 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++){
int sum = 0;
for(int j = 0;j < m;j++){
if(1 & (i >> j)){
sum += b[j];
}
}
mp[sum].pb(i);
}
// cout<<"here"<<endl;
int use = 0;
ans(0 , use);
if(f){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int ti = 1;
while (ti--) {
solve();
}
}
# | 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... |