제출 #1303807

#제출 시각아이디문제언어결과실행 시간메모리
1303807peanut은행 (IZhO14_bank)C++20
71 / 100
1095 ms1360 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define pb push_back
#define mp make_pair

vector<int> dx = {0, 0, 1, -1};
vector<int> dy = {1, -1, 0, 0};

const int maxn = 20 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;

int salary[maxn], notes[maxn];
bool dp[1 << 20];
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    //freopen(".INP", "r", stdin);
    //freopen(".OUT", "w", stdout);
    int n, m; cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> salary[i];
    for (int i = 0; i < m; ++i) cin >> notes[i];

    vector<vector<int>> valid(n);
    for (int i = 0; i < (1 << m); ++i)
    {
        int sum = 0;
        for (int j = 0; j < m; ++j) if (i >> j & 1) sum += notes[j];
        for (int j = 0; j < n; ++j) if (sum == salary[j]) valid[j].pb(i);
    }

    if (n == 1)
    {
        if (sz(valid[0])) cout << "YES";
        else cout << "NO";
        return 0;
    }
    dp[0] = true;
    for (int i = 0; i < n; ++i)
    {
        for (int j = (1 << m) - 1; j >= 0; --j)
        {
            dp[j] = false;
            for (auto k : valid[i]) {
              if ((k & j) == k) {
                dp[j] |= dp[j ^ k];
                if (dp[j]) break;
              }
            }
        }
    }

    for (int i = 0; i < (1 << m); ++i)
    {
        if (dp[i])
        {
            cout << "YES";
            return 0;
        }
    }
    cout << "NO";
    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...