#include <bits/stdc++.h>
#define ll unsigned long long
#define pii pair<ll, ll>
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define ub(c, x) distance((c).begin(),lower_bound(c.begin(),c.end(), (x)))
#define ff first
#define ss second
#define N 200015
using namespace std;
const int inf = 1e9;
ll gcd(ll a, ll b){
    if(b == 0) return 1;
    return gcd(b, a % b);
}
const ll INF = 1e18;
vector<vector<pair<ll,ll>>> g;
ll n, m, x;
vector<ll> djikstras(ll s, ll n){
    vector<ll> dist(n, INF);
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll,ll>>> pq;
    vector<bool> vist(n, false);
    dist[s] = 0;
    pq.push({dist[s], s});
    
    while(!pq.empty()){
        ll a = pq.top().second; 
        pq.pop();
        if(vist[a]) continue;
        vist[a] = true;
        for(auto [b , w]: g[a]){
            if(dist[a] + w < dist[b]){
                dist[b] = dist[a] + w;
                pq.push({dist[b], b});
            }
        }
    }
    return dist;
}
pair<int, int> dp[5000000];
void F(){
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(m);
    
    memset(dp, -1, sizeof dp);
    dp[0] = {0, 0};
    
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int j = 0; j < m; j++) cin >> b[j];
    
    for(int mask = 0; mask < (1 << m); mask++){
        for(int last = 0; last < m; last++){
            if(!(mask & (1 << last))) continue;
            int pre = mask ^ (1 << last);
            if(dp[pre].ff == -1) continue;
            
            int rem = dp[pre].ff + b[last];
            int need = a[dp[pre].ff];
            
            if(rem < need) dp[mask] = {dp[pre].ff, rem};
            if(rem == need) dp[mask] = {dp[pre].ff + 1, 0};
        }
        if(dp[mask].ff == n){
            cout << "YES"; return;
        }
    }
    cout << "NO";
}
int main() {
    int tc = 1;
    //cin >> tc;
    while(tc--) F();
}
| # | 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... |