/*
#ifndef ONLINE_JUDGE
#include "AkbarKING.h"
#else
#define debug(...)
#define debugArr(...)
#define debugG(...)
#endif
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define $AzH_TxdmN$ ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#define ep emplace_back
#define pb push_back
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using __indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using __indexed_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
const int sz = 3e5+9;
const int LOG = 63;
const int MOD = 1e9+7;
const int INF = 1e18;
int a[sz], b[sz];
int n, m;
set<vector<int>>BIT[sz];
void solve()
{
cin >> n >> m;
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}
for (int i = 0; i < m; ++i)
{
cin >> b[i];
}
auto sum = [&](const vector<int> &indicies)
{
int sum = 0;
for (auto index : indicies)
{
sum += b[index];
}
return sum;
};
for (int i = 0; i < n; ++i)
{
for (int mask = 1; mask < (1LL << m); ++mask)
{
vector<int> ind;
for (int j = 0; j < m; ++j)
{
if (mask & (1LL << j))
{
ind.ep(j);
}
}
if (sum(ind) == a[i])
{
BIT[i].insert(ind);
}
}
}
for (int i = 0; i < n; ++i)
{
if (BIT[i].empty())
{
cout << "NO\n";
return;
}
}
function<bool(int, vector<vector<int>> &)> backtrack = [&](int i, vector<vector<int>> ¤t)
{
if (i == n)
{
return true;
}
for (const auto &subset : BIT[i])
{
bool conflict = false;
for (const auto &chosen : current)
{
vector<int> intersection;
set_intersection(all(chosen), all(subset), back_inserter(intersection));
if (!intersection.empty())
{
conflict = true;
break;
}
}
if (!conflict)
{
current.ep(subset);
if (backtrack(i + 1, current))
{
return true;
}
current.pop_back();
}
}
return false;
};
vector<vector<int>> current;
cout << (backtrack(0, current) ? "YES" : "NO") << '\n';
}
signed main()
{
$AzH_TxdmN$
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
}
# | 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... |