This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/// PhongVan cbhk64
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fo(i, l, r) for(int i = l; i <= r; i++)
#define foi(i, l, r) for(int i = l; i >= r; i--)
#define pii pair<int, int>
#define mx(x, y) max(x, y)
#define fi first
#define se second
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#define ll long long
#define pob pop_back
#define all(x) x.begin(),x.end()
#define vii vector<int>
#define int long long
#define getbit(i, j) (i >> j) & 1
#define offbit(i, j) (1 << j) ^ i
#define onbit(i, j) (1 << j) i
#define built(mask) __builtin_popcountll(mask)
#define len(s) (int)((s).size())
#define iii pair<int,pair<int, int> >
#define fillcharval(a) memset(a, -0x3f, sizeof(a));
#define fillchar(a,x) memset(a, x, sizeof (a))
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const int N = 1e6 + 6;
const int mod = 1e9 + 7;
const int base = 31;
const int inf = 1e9;
void add(ll &x, const ll y){
x+= y;
if(x>=mod) x-= mod;
}
int n, m;
int a[20];
int b[20];
pii dp[1<<20];
pii Max(pii x, pii y){
if(x.fi > y.fi) return x;
if(x.fi < y.fi) return y;
return {x.fi, max(x.se, y.se)};
}
signed main()
{
faster
// in("task.inp");
// out("task.out");
cin >> n >> m;
fo(i, 0, n - 1) cin >> a[i];
fo(i, 0, m - 1) cin >> b[i];
dp[0] = {0, 0};
fo(mask, 1, (1<<m) - 1){
fo(i, 0, m - 1){
if(getbit(mask, i)){
pii pre_mask = dp[offbit(mask, i)];
if(pre_mask.se + b[i] < a[pre_mask.fi]){
dp[mask] = Max(dp[mask], {pre_mask.fi, pre_mask.se + b[i]});
// cout << mask << "\n";
// cout << dp[mask].fi << " " << dp[mask].se << "\n";
// cout << pre_mask.fi << " " << pre_mask.se + b[i] << "\n";
}
else if(pre_mask.se + b[i] == a[pre_mask.fi]) dp[mask] = Max(dp[mask], {pre_mask.fi + 1, 0});
}
}
if(dp[mask].fi == n) return cout << "YES", 0;
}
cout << "NO";
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... |