답안 #650589

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
650589 2022-10-14T08:47:09 Z alvinpiter 은행 (IZhO14_bank) C++17
100 / 100
180 ms 8496 KB
/*
dp[mask] -> The maximum number of prefix of salaries that we can pay using the banknotes represented by "mask".

Transition:
Brute force on the last used banknotes. Assume the last used banknote is the i-th:
* The number of paid salaries so far is k = dp[mask ^ (1 << i)]
* The next salary to pay is the (k + 1)-th one
* After paying the first k salaries, there might be leftover. Use this leftover together with the
  i-th banknote to pay for (k + 1)-th salary

Total complexity is O(m * 2^m)
*/

#include<bits/stdc++.h>
using namespace std;

int n, m, a[22], b[22], asum[22], bsum[(1 << 20) + 3];
int dp[(1 << 20) + 2];

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }

  asum[0] = a[0];
  for (int i = 1; i < n; i++) {
    asum[i] = asum[i - 1] + a[i];
  }

  for (int mask = 0; mask < (1 << m); mask++) {
    bsum[mask] = 0;
    for (int i = 0; i < m; i++) {
      if (mask & (1 << i)) {
        bsum[mask] += b[i];
      }
    }
  }

  dp[0] = 0;
  for (int mask = 1; mask < (1 << m); mask++) {
    dp[mask] = 0;

    for (int i = 0; i < m; i++) {
      if (mask & (1 << i)) {
        int prevMask = mask ^ (1 << i);

        // The current subset of bank notes must be at least as good
        // as the previous subset of bank notes.
        dp[mask] = max(dp[mask], dp[prevMask]);

        int leftOver = bsum[prevMask] - (dp[prevMask] > 0 ? asum[dp[prevMask] - 1] : 0);
        if (leftOver + b[i] == a[dp[prevMask]]) {
          dp[mask] = max(dp[mask], dp[prevMask] + 1);
          dp[mask] = min(dp[mask], n);
        }
      }
    }
  }

  cout << (dp[(1 << m) - 1] == n ? "YES" : "NO") << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 5 ms 468 KB Output is correct
5 Correct 148 ms 8412 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 166 ms 8440 KB Output is correct
9 Correct 150 ms 8436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 388 KB Output is correct
5 Correct 3 ms 356 KB Output is correct
6 Correct 3 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 3 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 5 ms 468 KB Output is correct
5 Correct 148 ms 8412 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 166 ms 8440 KB Output is correct
9 Correct 150 ms 8436 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 3 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 3 ms 388 KB Output is correct
24 Correct 3 ms 356 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 3 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 3 ms 340 KB Output is correct
31 Correct 160 ms 8484 KB Output is correct
32 Correct 163 ms 8484 KB Output is correct
33 Correct 148 ms 8476 KB Output is correct
34 Correct 156 ms 8416 KB Output is correct
35 Correct 148 ms 8408 KB Output is correct
36 Correct 144 ms 8408 KB Output is correct
37 Correct 137 ms 8488 KB Output is correct
38 Correct 140 ms 8368 KB Output is correct
39 Correct 151 ms 8368 KB Output is correct
40 Correct 147 ms 8424 KB Output is correct
41 Correct 144 ms 8492 KB Output is correct
42 Correct 152 ms 8484 KB Output is correct
43 Correct 140 ms 8404 KB Output is correct
44 Correct 151 ms 8436 KB Output is correct
45 Correct 149 ms 8496 KB Output is correct
46 Correct 156 ms 8368 KB Output is correct
47 Correct 139 ms 8368 KB Output is correct
48 Correct 162 ms 8388 KB Output is correct
49 Correct 138 ms 8376 KB Output is correct
50 Correct 157 ms 8488 KB Output is correct
51 Correct 139 ms 8484 KB Output is correct
52 Correct 137 ms 8396 KB Output is correct
53 Correct 147 ms 8476 KB Output is correct
54 Correct 180 ms 8484 KB Output is correct
55 Correct 168 ms 8436 KB Output is correct
56 Correct 146 ms 8392 KB Output is correct
57 Correct 143 ms 8472 KB Output is correct
58 Correct 151 ms 8484 KB Output is correct