#include <bits/stdc++.h>
#define SPED \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"
#define fi first
#define se second
#define lint long long
const lint INF = 1e15;
using namespace std;
int m, n;
lint a[25], b[25];
pair<int, lint> dp[(1 << 20) + 5]; // người hiện tại lớn nhất có thể tuần tự từ 0 đến m - 1 đang trả, số tiền còn lại
int main()
{
SPED;
cin >> m >> n;
for (int i = 0; i < m; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
cin >> b[i];
for (int mask = 1; mask < (1 << n); mask++)
{
lint maxi = -1, temp = -1;
for (int i = 0; i < n; i++)
{
if (mask & (1 << i))
{
auto [paying, left] = dp[mask ^ (1 << i)];
if (left + b[i] > a[paying])
{
if (paying + 1 > maxi)
{
maxi = paying + 1;
temp = 0;
}
}
else
{
if (paying > maxi)
{
maxi = paying;
temp = left + b[i];
}
else if (paying == maxi)
temp = max(temp, left + b[i]);
}
}
dp[mask] = {maxi, temp};
}
}
if (dp[(1 << n) - 1].fi == m - 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
# | 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... |