This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//don't copy pls)
/*TAAK ZDES NADO RECURSIU PISAT*/
//I'm not in the danger i am the DANGER
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
// #define int long long
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define sigma signed
using namespace std;
using namespace __gnu_pbds;
const int N = 1e3 + 5;
int mod = 1e9 + 7;
int n,m,a[N],b[N],dp[N][N],used[21][(1 << 20) + 1],sum;
vector <int> g[N];
int rec(int pos = 1 , int mask = 0){
if(pos == n + 1){
cout << "YES\n";
exit(0);
}
if(used[pos][mask]) return used[pos][mask];
int x = a[pos];
for(auto msk : g[x]){
if(used[pos][mask]) break;
if((msk & mask) != 0) continue;
used[pos][mask] = max(rec(pos + 1 , msk | mask) , used[pos][mask]);
}
return used[pos][mask];
}
void Gold(){
cin >> n >> m;
for(int i = 1 ; i <= n ; i++){
cin >> a[i];
}
for(int i = 1 ; i <= m ; i++){
cin >> b[i];
}
for(int i = 1 ; i <= n ; i++){
int x = a[i];
for(int mask = 0 ; mask < (1 << m) ; mask++){
int sum = 0;
for(int i = 1 ; i <= m ; i++){
if(((mask >> (i - 1)) & 1)){
sum += b[i];
}
}
if(sum == x){
g[a[i]].pb(mask);
}
}
}
if(n == 1){
int x = a[1];
for(int i = 1 ; i <= m ; i++) dp[i][0] = 1;
for(int i = 1 ; i <= m ; i++){
for(int j = 1 ; j <= x ; j++){
if(dp[i][j]) break;
dp[i][j] = max(dp[i][j] , dp[i - 1][j]);
if(j >= b[i]){
dp[i][j] = max(dp[i][j] , dp[i - 1][j - b[i]]);
}
}
}
for(int i = 1 ; i <= m ; i++){
if(dp[i][x]){
cout << "YES\n";
return;
}
}
cout << "NO\n";
return;
}
int ans = rec();
if(ans){
cout << "YES\n";
return;
}
cout << "NO";
}
sigma main(){
//freopen("txt.in","r",stdin);
//freopen("txt.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
srand(time(0));
int TT = 1;
// cin >> TT;
for(int i = 1 ; i <= TT ; i++){
//cout << "Case " << i << ": ";
Gold();
}
}
# | 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... |