Submission #516983

# Submission time Handle Problem Language Result Execution time Memory
516983 2022-01-22T10:21:28 Z shrimb Split the sequence (APIO14_sequence) C++17
39 / 100
2000 ms 63532 KB
#include"bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'

int dp[10001][201];
int dp2[10001][201];
int best[10001][201];
int best2[10001][201];

signed main() {
    int n, k;
    cin >> n >> k;
    assert(k < n);

    int a[n];
    int pref[n];
    int suff[n];
    for (int i = 0 ; i < n ; i++) {
        cin >> a[i];
        if (i) pref[i] = pref[i-1];
        else pref[i] = 0;
        pref[i] += a[i];
    }
    for (int i = n - 1 ; i >=  0 ;i--) suff[i] = (i < n - 1 ? suff[i + 1] : 0) + a[i];
    int mx1 = 0;
    for (int j = 0 ; j < k ; j++) {
        for (int i = 0 ; i < n ; i++) {
            if (j == 0) {
                if (j == k - 1) dp[i][j] = (i < n - 1 ? suff[i + 1] : 0) * pref[i];
                else dp[i][j] = 0;
                mx1 = max(mx1, dp[i][j]);
                continue;
            }
            dp[i][j] = -1e15;
            for (int l = 0 ; l < i ; l++) {
                int prod = pref[l] * (pref[i] - pref[l]);
                int v = dp[l][j-1] + prod;
                if (j == k - 1) v += (i < n - 1 ? suff[i + 1] : 0) * pref[i];
                if (v > dp[i][j]) {
                    dp[i][j] = v;
                    best[i][j] = l;
                }
            }
            mx1 = max(mx1, dp[i][j]);
        }
    }
    int mx2 = 0;
    for (int j = 0 ; j < k ; j++) {
        for (int i = n-1 ; i >= 0 ; i--) {
            if (j == 0) {
                if (j == k - 1) {
                    dp[i][j] = suff[i] * (i ? pref[i-1] : 0);
                }
                else dp2[i][j] = 0;
                mx2 = max(mx2, dp2[i][j]);
                continue;
            }
            dp2[i][j] = -1e15;
            for (int l = n-1 ; l > i ; l--) {
                int prod = suff[l] * (suff[i] - suff[l]);
                int v = dp2[l][j-1] + prod;
                if (j == k - 1) v += suff[i] * (i ? pref[i-1] : 0);
                if (v > dp2[i][j]) {
                    dp2[i][j] = v;
                    best2[i][j] = l;
                }
            }
            mx2 = max(mx2, dp2[i][j]);
        }
    }
    // for (int i = 0 ; i < n ; i++) {
    //     for (int j = 0 ; j < k ; j++) {
    //         cout << dp2[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    bool tag = 0;
    if (mx2 > mx1) {tag = 1;swap(mx2, mx1);swap(dp, dp2);swap(best, best2);}

    // cerr << tag << endl;

    int cur = 0;
    int kk = k - 1;
    for (int i = 0 ; i < n-1 ; i++) {
        if (dp[i][kk] == mx1) cur = i;
    }
    cout << mx1 << "\n";
    vector<int> v;
    while (kk >= 0) {
        v.push_back(cur + 1 - tag);
        cur = best[cur][kk--];
    }
    reverse(v.begin(), v.end());
    for (int i : v) cout << i << " ";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB contestant found the optimal answer: 108 == 108
2 Correct 0 ms 332 KB contestant found the optimal answer: 999 == 999
3 Correct 0 ms 204 KB contestant found the optimal answer: 0 == 0
4 Correct 0 ms 332 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 1 ms 332 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Incorrect 0 ms 204 KB declared answer doesn't correspond to the split scheme: declared = 1, real = 0
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 588 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 1 ms 588 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 588 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 0 ms 588 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 1 ms 588 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 588 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 0 ms 588 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 588 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 1 ms 588 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1484 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 1484 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 11 ms 1484 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 1484 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 9 ms 1504 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 10 ms 1580 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 10 ms 1484 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 4 ms 1484 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 2 ms 1484 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 5 ms 1484 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 6 ms 6552 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 5 ms 6604 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 255 ms 6660 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 6 ms 6604 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 337 ms 6612 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 279 ms 6660 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 250 ms 6612 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 247 ms 6596 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 73 ms 6528 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 152 ms 6528 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 756 ms 63532 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 805 ms 63444 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Execution timed out 2092 ms 31932 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 37 ms 37064 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -