이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//Day 1 Problem B: bank
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
int n, m, a[20], b[20];
pair<int, int> dp[1<<20]; // <i, j> = <done with first i people, payed (i+1)th person j>
bool pos=0;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i=0; i<n; ++i) cin >> a[i];
for (int i=0; i<m; ++i) cin >> b[i];
for (int i=1; i<(1<<m); ++i) dp[i].first=-1;
for (int mask=0; mask<(1<<m); ++mask) {
if (dp[mask]==make_pair(n, 0)) {
pos=1;
break;
}
if (dp[mask].first==-1) continue;
int ind=dp[mask].first, payed=dp[mask].second; //index of person we are on (0-indexed), amount we have already payed
assert(ind<n&&payed<a[ind]);
for (int i=0; i<m; ++i) if (!(mask&(1<<i))) {
if (payed+b[i]>a[ind]) continue;
if (payed+b[i]==a[ind]) dp[mask|(1<<i)]={ind+1, 0};
else dp[mask|(1<<i)]={ind, payed+b[i]};
}
}
cout << (pos?"YES":"NO");
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
*/
# | 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... |