#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// #pragma GCC target ("avx2")
// #pragma GCC optimize ("O3")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC optimize("fast-math")
#define ll long long
#define ld long double
#define vi vector<ll>
#define endl "\n"
#define pr pair<ll, ll>
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
const int mod = 1e9+7;
void _p(ll a){cout<<a<<endl;}
void _p(string a){cout<<a<<endl;}
void _p(ld a) {cout<<a<<endl;}
template <class T>
void _p(vector<T> a){for(T val:a)cout<<val<<" ";cout<<endl;}
#define debug(x) cout<<#x<<" -> ";_p(x)
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update > ordered_set;
vector<pr> move8 = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
vector<pr> move4 = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<pr> move2 = {{0, 1}, {1, 0}};
void solution();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solution();
return 0;
}
ll a[21], bankNotes[20];
bool dp[1<<20][21];
void solution() {
ll n, m; cin >> n >> m;
for (int i = 1; i <= n; i++) {cin >>a[i]; a[i] += a[i-1];}
for (int i = 0; i < m; i++) cin >> bankNotes[i];
for (int i = 0; i < (1 << 20); i++) dp[i][0] = 1;
for (int mask = 0; mask < (1 << m); mask++) {
for (int i = 1; i <= n; i++) {
ll s = 0;
for (int j = 0; j < m; j++) {
if (mask & (1 << j)) s += bankNotes[j];
}
if (s == a[i] && dp[mask][i-1]) {
dp[mask][i] = 1;
}
}
for (int j = 0; j < m; j++) {
if ((mask & (1 << j))) continue;
for (int i = 1; i <= n; i++) {
dp[mask ^ (1 << j)][i] = dp[mask ^ (1 << j)][i] | dp[mask][i];
}
}
}
if (dp[(1 << m)-1][n]) 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... |