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 = 25;
// 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];
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;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= m; ++i) {
cin >> b[i];
}
if (n == 1) {
int sum;
for (int i = 1; i <= m; ++i) sum += b[i];
vector <int> dp(sum+1, 0);
dp[0] = 1;
for (int i = 1; i <= m; ++i) {
for (int j = sum - b[i]; j >= 0; j--) {
if (dp[j]) {
dp[j + b[i]] = 1;
}
}
}
if (dp[a[1]]&1) {
cout << "YES\n";
return 0;
}
cout << "NO\n";
return 0;
}
sort (b + 1, b + m + 1);
do {
int pos = 1;
int sum = 0;
int bolonok = 0;
for (int i = 1; i <= m; ++i) {
if (pos == n + 1) break;
if (sum < a[pos]) {
sum += b[i];
}
else if (sum > a[pos]) {
bolonok = 1;
break;
}
else {
if (pos == n) break;
pos++;
sum = 0;
sum += b[i];
}
}
if (pos <= n) {
if (sum == a[pos]) pos++;
}
if (bolonok or pos != (n + 1)) {
continue;
}
return cout << "YES\n", 0;
}while (next_permutation(b + 1, b + m + 1));
cout << "NO\n";
}
/*
██╗░░██╗░█████╗░██╗░░░░░██╗░░░██╗███╗░░░███╗██████╗░░█████╗░░█████╗░███████╗
██║░░██║██╔══██╗██║░░░░░╚██╗░██╔╝████╗░████║╚════██╗██╔══██╗██╔══██╗╚════██║
███████║███████║██║░░░░░░╚████╔╝░██╔████╔██║░░███╔═╝██║░░██║██║░░██║░░░░██╔╝
██╔══██║██╔══██║██║░░░░░░░╚██╔╝░░██║╚██╔╝██║██╔══╝░░██║░░██║██║░░██║░░░██╔╝░
██║░░██║██║░░██║███████╗░░░██║░░░██║░╚═╝░██║███████╗╚█████╔╝╚█████╔╝░░██╔╝░░
╚═╝░░╚═╝╚═╝░░╚═╝╚══════╝░░░╚═╝░░░╚═╝░░░░░╚═╝╚══════╝░╚════╝░░╚════╝░░░╚═╝░░░
*/
Compilation message (stderr)
bank.cpp: In function 'int main()':
bank.cpp:34:13: warning: 'sum' may be used uninitialized in this function [-Wmaybe-uninitialized]
34 | for (int j = sum - b[i]; j >= 0; j--) {
| ^
# | 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... |