Submission #1106133

# Submission time Handle Problem Language Result Execution time Memory
1106133 2024-10-29T10:33:54 Z manhlinh1501 Feast (NOI19_feast) C++17
30 / 100
37 ms 7168 KB
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;

template<class X, class Y>
bool minimize(X &x, const Y &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

const int MAXN = 3e5 + 5;
const int oo32 = 1e9 + 5;
const i64 oo64 = 1e18 + 5;

int N, K;
int a[MAXN];

namespace subtask1 {
    bool is_subtask() {
        for(int i = 1; i <= N; i++) {
            if(a[i] < 0) return false;
        }
        return true;
    }
    void solution() {
        if(is_subtask() == false) return;
        i64 ans = accumulate(a + 1, a + N + 1, 0LL);
        cout << ans;
        exit(0);
    }
}

namespace subtask2 {
    bool is_subtask() {
        int c = 0;
        for(int i = 1; i <= N; i++)
            c += (a[i] < 0);
        return c <= 1;
    }

    void solution() {
        if(is_subtask() == false) return;
        i64 tot = accumulate(a + 1, a + N + 1, 0LL);
        i64 ans = tot;
        for(int i = 1; i <= N; i++) ans = max(ans, tot - a[i]);
        cout << ans;
        exit(0);
    }
}

namespace subtask3 {
    bool is_subtask() {
        return K == 1;
    }
    i64 dp[MAXN];

    void solution() {
        if(is_subtask() == false) return;
        for(int i = 1; i <= N; i++) dp[i] = max(dp[i - 1], 0LL) + a[i];
        cout << *max_element(dp + 1, dp + N + 1);
        exit(0);
    }
}

namespace subtask {
    void solution() {
        for(int i = 1; i <= N; i++) a[i] += a[i - 1];
        vector<vector<i64>> dp(N + 1, vector<i64>(K + 1, -oo64));
        i64 ans = 0;
        dp[0][0] = 0;
        for(int j = 1; j <= K; j++) {
            for(int i = 1; i <= N; i++) {
                for(int z = 1; z <= i; z++)
                    maximize(dp[i][j], max(dp[z][j], dp[z - 1][j - 1] + a[i] - a[z - 1]));
                maximize(ans, dp[i][j]);
            }
        }
        cout << ans;
    }
}

signed main() {
#define task ""
    if(fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> N >> K;
    for(int i = 1; i <= N; i++) cin >> a[i];
    subtask1::solution();
    subtask3::solution();
    subtask2::solution();
    subtask::solution();
    return (0 ^ 0);
}


Compilation message

feast.cpp: In function 'int main()':
feast.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 3656 KB Output is correct
2 Correct 26 ms 4444 KB Output is correct
3 Correct 27 ms 3400 KB Output is correct
4 Correct 24 ms 3724 KB Output is correct
5 Correct 24 ms 4424 KB Output is correct
6 Correct 23 ms 4168 KB Output is correct
7 Correct 24 ms 4176 KB Output is correct
8 Correct 23 ms 4432 KB Output is correct
9 Correct 23 ms 4176 KB Output is correct
10 Correct 32 ms 3512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 4836 KB Output is correct
2 Correct 17 ms 4672 KB Output is correct
3 Correct 16 ms 4944 KB Output is correct
4 Correct 15 ms 1872 KB Output is correct
5 Correct 24 ms 6224 KB Output is correct
6 Correct 16 ms 4176 KB Output is correct
7 Correct 20 ms 2148 KB Output is correct
8 Correct 24 ms 6624 KB Output is correct
9 Correct 24 ms 6480 KB Output is correct
10 Correct 16 ms 2640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 5888 KB Output is correct
2 Correct 29 ms 6232 KB Output is correct
3 Correct 37 ms 6728 KB Output is correct
4 Correct 25 ms 6216 KB Output is correct
5 Correct 25 ms 5704 KB Output is correct
6 Correct 29 ms 7168 KB Output is correct
7 Correct 25 ms 6984 KB Output is correct
8 Correct 32 ms 5712 KB Output is correct
9 Correct 30 ms 6196 KB Output is correct
10 Correct 25 ms 6988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Incorrect 1 ms 336 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Incorrect 1 ms 336 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Incorrect 1 ms 336 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 3656 KB Output is correct
2 Correct 26 ms 4444 KB Output is correct
3 Correct 27 ms 3400 KB Output is correct
4 Correct 24 ms 3724 KB Output is correct
5 Correct 24 ms 4424 KB Output is correct
6 Correct 23 ms 4168 KB Output is correct
7 Correct 24 ms 4176 KB Output is correct
8 Correct 23 ms 4432 KB Output is correct
9 Correct 23 ms 4176 KB Output is correct
10 Correct 32 ms 3512 KB Output is correct
11 Correct 18 ms 4836 KB Output is correct
12 Correct 17 ms 4672 KB Output is correct
13 Correct 16 ms 4944 KB Output is correct
14 Correct 15 ms 1872 KB Output is correct
15 Correct 24 ms 6224 KB Output is correct
16 Correct 16 ms 4176 KB Output is correct
17 Correct 20 ms 2148 KB Output is correct
18 Correct 24 ms 6624 KB Output is correct
19 Correct 24 ms 6480 KB Output is correct
20 Correct 16 ms 2640 KB Output is correct
21 Correct 29 ms 5888 KB Output is correct
22 Correct 29 ms 6232 KB Output is correct
23 Correct 37 ms 6728 KB Output is correct
24 Correct 25 ms 6216 KB Output is correct
25 Correct 25 ms 5704 KB Output is correct
26 Correct 29 ms 7168 KB Output is correct
27 Correct 25 ms 6984 KB Output is correct
28 Correct 32 ms 5712 KB Output is correct
29 Correct 30 ms 6196 KB Output is correct
30 Correct 25 ms 6988 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Incorrect 1 ms 336 KB Output isn't correct
33 Halted 0 ms 0 KB -