#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 600005
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
ll T, n, a[N], t, m, b[N], pos[N], vis[N];
void solve(int x) {
if(x == n + 1) {
cout << "YES\n";
exit(0);
}
for(int j = 1; j <= a[x]; j++) {
pos[j] = 0;
}
pos[0] = 1;
for(int j = 1; j <= m; j++) {
if(vis[j]) continue;
for(int k = 1000; k >= 0; k--) {
if(pos[k] != 0 && pos[k + b[j]] == 0) {
pos[k + b[j]] = j;
}
}
}
if(!pos[a[x]]) return;
while(a[x] > 0) {
vis[pos[a[x]]] = 1;
a[x] -= b[pos[a[x]]];
}
solve(x + 1);
}
int main () {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
for(int i = 1; i <= m; i++) {
cin >> b[i];
}
solve(1);
cout << "NO\n";
}
# | 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... |