#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pll pair<int, int>
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define endl '\n'
signed main(){
int n, m;cin>>n>>m;
vector<int> a(n), b(m);
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<m;i++)cin>>b[i];
vector<pair<int,int>> mem(1<<m, {0, 0});
//{max prefix of people paid, leftover}
mem[0]={0, 0};
for(int bm=1;bm<=(1<<m);bm++){
for(int i=0;i<m;i++){
if(! ((1<<i)&bm))continue;
int prv=bm ^ (1<<i);
// transition to mask 'prv' with ith bit off.
int sm=mem[prv].s + b[i];
int tar=a[mem[prv].f];
if(sm > tar){
//overshoots target guy, dont do anything
}
else if (sm == tar){
// just nice
mem[bm]=max(mem[bm], {mem[prv].f+1, 0});
}
else {
// accumulate
mem[bm]=max(mem[bm], {mem[prv].f, sm});
}
}
if (mem[bm].f == n){
cout<<"YES";
return 0;
}
}
cout<<"NO";
}
# | 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... |