Submission #130111

#TimeUsernameProblemLanguageResultExecution timeMemory
130111mlyean00수열 (APIO14_sequence)C++14
50 / 100
2041 ms18584 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    vector<ll> pref(n, 0);
    partial_sum(a.begin(), a.end(), pref.begin());

    ll dp[k + 1][n], parent[k + 1][n];
    memset(dp, 0xff, sizeof(dp));

    for (int j = 0; j < n; ++j) {
        dp[0][j] = 0;
    }
    for (int i = 1; i <= k; ++i) {
        for (int j = 0; j < n; ++j) {
            dp[i][j] = LLONG_MIN;
        }
    }

    for (int i = 1; i <= k; ++i) {
        for (int j = 0; j < n; ++j) {
            for (int h = 0; h < j; ++h) {
                ll t = dp[i - 1][h] + pref[h] * (pref[j] - pref[h]);
                if (t >= dp[i][j]) {
                    dp[i][j] = t;
                    parent[i][j] = h;
                }
            }
        }
    }

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

    int u = n - 1;
    for (int i = k; i > 0; --i) {
        u = parent[i][u];
        cout << (u + 1) << ' ';
    }
    cout << endl;

    return 0;
}
#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...