답안 #884387

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
884387 2023-12-07T09:19:00 Z Asamai Feast (NOI19_feast) C++17
71 / 100
1000 ms 15956 KB
#include <bits/stdc++.h>

using namespace std;

template<class A, class B> bool maximize(A& x, B y) {if (x < y) return x = y, true; else return false;}
template<class A, class B> bool minimize(A& x, B y) {if (x > y) return x = y, true; else return false;}

#define     all(a)                a.begin(), a.end()
#define     pb                    push_back
#define     pf                    push_front
#define     fi                    first
#define     se                    second
#define     int                   long long

typedef     long long             ll;
typedef     unsigned long long    ull;
typedef     double                db;
typedef     long double           ld;
typedef     pair<db, db>          pdb;
typedef     pair<ld, ld>          pld;
typedef     pair<int, int>        pii;
typedef     pair<ll, ll>          pll;
typedef     pair<ll, int>         plli;
typedef     pair<int, ll>         pill;

const int MAX_N = 3e5 + 5;
const int mod = 1e9 + 7;
const ll inf = 1e18;

int n, k;
int a[MAX_N];
int pref[MAX_N];
int cntAm;
int dp[2][MAX_N][2];

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    if (fopen("party.inp", "r")) {
        freopen("party.inp", "r", stdin);
        freopen("party.out", "w", stdout);
    }

    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        pref[i] = pref[i - 1] + a[i];
        cntAm += (a[i] < 0);
    }

    if (cntAm == 0) {
        cout << pref[n];
        return 0;
    }

    if (k == 1) {
        int ans = 0;
        int prefMin = 0;
        for (int i = 1; i <= n; i++) {
            maximize(ans, pref[i] - prefMin);
            minimize(prefMin, pref[i]);
        }
        cout << ans;
        return 0;
    }

    if (cntAm == 1) {
        // k > 1
        for (int i = 1; i <= n; i++) {
            if (a[i] < 0) {
                cout << pref[n] - a[i];
                return 0;
            }
        }
        return 0;
    }

    bool curr = 0, nxt = 1;
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= 1; j++) {
            dp[curr][i][j] = dp[nxt][i][j] = -inf;
        }
    }
    dp[curr][0][0] = 0;

    for (int i = 0; i < n; i++, swap(curr, nxt)) {
        for (int j = 0; j <= i; j++) {
            if (dp[curr][j][0] > -inf) {
                // dp[curr][j][0]
                maximize(dp[nxt][j][0], dp[curr][j][0]); // khong mo
                maximize(dp[nxt][j + 1][1], dp[curr][j][0] + a[i + 1]); // mo
            }

            if (dp[curr][j][1] > -inf) {
                // dp[curr][j][1]
                maximize(dp[nxt][j][1], dp[curr][j][1] + a[i + 1]); // tiep tuc
                maximize(dp[nxt][j][0], dp[curr][j][1]); // dong lai
            }
        }

        for (int j = 0; j <= i; j++) {
            for (int state = 0; state <= 1; state++) {
                dp[curr][j][state] = -inf;
            }
        }
    }

    int ans = 0;
    for (int j = 0; j <= k; j++) {
        for (int state = 0; state <= 1; state++) {
            maximize(ans, dp[curr][j][state]);
        }
    }

    cout << ans;

    return 0;
}

/*

dp[i][j][0/1]

Thursday, 07 December 2023
15:25:54
*/

Compilation message

feast.cpp: In function 'int main()':
feast.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen("party.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         freopen("party.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 5212 KB Output is correct
2 Correct 23 ms 4956 KB Output is correct
3 Correct 23 ms 5132 KB Output is correct
4 Correct 22 ms 4956 KB Output is correct
5 Correct 23 ms 4952 KB Output is correct
6 Correct 23 ms 4956 KB Output is correct
7 Correct 22 ms 4956 KB Output is correct
8 Correct 28 ms 4952 KB Output is correct
9 Correct 22 ms 4944 KB Output is correct
10 Correct 23 ms 4952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 4956 KB Output is correct
2 Correct 16 ms 4956 KB Output is correct
3 Correct 20 ms 4956 KB Output is correct
4 Correct 19 ms 5064 KB Output is correct
5 Correct 24 ms 7204 KB Output is correct
6 Correct 15 ms 5968 KB Output is correct
7 Correct 15 ms 6236 KB Output is correct
8 Correct 24 ms 7596 KB Output is correct
9 Correct 30 ms 7260 KB Output is correct
10 Correct 15 ms 6236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 5016 KB Output is correct
2 Correct 25 ms 5000 KB Output is correct
3 Correct 25 ms 4956 KB Output is correct
4 Correct 25 ms 4944 KB Output is correct
5 Correct 25 ms 4944 KB Output is correct
6 Correct 25 ms 4984 KB Output is correct
7 Correct 28 ms 4900 KB Output is correct
8 Correct 25 ms 4880 KB Output is correct
9 Correct 26 ms 5108 KB Output is correct
10 Correct 31 ms 5024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 4596 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 4596 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4440 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4440 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 1 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 4596 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4440 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4440 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 1 ms 4440 KB Output is correct
21 Correct 5 ms 4444 KB Output is correct
22 Correct 6 ms 4616 KB Output is correct
23 Correct 5 ms 4444 KB Output is correct
24 Correct 5 ms 4440 KB Output is correct
25 Correct 6 ms 4444 KB Output is correct
26 Correct 5 ms 4528 KB Output is correct
27 Correct 5 ms 4444 KB Output is correct
28 Correct 5 ms 4444 KB Output is correct
29 Correct 5 ms 4444 KB Output is correct
30 Correct 5 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 5212 KB Output is correct
2 Correct 23 ms 4956 KB Output is correct
3 Correct 23 ms 5132 KB Output is correct
4 Correct 22 ms 4956 KB Output is correct
5 Correct 23 ms 4952 KB Output is correct
6 Correct 23 ms 4956 KB Output is correct
7 Correct 22 ms 4956 KB Output is correct
8 Correct 28 ms 4952 KB Output is correct
9 Correct 22 ms 4944 KB Output is correct
10 Correct 23 ms 4952 KB Output is correct
11 Correct 15 ms 4956 KB Output is correct
12 Correct 16 ms 4956 KB Output is correct
13 Correct 20 ms 4956 KB Output is correct
14 Correct 19 ms 5064 KB Output is correct
15 Correct 24 ms 7204 KB Output is correct
16 Correct 15 ms 5968 KB Output is correct
17 Correct 15 ms 6236 KB Output is correct
18 Correct 24 ms 7596 KB Output is correct
19 Correct 30 ms 7260 KB Output is correct
20 Correct 15 ms 6236 KB Output is correct
21 Correct 24 ms 5016 KB Output is correct
22 Correct 25 ms 5000 KB Output is correct
23 Correct 25 ms 4956 KB Output is correct
24 Correct 25 ms 4944 KB Output is correct
25 Correct 25 ms 4944 KB Output is correct
26 Correct 25 ms 4984 KB Output is correct
27 Correct 28 ms 4900 KB Output is correct
28 Correct 25 ms 4880 KB Output is correct
29 Correct 26 ms 5108 KB Output is correct
30 Correct 31 ms 5024 KB Output is correct
31 Correct 1 ms 2392 KB Output is correct
32 Correct 1 ms 4596 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 1 ms 4444 KB Output is correct
35 Correct 1 ms 4444 KB Output is correct
36 Correct 1 ms 4444 KB Output is correct
37 Correct 1 ms 4440 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 2396 KB Output is correct
40 Correct 1 ms 4444 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 1 ms 4444 KB Output is correct
43 Correct 1 ms 4440 KB Output is correct
44 Correct 1 ms 4444 KB Output is correct
45 Correct 1 ms 4440 KB Output is correct
46 Correct 1 ms 4444 KB Output is correct
47 Correct 1 ms 4444 KB Output is correct
48 Correct 1 ms 4444 KB Output is correct
49 Correct 1 ms 4440 KB Output is correct
50 Correct 1 ms 4440 KB Output is correct
51 Correct 5 ms 4444 KB Output is correct
52 Correct 6 ms 4616 KB Output is correct
53 Correct 5 ms 4444 KB Output is correct
54 Correct 5 ms 4440 KB Output is correct
55 Correct 6 ms 4444 KB Output is correct
56 Correct 5 ms 4528 KB Output is correct
57 Correct 5 ms 4444 KB Output is correct
58 Correct 5 ms 4444 KB Output is correct
59 Correct 5 ms 4444 KB Output is correct
60 Correct 5 ms 4444 KB Output is correct
61 Execution timed out 1032 ms 15956 KB Time limit exceeded
62 Halted 0 ms 0 KB -