Submission #497312

#TimeUsernameProblemLanguageResultExecution timeMemory
497312tphuc2908Bank (IZhO14_bank)C++14
100 / 100
109 ms8644 KiB
#include <bits/stdc++.h>

using namespace std;
#define rep(i,x,y) for(int i = x; i <= y; ++i)
#define repi(i,x,y) for(int i = x; i >= y; --i)
#define fi first
#define se second
#define ci(x) int x; cin >> x
#define cii(x,y) int x, y; cin >> x >> y
#define ciii(x,y,z) int x,y,z; cin >> x >> y >> z
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
//#define int long long
typedef long long ll;
const int N = 5e5 + 5;
const int mod = 1e9 + 7;
const int mod1 = 1e9 + 9;
const int pi = 23;
const int inf = 1e9 + 5;

void read_file(){
    freopen("text.inp", "r", stdin);
//    freopen("SEARCHING.INP" , "r", stdin);
//    freopen("SEARCHING.OUT", "w", stdout);
}

/*
    dp[bit][i] is the subset of M so that you can pay for first i members
    enumerate subset of subset in 3^n for sub 1,2,3
    for sub 4, use pair<int,int> dp[bit] is the maximum numbers of costumers you can pay and the
    least amount left to pay the next costumer.
*/

int n, m;
int a[N], b[N];
int dp[(1<<20) + 5][25];

void inp(){
    cin >> n >> m;
    rep(i,1,n) cin >> a[i];
    rep(i,0,m-1) cin >> b[i];
}

int sum[(1<<20) + 5];

void process(){
    rep(i,0,(1<<m)-1)
        for(int j = 0; j < m; ++j)
            if((i>>j)&1)
                sum[i] += b[j];
    if(n==1){
        bool ok = 0;
        rep(i,0,(1<<m)-1)
            if(sum[i]==a[1]){
                ok = 1;
                break;
            }
        if(ok) cout << "YES";
        else cout << "NO";
        return;
    }
    dp[0][0] = 1;
    rep(i,1,(1<<m)-1){
        for(int t = i; t; t = (t-1)&i){
            rep(j,1,n){
                int s = sum[t];
                if(s==a[j] && dp[i^t][j-1]){
                    dp[i][j] = 1;
                }
            }
        }
    }
    bool ok = 0;
    rep(i,0,(1<<m)-1)
        if(dp[i][n]){
            ok = 1;
            break;
        }
    if(ok) cout << "YES";
    else cout << "NO";
}

pair<int,int> f[(1<<20) + 5];

void sub4(){
    f[0] = {0, a[1]};
    rep(i,1,(1<<m)-1)
        f[i].se = inf;
    for(int i = 1; i < (1<<m); ++i){
        for(int j = 0; j < m; ++j){
            if((i>>j)&1){
                pair<int,int> temp = f[i^(1<<j)];
                if(temp.se==b[j])
                    temp = {temp.fi + 1, a[temp.fi + 2]};
                else if(temp.se > b[j])
                        temp.se-=b[j];
                if(temp.fi > f[i].fi)
                    f[i] = temp;
                else if(temp.fi == f[i].fi && temp.se < f[i].se)
                        f[i] = temp;
            }
        }
        if(f[i].fi==n){
            cout << "YES\n";
            return;
        }
    }
    cout << "NO\n";
}

int main()
{
//    read_file();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    inp();
    if(n==1 || m <= 14)
        process();
    else
        sub4();
    return 0;
}

Compilation message (stderr)

bank.cpp: In function 'void read_file()':
bank.cpp:23:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |     freopen("text.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...