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("Ofast,unroll-loops")
#define bupol __builtin_popcount
#define int long long
#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;
const int INF = 1e18;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
int n, m;
int a[25], b[25];
int 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];
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;
int num = (1<<m)-1-j; // bit yg bisa dipake
vector <int> vec;
for(int k=0; k<m; k++){
if((num>>k) & 1) vec.pb(b[k]);
}
//for(auto in : vec) cout << in << " vec\n";
vector <pii> v1, v2;
int cnt = (int)vec.size(), bit = cnt/2;
for(int y=0; y<(1<<bit); y++){
int val = 0;
for(int x=0; x<bit; x++){
if((y>>x) & 1) val += vec[x];
}
v1.pb({val, y});
}
bit = cnt-bit;
for(int y=0; y<(1<<bit); y++){
int val = 0;
for(int x=0; x<bit; x++){
if((y>>x) & 1) val += vec[cnt/2+x];
}
v2.pb({val, y});
}
sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end());
//for(auto in : v1) cout << in.fi << ' ' << in.se << " v1\n";
//for(auto in : v2) cout << in.fi << ' ' << in.se << " v2\n";
for(auto in : v1){
int te = in.fi;
auto it = lower_bound(v2.begin(), v2.end(), pii(a[i]-te, -1));
while(it!=v2.end() && (*it).fi == a[i]-te){
int num = j + in.se + ((*it).se << (cnt/2));
dp[i][num] = 1;
//cout<< i << ' ' << num << " num\n";
it++;
}
}
}
}
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... |