#include "bits/stdc++.h"
#define pb push_back
#define endl '\n'
#define fi first
#define se second
#define CDIV(a,b) (((a)+(b)-(1))/(b))
using ll = long long;
using ld = long double;
const ll inf = 1e15 + 37;
const ll mod = 1e9 + 7;
const ll N = 3e5 + 37;
const ld eps = 1e-9;
const ld PI = acos((ld)-1);
template<typename T, size_t N>
std::ostream& operator<<(std::ostream& os, const std::array<T, N>& a);
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p);
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s);
template<typename T, typename cmp>
std::ostream& operator<<(std::ostream& os, const std::set<T, cmp>& s);
template<typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v);
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline void solve(){
ll n, m;
cin >> n >> m;
vector<ll> ppl(n), notes(m);
cin >> ppl >> notes;
const ll SZ = 1 << m;
vector<pair<ll, ll> > dp(SZ, {-1, -inf});
dp[0] = {0, 0};
bool ok = false;
for(ll mask = 1; mask < SZ; ++mask) {
for(ll i = 0; i < m; ++i) {
if(!(1 << i & mask))
continue;
const ll prev_mask = mask ^ (1 << i);
const auto [cur_person, collected] = dp[prev_mask];
if(cur_person >= n) {
ok = true;
break;
}
if(cur_person == -1)
continue;
if(collected + notes[i] == ppl[cur_person]) {
dp[mask] = max(dp[mask], make_pair(cur_person + 1, 0LL));
}
else if(collected + notes[i] < ppl[cur_person]) {
dp[mask] = max(dp[mask], make_pair(cur_person, collected + notes[i]));
}
}
if(ok)
break;
}
// for(ll i = 0; i < SZ; ++i) {
// string s = bitset<5>(i).to_string();
// reverse(s.begin(), s.end());
// cout << s << " : " << dp[i] << endl;
// }
cout << (max_element(dp.begin(), dp.end())->first >= n ? "YES" : "NO");
}
signed main(){
std::ios_base::sync_with_stdio(0);
std::cin.tie(NULL);
// signed t; cin >> t; while(t--)
solve();
}
template<typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
for(T& x : v)
is >> x;
return is;
}
template<typename T, size_t N>
std::ostream& operator<<(std::ostream& os, const std::array<T, N>& a) {
os << "[";
for(size_t i = 0; i + 1 < N; ++i) {
os << a[i] << ", ";
}
if(N > 0)
os << a[N - 1];
os << "]";
return os;
}
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {
os << "(" << p.first << ", " << p.second << ") ";
return os;
}
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << '[';
for(auto x : v)
os << x << ", ";
os << "] ";
return os;
}
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
os << "{";
for(auto x : s)
os << x << ", ";
os << "} ";
return os;
}
//
template<typename T, typename cmp>
std::ostream& operator<<(std::ostream& os, const std::set<T, cmp>& s) {
os << "{";
for(auto x : s)
os << x << ", ";
os << "} ";
return os;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
824 KB |
Output is correct |
5 |
Correct |
86 ms |
16984 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
16732 KB |
Output is correct |
9 |
Correct |
86 ms |
16732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
824 KB |
Output is correct |
5 |
Correct |
86 ms |
16984 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
16732 KB |
Output is correct |
9 |
Correct |
86 ms |
16732 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
2 ms |
600 KB |
Output is correct |
29 |
Correct |
2 ms |
600 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
91 ms |
16732 KB |
Output is correct |
32 |
Correct |
96 ms |
16728 KB |
Output is correct |
33 |
Correct |
75 ms |
16732 KB |
Output is correct |
34 |
Correct |
82 ms |
16732 KB |
Output is correct |
35 |
Correct |
84 ms |
16728 KB |
Output is correct |
36 |
Correct |
87 ms |
16728 KB |
Output is correct |
37 |
Correct |
83 ms |
16732 KB |
Output is correct |
38 |
Correct |
93 ms |
16984 KB |
Output is correct |
39 |
Correct |
4 ms |
16728 KB |
Output is correct |
40 |
Correct |
84 ms |
16632 KB |
Output is correct |
41 |
Correct |
86 ms |
16732 KB |
Output is correct |
42 |
Correct |
100 ms |
16732 KB |
Output is correct |
43 |
Correct |
76 ms |
16728 KB |
Output is correct |
44 |
Correct |
85 ms |
16760 KB |
Output is correct |
45 |
Correct |
6 ms |
16732 KB |
Output is correct |
46 |
Correct |
89 ms |
16732 KB |
Output is correct |
47 |
Correct |
84 ms |
16732 KB |
Output is correct |
48 |
Correct |
5 ms |
16732 KB |
Output is correct |
49 |
Correct |
85 ms |
16732 KB |
Output is correct |
50 |
Correct |
86 ms |
16732 KB |
Output is correct |
51 |
Correct |
81 ms |
16728 KB |
Output is correct |
52 |
Correct |
69 ms |
16728 KB |
Output is correct |
53 |
Correct |
96 ms |
16728 KB |
Output is correct |
54 |
Correct |
93 ms |
16732 KB |
Output is correct |
55 |
Correct |
90 ms |
16728 KB |
Output is correct |
56 |
Correct |
93 ms |
16732 KB |
Output is correct |
57 |
Correct |
87 ms |
16732 KB |
Output is correct |
58 |
Correct |
88 ms |
16728 KB |
Output is correct |