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 ll long long
const int N = 1e3 + 5, inf = 1e9, mod = 1e9 + 7;
int n, m;
int dp[21][(1 << 20)];
vector<int> musks[N];
vector<int> people, cash;
int main()
{
ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n >> m;
people.resize(n), cash.resize(m);
for (int i = 0; i < n; i++)
cin >> people[i];
for (int i = 0; i < m; i++)
cin >> cash[i];
for (int i = 0; i < (1 << m); i++)
{
int sum = 0;
for (int j = 0; j < m; j++)
if (i & (1 << j))
sum += cash[j];
if (sum <= 1000)
musks[sum].push_back(i);
}
for (auto &v : musks[people[0]])
dp[0][v] = 1;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < (1 << m); j++)
{
if (dp[i][j] == 0)
continue;
for (auto v : musks[people[i + 1]])
if ((j & v) == 0)
dp[i + 1][j | v] |= dp[i][j];
}
}
for (int i = 0; i < (1 << m); i++)
if (dp[n - 1][i])
return cout << "YES\n", 0;
cout << "NO\n";
}
/*
YES
*/
# | 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... |