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 <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define pbds tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
#define ll long long
#define int ll
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define rep(i,a,b) for(int i = a; i < b; i++)
#define irep(i, a, b) for(int i = a; i > b; i--)
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define clz __builtin_clzll //leading zeroes
#define ctz __builtin_ctzll //trailing zeroes
#define ppc __builtin_popcountll
#define nl cout << '\n'
template<typename T>
istream &operator>>(istream &in, vector<T>& v){
for(int i = 0; i < v.size(); i++)
in >> v[i];
return in;
}
template<typename T>
ostream &operator<<(ostream &out, vector<T>& v){
for(int i = 0; i < v.size(); i++)
out << v[i] << " ";
return out;
}
template<typename T1, typename T2>istream& operator>>(istream& in, pair<T1, T2>& p){ in >> p.fr >> p.sc; return in; }
template<typename T1, typename T2>ostream& operator<<(ostream& out, pair<T1, T2>& p){ out << p.fr << " " << p.sc << " "; return out; }
const ll INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const int MAX = numeric_limits<int>::max();
const int MIN = numeric_limits<int>::min();
const int N = 0;
int egcd(int a, int b, int& x, int& y, int mod){
if(b == 0){
x = 1, y = 0;
return a;
}
int x1, y1, g = egcd(b, a % b, x1, y1, mod);
x = y1 % mod, y = ((x1 - (a / b)*y1) % mod + mod) % mod;
return g;
}
void solve(){
int n, m; cin >> n >> m;
vi a(n), b(m); cin >> a >> b;
vector<pii> dp(1<<m, {0, 0});
//dp[msk] -> {max prefix that can be paid using msk subset of denominations, remaining sum of denominations after paying them}
rep(msk, 1, 1<<m) rep(i, 0, m) if(msk&(1<<i)){
if(dp[msk^(1<<i)].fr < n && dp[msk^(1<<i)].sc + b[i] == a[dp[msk^(1<<i)].fr]) dp[msk] = {dp[msk^(1<<i)].fr + 1, 0};
else if(dp[msk].fr <= dp[msk^(1<<i)].fr) dp[msk] = {dp[msk^(1<<i)].fr, dp[msk^(1<<i)].sc + b[i]};
}
cout << (dp[(1<<m) - 1].fr == n ? "YES" : "NO"); nl;
}
signed main(){
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1; //cin >> t;
rep(i, 0, t){
solve();
}
}
Compilation message (stderr)
bank.cpp: In instantiation of 'std::istream& operator>>(std::istream&, std::vector<_Tp>&) [with T = long long int; std::istream = std::basic_istream<char>]':
bank.cpp:68:24: required from here
bank.cpp:35:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~
# | 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... |