/*input
*/
#include <bits/stdc++.h>
#define up(i,a,b) for(int (i) = (a);(i)<=(b);++(i))
#define down(i,b,a) for(int (i) = (b);i>=(a);--i)
#define debug(x) cerr << (x) << '\n';
#define bits(x,i) ((x >> i) & 1)
#define mid ((l+r)/2)
#define pr pair<int,int>
using namespace std;
const int N = 20;
int a[N], b[N];
int n,m;
pr dp[1 << N]; // dp[mask] = (i, j): have already payed i-th bill, and the current bills, have already payed j.
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for(int i=0;i<n;++i) cin >> a[i];
for(int j=0;j<m;++j) cin >> b[j];
for(int i=1;i<(1 << m);++i) dp[i] = {-1, -1};
dp[0] = {0,0};
bool ans = false;
for(int mask = 0; mask < (1 << m); ++mask){
if (dp[mask] == pr(-1,-1)) continue;
if (dp[mask] == pr(n, 0)) ans = 1;
int pos = dp[mask].first, bill = dp[mask].second;
for(int i=0;i<m;++i) {
if (bits(mask, i)) continue;
if (bill + b[i] > a[pos]) continue;
if (bill + b[i] == a[pos]) dp[mask|(1 << i)] = {pos + 1, 0};
else dp[mask|(1 << i)] = {pos, bill + b[i]};
}
}
cout << (ans ? "YES" : "NO");
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
7 ms |
8576 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
47 ms |
8576 KB |
Output is correct |
9 |
Correct |
74 ms |
8576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
7 ms |
8576 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
47 ms |
8576 KB |
Output is correct |
9 |
Correct |
74 ms |
8576 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
69 ms |
8588 KB |
Output is correct |
32 |
Correct |
84 ms |
8576 KB |
Output is correct |
33 |
Correct |
17 ms |
8576 KB |
Output is correct |
34 |
Correct |
7 ms |
8576 KB |
Output is correct |
35 |
Correct |
7 ms |
8576 KB |
Output is correct |
36 |
Correct |
8 ms |
8576 KB |
Output is correct |
37 |
Correct |
7 ms |
8576 KB |
Output is correct |
38 |
Correct |
7 ms |
8576 KB |
Output is correct |
39 |
Correct |
27 ms |
8576 KB |
Output is correct |
40 |
Correct |
7 ms |
8576 KB |
Output is correct |
41 |
Correct |
7 ms |
8576 KB |
Output is correct |
42 |
Correct |
55 ms |
8576 KB |
Output is correct |
43 |
Correct |
9 ms |
8576 KB |
Output is correct |
44 |
Correct |
7 ms |
8576 KB |
Output is correct |
45 |
Correct |
46 ms |
8576 KB |
Output is correct |
46 |
Correct |
27 ms |
8576 KB |
Output is correct |
47 |
Correct |
7 ms |
8576 KB |
Output is correct |
48 |
Correct |
23 ms |
8576 KB |
Output is correct |
49 |
Correct |
7 ms |
8576 KB |
Output is correct |
50 |
Correct |
86 ms |
8592 KB |
Output is correct |
51 |
Correct |
11 ms |
8576 KB |
Output is correct |
52 |
Correct |
12 ms |
8704 KB |
Output is correct |
53 |
Correct |
102 ms |
8576 KB |
Output is correct |
54 |
Correct |
91 ms |
8576 KB |
Output is correct |
55 |
Correct |
90 ms |
8592 KB |
Output is correct |
56 |
Correct |
93 ms |
8576 KB |
Output is correct |
57 |
Correct |
89 ms |
8576 KB |
Output is correct |
58 |
Correct |
84 ms |
8700 KB |
Output is correct |