#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
using namespace std;
#define F first
#define S second
typedef long long ll;
#define pii pair <int, int>
#define pll pair <ll, ll>
typedef long double ld;
const ll N = 3 * 1e5 + 100, M = 500 + 10, len = 315, inf = 1e18;
const ll mod = 998244353;
ll um(ll a, ll b){
return (1LL * a * b) % mod;
}
ll subr(ll a, ll b){
return ((1LL * a - b) % mod + mod) % mod;
}
ll bp(ll x, ll step){
ll res = 1;
while(step){
if(step & 1) res = um(res, x);
x = um(x, x);
step /= 2;
}
return res;
}
ll inv(ll x){
return bp(x, mod - 2);
}
int a[N], b[N], n, m, cnt[N];
void subtask1(){
bool can = false;
for(int mask = 0; mask < (1 << m); mask++){
int cur = 0;
for(int dig = 0; dig < m; dig++){
if((mask & (1 << dig)) > 0) cur += b[dig];
}
if(cur == a[0]) can = true;
}
if(can) cout << "YES";
else cout << "NO";
}
void subtask2(){
sort(b, b + m);
bool tot = false;
do {
int from = 0, sm = 0;
for(int i = 0; i < m; i++){
sm += b[i];
if(sm == a[from]){
from++;
sm = 0;
}
if(from == n){
tot = true;
break;
}
}
} while(next_permutation(b, b + m) == 1);
if(tot) cout << "YES";
else cout << "NO";
}
void subtask3(){
for(int i = 0; i < n; i++){
cnt[a[i]]++;
}
int nx = n, mx = m;
vector <int> vn, vm;
for(int i = 0; i < m; i++){
if(cnt[b[i]] > 0){
cnt[b[i]]--;
nx--;
mx--;
} else vm.pb(b[i]);
}
for(int i = 0; i < n; i++){
if(cnt[a[i]] > 0){
vn.pb(a[i]);
cnt[a[i]]--;
}
}
if(mx/2 < nx){
cout << "NO";
return;
}
sort(vm.begin(), vm.end());
bool tot = false;
do {
int from = 0, sm = 0;
for(int i = 0; i < mx; i++){
sm += vm[i];
if(sm == vn[from]){
from++;
sm = 0;
}
if(from == nx){
tot = true;
break;
}
}
} while(next_permutation(vm.begin(), vm.end()) == 1);
if(tot) cout << "YES";
else cout << "NO";
}
int main() {
//ios_base::sync_with_stdio(false);
//cin.tie(nullptr);
//cout.tie(nullptr);
cin >> n >> m;
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < m; i++){
cin >> b[i];
}
//subtask3();
if(n == 1) subtask1();
else{
if(m <= 10) subtask2();
else subtask3();
}
return 0;
}
# | 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... |