Submission #1293189

#TimeUsernameProblemLanguageResultExecution timeMemory
1293189trandaihao5555Bank (IZhO14_bank)C++20
100 / 100
109 ms19500 KiB
#include <bits/stdc++.h>

#define int long long

#define debug     cout << "ok\n";
#define SQR(x)    (1LL * ((x) * (x)))
#define MASK(i)   (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi        first
#define se        second
#define pb        push_back

#define mp make_pair
#define pii pair<int,int>
#define pli pair<ll,int>
#define vi vector<int>

#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned int ui;

using namespace std;

const int M = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFLL = (ll)2e18 + 7LL;
const ld PI = acos(-1);

const int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};

template<class _, class __>
    bool minimize(_ &x, const __ y){
        if(x > y){
            x = y;
            return true;
        } else return false;
    }
template<class _, class __>
    bool maximize(_ &x, const __ y){
        if(x < y){
            x = y;
            return true;
        } else return false;
    }

template<class _,class __>
    void Add(_ &x, const __ y) {
        x += y;
        if (x >= M) {
            x -= M;
        }
        return;
    }

template<class _,class __>
    void Diff(_ &x, const __ y) {
        x -= y;
        if (x < 0) {
            x += M;
        }
        return;
    }

//--------------------------------------------------------------

const int Log = 21;
const int MaxN = 27;

int n,m,a[MaxN],b[MaxN],sum[MASK(Log) + 7],bac[MASK(Log) + 7];
bool vis[MASK(Log) + 7];

void sol() {
    cin >> n >> m;
    a[0] = 0;
    for (int i=1;i<=n;i++) cin >> a[i];
    for (int i=1;i<=n;i++) a[i] += a[i-1];
    for (int i=0;i<m;i++) cin >> b[i];
    for (int i=0;i<MASK(m);i++) {
        for (int j=0;j<m;j++) if (BIT(i,j)) sum[i] += b[j];
        bac[i] = 0;
        while (bac[i] <= n && a[bac[i]] < sum[i]) bac[i]++;
//        bitset<6> tmp = i;
//        cout << tmp << ' ' << sum[i] << ' ' << bac[i] << '\n';
    }
    queue<int> q;
    q.push(0);
    vis[0] = true;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        if (sum[u] == a[n]) {
            cout << "YES";
            return;
        }
        for (int i=0;i<m;i++) {
            int v = u | MASK(i);
            if (vis[v]) continue;
            if (bac[v] > n || bac[v] > bac[u] + 1 || (bac[u] != bac[v] && sum[u] != a[bac[u]])) continue;
            vis[v] = true;
            q.push(v);
        }
    }
    cout << "NO";
}

signed main() {
//    freopen("test.inp","r",stdin);
//    freopen("test.out","w",stdout);
    FAST
    int t=1;
//    cin >> t;
    while (t--) sol();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...