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>
using namespace std;
// type
using ll = long long;
using ld = long double;
using db = double;
using str = string;
// pairs
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
// vector
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define sor(x) sort(all(x))
#define rsz resize
#define rtn return
// binary search
#define lb lower_bound
#define ub upper_bound
template<class T> ll lwb(vector<T>& a, const T& b) { rtn lb(all(a), b) - a.begin(); }
template<class T> ll upb(vector<T>& a, const T& b) { rtn ub(all(a), b) - a.begin(); }
// pairs
#define mp make_pair
#define f first
#define s second
// loops
#define FOR(i,a,b) for (ll i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (ll i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define each(a,x) for (auto& a: x)
#define cont continue
#define bre break
// FILE I/O
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void setIO(str s) {
setIn(s+".inp"); setOut(s+".out");
}
const int MOD = 1e9 + 7; //998244353;
const int MX = 2e5+5;
const ll INF = 1e9;
const db PI = acos((db)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
// modulo, exponentiation, inverse
ll pow_mod(ll a, ll b, ll m) { ll res = 1; while(b){ res = res * (b & 1 ? a : 1) % m; a = a * a % m; b >>= 1; } rtn res; }
ll inv(ll a, ll m) { rtn pow_mod(a, m - 2, m); }
ll nxt(void){ // fast way for cin operation
ll x; cin >> x;
rtn x;
}
#define isOn(i, mask) ((mask >> i) & 1)
ll n, m, a[20], b[20];
pl dp[1 << 20];
void init(void){
cin >> n >> m;
F0R(i, n) cin >> a[i];
F0R(i, m) cin >> b[i];
F0R(mask, 1 << n) dp[mask] = {-INF, 0};
dp[0] = {0, 0};
}
void solve(void){
F0R(mask, 1 << m){
if(dp[mask].f == -INF) cont;
// cout << mask << ' ' << dp[mask].f << ' ' << dp[mask].s << '\n';
if(dp[mask].f == n){
cout << "YES";
rtn;
}
F0R(i, m) if(!isOn(i, mask)){
ll newVal = dp[mask].s + b[i];
if(newVal < dp[mask | (1 << i)].f) cont;
ll target = a[dp[mask].f];
if(newVal < target)
dp[mask | (1 << i)] = max(dp[mask | (1 << i)],{dp[mask].f, newVal});
else if(newVal == target)
dp[mask | (1 << i)] = max(dp[mask | (1 << i)], {dp[mask].f + 1, 0});
}
}
cout << "NO";
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
// setIO("CodeforcesEasy");
init();
solve();
rtn 0;
}
Compilation message (stderr)
bank.cpp: In function 'void setIn(str)':
bank.cpp:54:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | void setIn(str s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
bank.cpp: In function 'void setOut(str)':
bank.cpp:55:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | void setOut(str s) { freopen(s.c_str(),"w",stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |