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;
const int N = 21;
const int M = 1e3 + 7;
int n, m;
int a[N], b[N];
int cnt[M];
 
bool rock(int x) {
    if (n==1 && a[x] == 0) 
        return 1;
    if (x == n)
        return 1;
    if (!a[x])
        return rock(x + 1);
    for (int i = a[x]; i > 0; i--) {
        if (cnt[i]) {
            cnt[i]--;
            a[x] -= i;
            if (rock(x) == 1)
                return 1;
            a[x] += i;
            cnt[i]++;
        }
    }
    return 0;
}               
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.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];
        cnt[b[i]]++;
    }
    if (n == 1) {
        int x = a[0];
        for (int ma = 1; ma <= (1 << m); ma++) {
            int sm = 0;
            for (int i = 0; i < m; i++) {
                if ((1 << i) & ma) {
                    sm += b[i];
                }
            }
            if (sm == x) {
                cout << "YES";
                exit(false);
            }
        }
        cout << "NO";
        exit(false);
    }
    cout << (rock(0) == 1 ? "YES" : "NO");
    return 0;
}       
| # | 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... |