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;
#define lesgooo ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl '\n'
#define int long long
int a[25], b[25];
int dp[21][1 << 20];
int n, m;
bool solve(int lvl, int msk, int rem, int i);
bool solve(int i, int j)
{
if (i == n) return 1;
if (~dp[i][j]) return dp[i][j];
return dp[i][j] = solve(i, j, a[i], 0);
}
bool solve(int lvl, int msk, int rem, int i)
{
if (i == m)
{
if (rem == 0) return solve(lvl+1, msk);
return 0;
}
return ((msk | (1 << i)) != msk && rem-b[i] >= 0? solve(lvl, msk | (1 << i), rem-b[i], i+1): 0) || solve(lvl, msk, rem, i+1);
}
signed main()
{
lesgooo;
memset(dp, -1, sizeof dp);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];
sort(b, b+m);
reverse(b, b+m);
cout << (solve(0, 0)? "YES" : "NO");
return 0;
}
Compilation message (stderr)
bank.cpp: In function 'bool solve(long long int, long long int)':
bank.cpp:17:21: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
17 | return dp[i][j] = solve(i, j, a[i], 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... |