Submission #997449

#TimeUsernameProblemLanguageResultExecution timeMemory
997449vjudge1Split the sequence (APIO14_sequence)C++17
71 / 100
378 ms32852 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll N = 1e4 + 100;
const ll K = 205;

ll n, k, a[N], dp[N][K], par[N][K], suff[N];

int main(){
    cin >> n >> k;
    k++;

    for (ll i = 1; i <= n; i ++)
        cin >> a[i];
    for (ll i = n; i > 0; i --)
        suff[i] = suff[i + 1] + a[i];

    for (ll i = n; i > 0; i --){
        for (ll p = 2; p <= k; p ++){
            if (i + p - 1 > n){
                dp[i][p] = -1e18;
                continue;
            }
            dp[i][p] = -1;
            if (p == 2){
                for (ll j = i + 1; j <= n; j ++){
                    ll val = dp[j][p - 1] + (suff[i] - suff[j]) * suff[j];
                    if (val > dp[i][p]){
                        dp[i][p] = val;
                        par[i][p] = j;
                    }
                }
            }
            else{
                for (ll j = par[i][p - 1]; j > i; j --){
                    ll val = dp[j][p - 1] + (suff[i] - suff[j]) * suff[j];
                    if (val > dp[i][p]){
                        dp[i][p] = val;
                        par[i][p] = j;
                    }
                }
            }
        }
    }

    cout << dp[1][k] << endl;

    ll cur_i = 1;
    ll cur_k = k;

    while (cur_k > 1){
        cout << par[cur_i][cur_k] - 1 << " ";
        cur_i = par[cur_i][cur_k];
        cur_k--;
    }
    cout << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...