This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// uggjaa ped
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(lcv, lower, upper) for (ll lcv = lower; lcv < upper; lcv++)
bool help(vector<vector<ll>> dp, ll i, ll mask, ll a[], ll left, ll b[], ll m, ll n)
{
if (mask == 0)
return false;
if (dp[i][mask] != -1)
return dp[i][mask];
bool ans = false;
loop(j, 0, m)
{
if (((1 << j) & mask))
{
if (left == b[j])
{
if (i == n - 1)
return dp[i][mask] = true;
ans = ans or help(dp, i + 1, mask ^ (1 << j), a, a[i + 1], b, m, n);
}
else if (left > b[j])
{
ans = ans or help(dp, i, mask ^ (1 << j), a, left - b[j], b, m, n);
}
}
}
return dp[i][mask] = ans;
}
void solve()
{
ll n, m;
cin >> n >> m;
ll a[n], b[m];
loop(i, 0, n) cin >> a[i];
loop(i, 0, m) cin >> b[i];
vector<vector<ll>> dp(n, vector<ll>(1 << m, -1));
cout << (help(dp, 0, (1 << m) - 1, a, a[0], b, m, n) ? "YES" : "NO") << endl;
}
int main()
{
solve();
return 0;
}
Compilation message (stderr)
bank.cpp: In function 'bool help(std::vector<std::vector<long long int> >, long long int, long long int, long long int*, long long int, long long int*, long long int, long long int)':
bank.cpp:22:40: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
22 | return dp[i][mask] = true;
bank.cpp:31:24: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
31 | return dp[i][mask] = ans;
# | 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... |