//Author RufatM
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<vector<pii>> vvp;
typedef vector<bool> vb;
typedef vector<string> vs;
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define pf push_front
#define eb emplace_back
#define ff first
#define ss second
#define all(x) begin(x),end(x)
#define rall(x) rbegin(x),rend(x)
#define mt19937_64 mt_rand(chrono::steady_clock::now().time_since_epoch().count())
#define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
template<typename T> bool isPrime(T n){if(n<=1)return false;if(n<=3)return true;if(n%2==0 or n%3==0)return false;for(T i=5;i*i<=n;i+=6)if(n%i==0 or n%(i+2)==0)return false;return true;}
const int MOD=1e9+7;
const int INF=1000000007;
const ll LINF=1e18+7;
const int LOG=21;
const int MAXN=300005;
const int MAXM=200005;
int n,m;
int a[25],b[25];
bool used[25];
bool dfs(int idx){
if(idx==n){
return true;
}
for(int mask=1;mask<(1<<m);mask++){
int sum = 0;
bool ok = true;
for(int j=0;j<m;j++){
if(mask&(1<<j)){
if(used[j]){
ok = false;
}
sum += b[j];
}
}
if(!ok){
continue;
}
if(sum == a[idx]){
for(int j=0;j<m;j++){
if(mask&(1<<j)){
used[j] = true;
}
}
if(dfs(idx+1)){
return true;
}
for(int j=0;j<m;j++){
if(mask&(1<<j)){
used[j] = false;
}
}
}
}
return false;
}
signed main(){
fastio;
#ifndef ONLINE_JUDGE
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#endif
int t=1;
//cin >> t;
while(t--){
cin >> n >> m;
for(int i=0;i<n;i++){
cin >> a[i];
}
for(int i=0;i<m;i++){
cin >> b[i];
}
sort(a,a+n);
if(dfs(0)){
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... |