# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1087796 | quangminh412 | Bank (IZhO14_bank) | C++14 | 61 ms | 1116 KiB |
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;
/*
John Watson
https://codeforces.com/profile/quangminh98
Mua Code nhu mua Florentino !!
*/
#define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
const int maxn = 30;
int n, m;
int a[maxn], b[maxn];
void solve_1()
{
for (int mask = 0; mask < (1 << m); mask++)
{
int sum = 0;
for (int i = 0; i < m; i++)
if (mask >> i & 1)
sum += b[i];
if (sum == a[1])
{
cout << "YES\n";
return;
}
}
cout << "NO\n";
}
signed main()
{
if (fopen("test.inp", "r"))
{
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
faster();
cin >> n >> m;
int full = (1 << m);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= m; i++) cin >> b[i];
if (n == 1)
{
solve_1();
return 0;
}
vector<vector<int>> dp(n + 5, vector<int>(full + 5, 0));
// base case
for (int mask = 0; mask < full; mask++)
{
int sum = 0;
for (int i = 0; i < m; i++)
if (mask >> i & 1)
sum += b[i];
if (sum == a[1])
dp[1][mask] = 1;
}
//
for (int i = 2; i <= n; i++)
{
for (int mask1 = 0; mask1 < full; mask1++)
if (dp[i - 1][mask1])
{
for (int mask2 = 0; mask2 < full; mask2++)
if ((mask1 & mask2) == 0)
{
int sum = 0;
for (int j = 0; j < m; j++)
if (mask2 >> j & 1)
sum += b[j];
if (sum == a[i])
dp[i][mask1 | mask2] = 1;
}
}
}
int ans = 0;
for (int mask = 0; mask < full; mask++)
ans = max(ans, dp[n][mask]);
cout << (ans == 0 ? "NO\n" : "YES\n");
return 0;
}
Compilation message (stderr)
# | 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... |