This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int N, M;
int S[22], A[22], B[22];
bool dp[(1 << 20)][20];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
// freopen("cc.inp", "r", stdin);
//freopen("cc.out", "w", stdout);
cin >> N >> M;
for (int i = 0; i < N; ++i) {
cin >> A[i];
S[i] = S[i - 1] + A[i];
}
for (int i = 0; i < M; ++i) cin >> B[i];
dp[0][0] = true;
for (int msk = 0; msk < (1 << M); ++msk) {
int sum = 0;
for (int i = 0; i < M; ++i) if (msk >> i & 1)
sum += B[i];
for (int K = 0; K < N; ++K) if (dp[msk][K]) {
for (int i = 0; i < M; ++i) if (!(msk >> i & 1)) {
int nmsk = msk | (1 << i);
if (sum + B[i] <= S[K]) dp[nmsk][K] = true;
if (sum == S[K] && B[i] <= A[K + 1]) dp[nmsk][K + 1] = true;
}
}
}
for (int msk = 0; msk < (1 << M); ++msk) {
int sum = 0;
for (int i = 0; i < M; ++i) if (msk >> i & 1)
sum += B[i];
if (sum == S[N - 1] && dp[msk][N - 1]) {
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |