Submission #886973

#TimeUsernameProblemLanguageResultExecution timeMemory
886973Amirreza_Fakhri은행 (IZhO14_bank)C++17
100 / 100
89 ms8792 KiB
// In the name oF the God 
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
using namespace std;

const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 20;

int n, m, a[1000], b[1000];
pii dp[1<<maxn];

int32_t main() {
    ios::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];
    for (int msk = 0; msk < (1<<m); msk++) dp[msk].F = -1;
    dp[0].F = 0;
    for (int msk = 0; msk < (1<<m); msk++) {
        if (dp[msk].F == -1) continue;
        if (dp[msk].F == n) {
            cout << "YES\n"; return 0;
        }
        for (int i = 0; i < m; i++) {
            if (msk&(1<<i)) continue;
            if (dp[msk].S+b[i] == a[dp[msk].F]) dp[msk+(1<<i)] = mp(dp[msk].F+1, 0);
            else if (dp[msk].S+b[i] < a[dp[msk].F]) dp[msk+(1<<i)] = mp(dp[msk].F, dp[msk].S+b[i]);
        }
    }
    cout << "NO\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...