This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <random>
using namespace __gnu_pbds;
using namespace std;
#define sp << ' '
#define ll long long
#define ull unsigned long long
#define endl '\n'
#define MOD 1000000007ll
#define b_rev boost::adaptors::reverse
typedef vector<int> vi;
typedef vector<ll> vll;
typedef array<int, 2> ii;
typedef vector<array<int, 2>> vii;
#define vv(type) vector<vector<type>>
#define F first
#define S second
#define PB push_back
#define REP(i, a, b) for (int i = a; i < b; i++)
#define elif else if
#define nl <<'\n'
#define out(txt) {cout << txt << '\n'; return;}
template<class T> using ordered_set = tree<T, null_type, less<T>,
rb_tree_tag, tree_order_statistics_node_update>;
template<class T> using multi_ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using min_queue = priority_queue<T, vector<T>, greater<>>;
// Pair addition
template<typename T, typename U, typename V, typename W>
auto operator+(const std::pair<T, U> &l, const std::pair<V, W> &r)
-> std::pair<decltype(l.first + r.first), decltype(l.second + r.second)> {
return {l.first + r.first, l.second + r.second};
}
template<typename T>
T getValue() {
T value;
std::cin >> value;
return value;
}
void printVec(vector<int> v) {
for (const auto &i: v) { cout << i sp; }
cout nl;
}
void printVec(vector<ll> v) {
for (const auto &i: v) { cout << i sp; }
cout nl;
}
void printVec(vector<vi> v) {
for (const auto &item: v)printVec(item);
}
void printVec(vector<vector<ll>> v) {
for (const auto &item: v)printVec(item);
}
void printPair(pair<ll, ll> x) { cout << x.first sp << x.second nl; }
ll lcm(ll a, ll b) {
return (a / __gcd(a, b)) * b;
}
short int sign(ll x) {
if (x < 0) return -1;
else if (x > 0) return 1;
else return 0;
}
double START_TIME;
void solve();
void precompute();
int main(int argc, char *argv[]) {
if (argc > 1 && argv[1][0] - '0') {
freopen("out.txt", "w", stdout);
freopen("in.txt", "r", stdin);
START_TIME = clock();
}
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
// precompute();
// cin >> t;
for (int i = 0; i < t; ++i) {
// cout << "Case #" << i + 1 << ": ";
solve();
}
if (argc > 1 && argv[1][0] - '0') {
double END_TIME = clock();
double TIME_TAKEN = double(END_TIME - START_TIME) / double(CLOCKS_PER_SEC);
cout << "\n// Time taken = " << fixed << std::setprecision(5) << TIME_TAKEN << " ms\n";
}
}
void solve() {
ll n, m;
cin >> n >> m;
vi a(n), b(m);
for (int i = 0; i < n; ++i)cin>>a[i];
for (int i = 0; i < m; ++i)cin >> b[i];
vector<int> dp(1 << m, -1);
vector<int> rem(1 << m, -1);
dp[0] = rem[0] = 0;
for (int msk = 0; msk < (1 << m); ++msk) {
if (dp[msk] == n){
cout << "YES\n";
return;
}
if (dp[msk] == -1) continue;
for (int i = 0; i < m; ++i) {
if (msk & (1 << i)) continue;
if (rem[msk] + b[i] > a[dp[msk]]){
continue;
}
rem[msk | (1 << i)] = rem[msk] + b[i];
dp[msk | (1 << i)] = dp[msk];
if (rem[msk | (1 << i)] == a[dp[msk]]){
rem[msk | (1 << i)] = 0;
dp[msk | (1 << i)]++;
}
}
}
cout << "NO\n";
}
Compilation message (stderr)
bank.cpp: In function 'int main(int, char**)':
bank.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen("out.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bank.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | freopen("in.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |