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>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 100003, mod = 1e9 + 7, inf = 1e9 + 7;
bool dp[21][(1 << 21)];
bool one_only[21][(1 << 21)];
int n, m;
void f(int i, int s){
one_only[i][s] = true;
for(int j = 0; j < m; ++j){
if(!(s & (1 << j))){
if(!one_only[i][s + (1 << j)]){
f(i, s + (1 << j));
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("movie.in", "r", stdin);
// freopen("movie.out", "w", stdout);
cin >> n >> m;
vector<int> vn(n), vm(m), sum((1 << m));
for(int i = 0; i < n; ++i){
cin >> vn[i];
}
for(int j = 0; j < m; ++j){
cin >> vm[j];
}
for(int s = 1; s < (1 << m); ++s){
for(int i = 0; i < m; ++i){
if((s & (1 << i))) sum[s] += vm[i];
}
}
for(int i = 0; i < n; ++i){
for(int s = 1; s < (1 << m); ++s){
if(sum[s] == vn[i]){
f(i, s);
}
}
}
// for(int i = 0; i < n; ++i){
// for(int s = 1; s < (1 << m); ++s){
// cout << i << ' ' << s << " " << one_only[i][s] << "\n";
// }
// }
for(int s = 1; s < (1 << m); ++s){
if(one_only[0][s]) dp[0][s] = true;
}
for(int i = 1; i < n; ++i){
for(int s = 1; s < (1 << m); ++s){
if(one_only[i][s]){
for(int s2 = 1; s2 < (1 << m); ++s2){
if((s & s2) != 0) continue;
if(dp[i - 1][s2]){
dp[i][s + s2] = true;
}
}
}
}
}
if(dp[n - 1][(1 << m) - 1]){
cout << "YES";
}else{
cout << "NO";
}
}
# | 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... |