답안 #833141

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
833141 2023-08-22T02:20:44 Z veehz Feast (NOI19_feast) C++17
71 / 100
255 ms 262144 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];

  // subtask 1 a_i > 0
  if (*min_element(a.begin(), a.end()) >= 0) {
    cout << accumulate(a.begin(), a.end(), 0LL) << endl;
    return 0;
  }

  // subtask 2 at most one a_i < 0
  if (count_if(a.begin(), a.end(), [](ll x) { return x < 0; }) <= 1) {
    int pos =
        find_if(a.begin(), a.end(), [](ll x) { return x < 0; }) - a.begin();
    ll mx = 0;
    mx = max(mx, accumulate(a.begin(), a.begin() + pos, 0LL));
    mx = max(mx, accumulate(a.begin() + pos + 1, a.end(), 0LL));
    mx = max(mx, accumulate(a.begin(), a.end(), 0LL));
    if (k >= 2) mx = max(mx, accumulate(a.begin(), a.end(), 0LL) - a[pos]);
    cout << mx << endl;
    return 0;
  }

  // subtask 3 K = 1, maximum subarray
  if (k == 1) {
    ll mx = 0, cur = 0;
    for (int i = 0; i < n; i++) {
      cur += a[i];
      mx = max(mx, cur);
      cur = max(cur, 0LL);
    }
    cout << mx << endl;
    return 0;
  }

  // dp[n][k][2] // dp[cur_index][how_many_left_excluding_cur][is_in_subarray]
  vector<vector<vector<ll>>> dp(n + 1,
                                vector<vector<ll>>(k + 1, vector<ll>(2)));

  dp[0][k - 1][1] = a[0];
  for (int i = 1; i < n; i++) {
    // dp[i][k][0] = 0
    for (int j = 0; j < k; j++) {
      dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j][1]);
      dp[i][j][1] = max(dp[i - 1][j + 1][0], dp[i - 1][j][1]) + a[i];
    }
  }

  ll mx = 0;
  for (int i = 0; i <= k; i++) {
    mx = max(mx, max(dp[n - 1][i][0], dp[n - 1][i][1]));
  }
  cout << mx << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 2560 KB Output is correct
2 Correct 80 ms 2516 KB Output is correct
3 Correct 86 ms 2644 KB Output is correct
4 Correct 80 ms 2516 KB Output is correct
5 Correct 79 ms 2516 KB Output is correct
6 Correct 79 ms 2568 KB Output is correct
7 Correct 83 ms 2640 KB Output is correct
8 Correct 79 ms 2608 KB Output is correct
9 Correct 78 ms 2516 KB Output is correct
10 Correct 102 ms 2592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 2516 KB Output is correct
2 Correct 43 ms 2644 KB Output is correct
3 Correct 43 ms 2516 KB Output is correct
4 Correct 43 ms 2516 KB Output is correct
5 Correct 79 ms 2568 KB Output is correct
6 Correct 43 ms 2516 KB Output is correct
7 Correct 43 ms 2644 KB Output is correct
8 Correct 79 ms 2624 KB Output is correct
9 Correct 77 ms 2568 KB Output is correct
10 Correct 44 ms 2516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 2616 KB Output is correct
2 Correct 86 ms 2580 KB Output is correct
3 Correct 85 ms 2600 KB Output is correct
4 Correct 84 ms 2516 KB Output is correct
5 Correct 85 ms 2608 KB Output is correct
6 Correct 94 ms 2612 KB Output is correct
7 Correct 87 ms 2644 KB Output is correct
8 Correct 86 ms 2604 KB Output is correct
9 Correct 91 ms 2644 KB Output is correct
10 Correct 87 ms 2516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 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 0 ms 212 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 852 KB Output is correct
12 Correct 1 ms 1236 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 852 KB Output is correct
12 Correct 1 ms 1236 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 980 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 19 ms 21676 KB Output is correct
22 Correct 160 ms 179372 KB Output is correct
23 Correct 40 ms 41556 KB Output is correct
24 Correct 22 ms 23380 KB Output is correct
25 Correct 30 ms 32468 KB Output is correct
26 Correct 18 ms 19412 KB Output is correct
27 Correct 28 ms 30592 KB Output is correct
28 Correct 4 ms 3924 KB Output is correct
29 Correct 4 ms 3540 KB Output is correct
30 Correct 1 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 2560 KB Output is correct
2 Correct 80 ms 2516 KB Output is correct
3 Correct 86 ms 2644 KB Output is correct
4 Correct 80 ms 2516 KB Output is correct
5 Correct 79 ms 2516 KB Output is correct
6 Correct 79 ms 2568 KB Output is correct
7 Correct 83 ms 2640 KB Output is correct
8 Correct 79 ms 2608 KB Output is correct
9 Correct 78 ms 2516 KB Output is correct
10 Correct 102 ms 2592 KB Output is correct
11 Correct 42 ms 2516 KB Output is correct
12 Correct 43 ms 2644 KB Output is correct
13 Correct 43 ms 2516 KB Output is correct
14 Correct 43 ms 2516 KB Output is correct
15 Correct 79 ms 2568 KB Output is correct
16 Correct 43 ms 2516 KB Output is correct
17 Correct 43 ms 2644 KB Output is correct
18 Correct 79 ms 2624 KB Output is correct
19 Correct 77 ms 2568 KB Output is correct
20 Correct 44 ms 2516 KB Output is correct
21 Correct 86 ms 2616 KB Output is correct
22 Correct 86 ms 2580 KB Output is correct
23 Correct 85 ms 2600 KB Output is correct
24 Correct 84 ms 2516 KB Output is correct
25 Correct 85 ms 2608 KB Output is correct
26 Correct 94 ms 2612 KB Output is correct
27 Correct 87 ms 2644 KB Output is correct
28 Correct 86 ms 2604 KB Output is correct
29 Correct 91 ms 2644 KB Output is correct
30 Correct 87 ms 2516 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 852 KB Output is correct
42 Correct 1 ms 1236 KB Output is correct
43 Correct 1 ms 596 KB Output is correct
44 Correct 1 ms 980 KB Output is correct
45 Correct 1 ms 724 KB Output is correct
46 Correct 1 ms 596 KB Output is correct
47 Correct 1 ms 1108 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 468 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 19 ms 21676 KB Output is correct
52 Correct 160 ms 179372 KB Output is correct
53 Correct 40 ms 41556 KB Output is correct
54 Correct 22 ms 23380 KB Output is correct
55 Correct 30 ms 32468 KB Output is correct
56 Correct 18 ms 19412 KB Output is correct
57 Correct 28 ms 30592 KB Output is correct
58 Correct 4 ms 3924 KB Output is correct
59 Correct 4 ms 3540 KB Output is correct
60 Correct 1 ms 724 KB Output is correct
61 Runtime error 255 ms 262144 KB Execution killed with signal 9
62 Halted 0 ms 0 KB -