Submission #1062469

# Submission time Handle Problem Language Result Execution time Memory
1062469 2024-08-17T07:35:36 Z CodeLakVN Feast (NOI19_feast) C++17
71 / 100
38 ms 64240 KB
#include <bits/stdc++.h>

#define no "NO"
#define yes "YES"
#define F first
#define S second
#define vec vector
#define task "FEAST"
#define ll long long
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maximize(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool minimize(U &a, V b) {
    if (a > b) { a = b; return 1; } return 0;
}

const int N = (int)3e5 + 9;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;

int n, k;
int a[N];

namespace sub3 {
    bool valid() {
        return k == 1;
    }

    void solve() {
        ll sum = 0, minPre = 0, ans = 0;
        FOR(i, 1, n) {
            sum += a[i];
            maximize(ans, sum - minPre);
            minimize(minPre, sum);
        }
        cout << ans << "\n";
    }
}

namespace sub1 {
    bool valid() {
        return *min_element(a + 1, a + n + 1) >= 0;
    }

    void solve() {
        ll ans = 0;
        FOR(i, 1, n) ans += a[i];
        cout << ans << "\n";
    }
}

namespace sub2 {
    bool valid() {
        bool appeared = 0;
        FOR(i, 1, n) {
            if (a[i] < 0 && appeared) return false;
            if (a[i] < 0) appeared = 1;
        }
        return true;
    }

    void solve() {
        ll ans = 0;
        FOR(i, 1, n) ans += a[i];
        ans -= *min_element(a + 1, a + n + 1);
        cout << ans << "\n";
    }
}

namespace sub5 {
    const int N = 303;
    ll dp[N][N];
    ll sum[N];

    bool valid() {
        return n <= 300;
    }

    void solve() {
        FOR(i, 1, n) sum[i] = sum[i - 1] + a[i];

        memset(dp, -0x3f, sizeof(dp));
        dp[0][0] = 0;
        FOR(i, 1, n) FOR(j, 1, min(k, i)) {
            ll maxDP = dp[j - 1][j - 1];
            FOR(x, j, i) {
                maximize(dp[i][j], maxDP + (sum[i] - sum[x - 1]));
                maximize(maxDP, dp[x][j - 1]);
            }
        }

        ll ans = 0;
        FOR(i, 1, n) FOR(j, 1, min(k, i)) maximize(ans, dp[i][j]);

        cout << ans << "\n";
    }
}

namespace sub6 {
    const int N = (int)2e3 + 3;
    ll dp[N][N][2];

    bool valid() {
        return n <= 2000;
    }

    void solve() {
        memset(dp, -0x3f, sizeof(dp));
        dp[0][0][0] = dp[0][0][1] = 0;
        ll ans = 0;
        FOR(i, 1, n) FOR(j, 1, min(i, k)) {
            dp[i][j][1] = max({1LL * a[i], dp[i - 1][j][1] + a[i], dp[i - 1][j - 1][0] + a[i]});
            dp[i][j][0] = max(dp[i - 1][j][0], dp[i][j][1]);
            maximize(ans, max({dp[i][j][0], dp[i][j][1]}));
            //cout << i << " " << j << "  " << dp[i][j][0] << " " << dp[i][j][1] << "\n";
        }
        cout << ans << "\n";
    }
}

void main_code() {
    cin >> n >> k;
    FOR(i, 1, n) cin >> a[i];

    if (sub3::valid()) sub3::solve();
    else if (sub1::valid()) sub1::solve();
    else if (sub2::valid()) sub2::solve();
    else if (sub5::valid()) sub5::solve();
    else if (sub6::valid()) sub6::solve();
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    const bool MULTITEST = 0;
    int num_test = 1; if (MULTITEST) cin >> num_test;
    while (num_test--) { main_code(); cout << "\n"; }
}

/* Lak lu theo dieu nhac */

Compilation message

feast.cpp: In function 'int main()':
feast.cpp:146:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  146 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:147:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  147 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 5112 KB Output is correct
2 Correct 21 ms 4944 KB Output is correct
3 Correct 22 ms 5100 KB Output is correct
4 Correct 22 ms 4968 KB Output is correct
5 Correct 20 ms 4944 KB Output is correct
6 Correct 21 ms 4692 KB Output is correct
7 Correct 23 ms 4948 KB Output is correct
8 Correct 22 ms 4984 KB Output is correct
9 Correct 21 ms 4944 KB Output is correct
10 Correct 22 ms 5048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 4184 KB Output is correct
2 Correct 15 ms 4196 KB Output is correct
3 Correct 13 ms 3932 KB Output is correct
4 Correct 15 ms 3932 KB Output is correct
5 Correct 21 ms 4948 KB Output is correct
6 Correct 13 ms 3928 KB Output is correct
7 Correct 14 ms 4156 KB Output is correct
8 Correct 25 ms 5132 KB Output is correct
9 Correct 26 ms 4688 KB Output is correct
10 Correct 24 ms 4188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 5000 KB Output is correct
2 Correct 23 ms 4928 KB Output is correct
3 Correct 23 ms 4944 KB Output is correct
4 Correct 24 ms 5200 KB Output is correct
5 Correct 22 ms 4948 KB Output is correct
6 Correct 25 ms 5068 KB Output is correct
7 Correct 27 ms 5332 KB Output is correct
8 Correct 24 ms 4952 KB Output is correct
9 Correct 23 ms 4944 KB Output is correct
10 Correct 23 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2512 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2512 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 3 ms 2396 KB Output is correct
12 Correct 4 ms 2396 KB Output is correct
13 Correct 2 ms 2396 KB Output is correct
14 Correct 4 ms 2512 KB Output is correct
15 Correct 3 ms 2396 KB Output is correct
16 Correct 2 ms 2396 KB Output is correct
17 Correct 5 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2512 KB Output is correct
6 Correct 1 ms 2648 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 3 ms 2396 KB Output is correct
12 Correct 4 ms 2396 KB Output is correct
13 Correct 2 ms 2396 KB Output is correct
14 Correct 4 ms 2512 KB Output is correct
15 Correct 3 ms 2396 KB Output is correct
16 Correct 2 ms 2396 KB Output is correct
17 Correct 5 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 28 ms 64092 KB Output is correct
22 Correct 32 ms 64212 KB Output is correct
23 Correct 38 ms 64084 KB Output is correct
24 Correct 29 ms 64240 KB Output is correct
25 Correct 26 ms 64076 KB Output is correct
26 Correct 25 ms 64084 KB Output is correct
27 Correct 30 ms 64084 KB Output is correct
28 Correct 30 ms 64168 KB Output is correct
29 Correct 25 ms 64080 KB Output is correct
30 Correct 24 ms 64092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 5112 KB Output is correct
2 Correct 21 ms 4944 KB Output is correct
3 Correct 22 ms 5100 KB Output is correct
4 Correct 22 ms 4968 KB Output is correct
5 Correct 20 ms 4944 KB Output is correct
6 Correct 21 ms 4692 KB Output is correct
7 Correct 23 ms 4948 KB Output is correct
8 Correct 22 ms 4984 KB Output is correct
9 Correct 21 ms 4944 KB Output is correct
10 Correct 22 ms 5048 KB Output is correct
11 Correct 14 ms 4184 KB Output is correct
12 Correct 15 ms 4196 KB Output is correct
13 Correct 13 ms 3932 KB Output is correct
14 Correct 15 ms 3932 KB Output is correct
15 Correct 21 ms 4948 KB Output is correct
16 Correct 13 ms 3928 KB Output is correct
17 Correct 14 ms 4156 KB Output is correct
18 Correct 25 ms 5132 KB Output is correct
19 Correct 26 ms 4688 KB Output is correct
20 Correct 24 ms 4188 KB Output is correct
21 Correct 24 ms 5000 KB Output is correct
22 Correct 23 ms 4928 KB Output is correct
23 Correct 23 ms 4944 KB Output is correct
24 Correct 24 ms 5200 KB Output is correct
25 Correct 22 ms 4948 KB Output is correct
26 Correct 25 ms 5068 KB Output is correct
27 Correct 27 ms 5332 KB Output is correct
28 Correct 24 ms 4952 KB Output is correct
29 Correct 23 ms 4944 KB Output is correct
30 Correct 23 ms 4948 KB Output is correct
31 Correct 1 ms 2396 KB Output is correct
32 Correct 1 ms 2396 KB Output is correct
33 Correct 0 ms 2396 KB Output is correct
34 Correct 1 ms 2396 KB Output is correct
35 Correct 0 ms 2512 KB Output is correct
36 Correct 1 ms 2648 KB Output is correct
37 Correct 1 ms 2396 KB Output is correct
38 Correct 1 ms 2396 KB Output is correct
39 Correct 1 ms 2396 KB Output is correct
40 Correct 0 ms 2396 KB Output is correct
41 Correct 3 ms 2396 KB Output is correct
42 Correct 4 ms 2396 KB Output is correct
43 Correct 2 ms 2396 KB Output is correct
44 Correct 4 ms 2512 KB Output is correct
45 Correct 3 ms 2396 KB Output is correct
46 Correct 2 ms 2396 KB Output is correct
47 Correct 5 ms 2396 KB Output is correct
48 Correct 1 ms 2396 KB Output is correct
49 Correct 1 ms 2396 KB Output is correct
50 Correct 1 ms 2392 KB Output is correct
51 Correct 28 ms 64092 KB Output is correct
52 Correct 32 ms 64212 KB Output is correct
53 Correct 38 ms 64084 KB Output is correct
54 Correct 29 ms 64240 KB Output is correct
55 Correct 26 ms 64076 KB Output is correct
56 Correct 25 ms 64084 KB Output is correct
57 Correct 30 ms 64084 KB Output is correct
58 Correct 30 ms 64168 KB Output is correct
59 Correct 25 ms 64080 KB Output is correct
60 Correct 24 ms 64092 KB Output is correct
61 Incorrect 23 ms 6204 KB Output isn't correct
62 Halted 0 ms 0 KB -