Submission #497781

#TimeUsernameProblemLanguageResultExecution timeMemory
497781StickfishSplit the sequence (APIO14_sequence)C++17
50 / 100
177 ms3436 KiB
#include <iostream>
using namespace std;
using ll = long long;

const ll INF = 1.77013e18;
const int MAXN = 1000;
const int MAXK = 202;
ll a[MAXN];
ll dp[MAXK][MAXN];
ll opt[MAXK][MAXN];

signed main() {
    int n, k;
    cin >> n >> k;
    ++k;
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    ll sm = 0;
    ll sqsm = 0;
    for (int i = 0; i < n; ++i) {
        sm += a[i];
        sqsm += a[i] * a[i];
        dp[0][i] = (sm * sm - sqsm) / 2;
        opt[0][i] = -1;
    }
    for (int t = 1; t < k; ++t) {
        for (int i = 0; i < n; ++i) {
            sm = a[i]; 
            sqsm = a[i] * a[i];
            dp[t][i] = INF;
            for (int j = i - 1; j >= 0; --j) {
                if (dp[t][i] > dp[t - 1][j] + (sm * sm - sqsm) / 2) {
                    dp[t][i] = dp[t - 1][j] + (sm * sm - sqsm) / 2;
                    opt[t][i] = j;
                }
                sm += a[j];
                sqsm += a[j] * a[j];
            }
        }
    }
    //for (int t = 0; t < k; ++t) {
        //for (int i = 0; i < n; ++i)
            //cout << dp[t][i] << ' ';
        //cout << endl;
    //}
    ll ans = 0;
    for (int i = 0; i < n; ++i)
        ans += a[i];
    ans *= ans;
    for (int i = 0; i < n; ++i)
        ans -= a[i] * a[i];
    ans /= 2;
    ans -= dp[k - 1][n - 1];
    cout << ans << endl;
    int i = n - 1;
    int t = k;
    while (--t) {
        i = opt[t][i];
        if (i != -1) {
            cout << i + 1 << ' ';
        }
    }
    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...