This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
#define MOD 1000000007LL
int N, M;
int a[20], b[20];
pair<int, int> dp[1<<20];
int main() {
    //freopen("feast.in", "r", stdin);
    //freopen("feast.out", "w", stdout);
    //ios_base::sync_with_stdio(false);
    //cin.tie(0);
    
    cin >> N >> M;
    for (int i = 0; i < N; i++) cin >> a[i];
    for (int i = 0; i < M; i++) cin >> b[i];
    dp[0] = {0, 0};
    for (int bm = 1; bm < (1<<M); bm++) {
        dp[bm] = {0, 0};
        for (int i = 0; i < M; i++) {
            if (bm & (1<<i)) {
                int nbm = bm^(1<<i);
                pair<int, int> pp = dp[nbm];
                if (pp.second + b[i] < a[pp.first]) pp.second += b[i];
                else if (pp.second+b[i] == a[pp.first]) pp.first++, pp.second = 0;
                dp[bm] = max(dp[bm], pp);
                if (pp.first == N) {
                    cout << "YES" << endl;
                    return 0;
                }
                
                //cout << bitset<5>(nbm) << " : " << bitset<5>(bm) << " - " << pp.first << " " << pp.second << endl;
            }
        }
    }
    cout << "NO" << endl;
}
| # | 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... |