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>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define bupol __builtin_popcount
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
using namespace std;
const int MAXN = 2e6+20;
const int LOG = 20;
const int MOD = 1e9+7;
const int SQRT = 520;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
int n, m;
int a[25], b[25];
bool dp[25][MAXN];
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i=1; i<=n; i++) cin >> a[i];
for(int i=0; i<m; i++) cin >> b[i];
vector <pii> vec;
vector <pii> v1, v2;
dp[0][0] = 1;
for(int i=1; i<=n; i++){
for(int j=0; j<(1<<m); j++){
if(dp[i-1][j]==0) continue;
vec.clear(); v1.clear(); v2.clear();
int num = (1<<m)-1-j; // bit yg bisa dipake
for(int k=0; k<m; k++){
if((num>>k) & 1) vec.pb({b[k], k});
}
//for(auto in : vec) cout << in << " vec\n";
int cnt = (int)vec.size(), bit = cnt/2;
for(int y=0; y<(1<<bit); y++){
int val = 0, idx = 0;
for(int x=0; x<bit; x++){
if((y>>x) & 1){
val += vec[x].fi; idx += (1<<vec[x].se);
}
}
v1.pb({val, idx});
}
bit = cnt-bit;
for(int y=0; y<(1<<bit); y++){
int val = 0, idx = 0;
for(int x=0; x<bit; x++){
if((y>>x) & 1){
val += vec[cnt/2+x].fi;
idx += (1<<vec[cnt/2+x].se);
}
}
v2.pb({val, idx});
}
sort(v1.begin(), v1.end());
sort(v2.rbegin(), v2.rend());
//for(auto in : v1) cout << in.fi << ' ' << in.se << " v1\n";
//for(auto in : v2) cout << in.fi << ' ' << in.se << " v2\n";
int idx = 0, siz = v2.size(), siz2 = v1.size();
vector <int> tem;
for(int i=0; i<siz2; ){
int te = v1[i].fi; tem.clear();
while(i<siz2 && v1[i].fi == te){
tem.pb(v1[i].se); i++;
}
// if(te==a[i]){
// dp[i][j + in.se] = 1;
// }
while(idx<siz && v2[idx].fi > a[i]-te) idx++;
while(idx<siz && v2[idx].fi == a[i]-te){
for(auto in : tem){
int num = j + in + v2[idx].se;
dp[i][num] = 1;
}
//cout<< i << ' ' << num << " num\n";
idx++;
}
}
}
}
bool ans = 0;
for(int i=0; i<(1<<m); i++) ans |= dp[n][i];
cout << (ans ? "YES\n" : "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... |