This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 23;
int n, m, a[N], b[N];
vector<int> v[1005], bank, used(K), sums[1005], people, dp[1<<12];
bitset<(1<<20)> dpp[1<<11];
void solve(){
cin >> n >> m;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= m; ++i) cin >> b[i];
if(n>m){
cout << "NO";
return;
}
cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
for(int i = 1; i <= n; ++i) v[a[i]].pb(i);
for(int i = 1; i <= m; ++i){
if(!v[b[i]].empty()){
used[v[b[i]].back()] = 1;
v[b[i]].pop_back();
}else{
bank.pb(b[i]);
}
}
for(int i = 1; i <= n; ++i) if(!used[i]) people.pb(a[i]);
m = bank.size();
n = people.size();
if(m < 2*n){
cout << "NO";
return;
}
for(int mask = 0; mask < (1<<m); ++mask){
int sum = 0;
for(int j = 0; j < m; ++j) if((1<<j)&mask) sum += bank[j];
if(sum <= 1000) sums[sum].pb(mask);
}
dp[0].pb(0);
for(int mask = 1; mask < (1<<n); mask = mask * 2 + 1){
bool ok = 0;
for(int j = n - 1; j >= 0; --j){
if(mask&(1<<j)){
for(int submask: dp[(1<<j)^mask]){
for(int subbank: sums[people[j]]){
if(!(submask & subbank)){
ok = 1;
if(!dpp[mask][subbank|submask]){
dpp[mask][subbank|submask] = 1;
dp[mask].pb(subbank|submask);
}
}
}
}
break;
}
}
}
if(!dp[(1<<n)-1].empty()){
cout << "YES";
}else{
cout << "NO";
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
cout << '\n';
}
cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message (stderr)
bank.cpp: In function 'void solve()':
bank.cpp:56:14: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
56 | bool ok = 0;
| ^~
bank.cpp: In function 'int main()':
bank.cpp:87:16: warning: unused variable 'aa' [-Wunused-variable]
87 | int T = 1, aa;
| ^~
# | 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... |