Submission #1308732

#TimeUsernameProblemLanguageResultExecution timeMemory
1308732tredsused70Bank (IZhO14_bank)C++20
100 / 100
142 ms5564 KiB
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
//#pragma GCC optimize("trapv")
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define mpr make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int nmax = 20, mod = 1000000007, inf = 2000010000, key = 200003, lg = 20, block = 300;
const ll infll = 4000000000000000000;
const ld eps = 1e-9;

int targets[nmax], banknotes[nmax];
bool dp[1 << nmax];
int sum[1 << nmax];

void solve()
{
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> targets[i];
    targets[n] = -1;
    for(int i = 0; i < m; i++)
        cin >> banknotes[i];

    for(int i = 1; i < (1 << m); i++) {
        for(int j = 0; j < m; j++) {
            if((i >> j) & 1)
                sum[i] += banknotes[j];
        }
    }
    int target = 0;
    dp[0] = 1;
    for(int i = 0; i < n; i++) {
        target += targets[i];
        for(int j = 1; j < (1 << m); j++) {
            if(sum[j] > target - targets[i] && sum[j] <= target && dp[j] == 0) {
                for(int k = 0; k < m; k++) {
                    if((j >> k) & 1)
                        dp[j] |= dp[j ^ (1 << k)];
                }
            }
        }
        for(int j = 0; j < (1 << m); j++)
            dp[j] &= (sum[j] == target);
    }
    for(int j = 0; j < (1 << m); j++)
        if(dp[j]) {
            cout << "YES\n";
            return ;
        }
    cout << "NO\n";
    return ;
}

int main()
{
    //freopen("movie.in","r",stdin);
    //freopen("movie.out","w",stdout);
    ios_base::sync_with_stdio(0);cin.tie(0);
    srand(8713);
    //init();
    int t = 1;
    //cin >> t;
    //int t_ = t;
    //t = rdi();
    while(t--) {
        //cout << "Case #" << t_ - t << ": ";
        solve();
    }
    //flush();
    return 0;
}

/*
20 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...