Submission #1141298

#TimeUsernameProblemLanguageResultExecution timeMemory
1141298Bilal_CoderBank (IZhO14_bank)C++20
0 / 100
5 ms324 KiB
#include <iostream>
#include <vector>
#include <bitset>
using namespace std;

bool canPayAllSalaries(int N, int M, vector<int>& salaries, vector<int>& banknotes) {
    vector<int> can[N]; // Возможные битмаски для каждого человека

    // Предрасчет битмасок для каждого человека
    for (int i = 0; i < N; ++i) {
        int salary = salaries[i];
        int totalSubsets = (1 << M); // 2^M
        for (int mask = 0; mask < totalSubsets; ++mask) {
            int sum = 0;
            for (int j = 0; j < M; ++j) {
                if (mask & (1 << j)) sum += banknotes[j];
            }
            if (sum == salary) {
                can[i].push_back(mask);
            }
        }
    }

    // Динамика
    vector<bool> dp(1 << M, false); // dp[mask]: можно ли покрыть маской зарплаты первых i человек
    dp[0] = true;

    for (int i = 0; i < N; ++i) {
        vector<bool> nextDp(1 << M, false);
        for (int mask = 0; mask < (1 << M); ++mask) {
            if (!dp[mask]) continue; // Если текущий mask недостижим, пропускаем
            for (int possibleMask : can[i]) {
                if ((mask & possibleMask) == possibleMask) {
                    nextDp[mask ^ possibleMask] = true; // Обновляем следующую маску
                }
            }
        }
        dp = nextDp; // Переходим к следующему человеку
    }

    // Если для полной маски возможно покрыть все зарплаты, ответ "YES"
    return dp[(1 << M) - 1];
}

int main() {
    int N, M;
    cin >> N >> M;

    vector<int> salaries(N), banknotes(M);
    for (int i = 0; i < N; ++i) cin >> salaries[i];
    for (int i = 0; i < M; ++i) cin >> banknotes[i];

    if (canPayAllSalaries(N, M, salaries, banknotes)) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

    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...