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 <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int a[22];
vector<int> b;
int m;
int getsum(int mask)
{
int res = 0;
int ind = m - 1;
while (mask)
{
if (mask & 1)
res += b[ind];
mask >>= 1;
ind--;
}
return res;
}
int main()
{
int n;
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < m; i++)
{
int x;
cin >> x;
b.push_back(x);
}
if (n == 1)
{
for (int mask = 1; mask < (1 << m); mask++)
{
int sum = getsum(mask);
//cerr << sum << '\n';
if (sum == a[0])
{
cout << "YES\n";
return 0;
}
}
cout << "NO\n";
}
else
{
sort(b.begin(), b.end());
do {
bool ok = true;
int ind = 0;
for (int i = 0; i < n; i++)
{
int cur = 0;
while (cur < a[i] && ind < m)
{
cur += b[ind];
ind++;
}
if (a[i] != cur)
ok = false;
}
if (ok)
{
cout << "YES\n";
return 0;
}
} while (next_permutation(b.begin(), b.end()));
cout << "NO\n";
}
return 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... |