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>
#define ff first
#define ss second
#define sz size()
#define pb push_back
using namespace std;
typedef long long ll;
const int N = 21;
// const int mod = 1e9+7;
// ll bigmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
void solve();
int n, m, a[N], b[N], p[N], sum;
bool dp[1 << N];
int main() {
// ios::sync_with_stdio(false);
// cin.tie(0), cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// clock_t tStart = clock();
// printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
cin >> n >> m;
// dp[i] - da i daky 1 duran elementlary ulanyp: pos - 1 cenli polny toleyan, i-nji tolensin,yada i-nji azrak tolensin yaly etyan.
for (int i = 1; i <= n; ++i) {
cin >> a[i];
p[i] = p[i - 1] + a[i]; // partial sum etyas
}
for (int i = 0; i < m; ++i) {
cin >> b[i];
}
dp[0] = 1;
for (int i = 0; i < (1 << m); ++i) {
sum = 0;
for (int j = 0; j < m; ++j) {
if (i>>j&1) {
sum += b[j]; // 1 bolan elementlaryn sum-ny alyan.
}
}
auto pos = lower_bound (p + 1, p + n + 1, sum) - p; // barde pos tapyan.
if (pos == n + 1) { // sum uly bolanson dp[i] edip bolonok.
continue;
}
for (int j = 0; j < m; ++j) {
if ((i>>j&1) and (b[j] <= sum - p[pos - 1])) dp[i] |= dp[i ^ (1 << j)];
}
if ((p[n] == sum) and (dp[i])) {
cout << "YES" << endl;
return 0;
}
}
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... |