Submission #1134224

#TimeUsernameProblemLanguageResultExecution timeMemory
1134224lopkusSplit the sequence (APIO14_sequence)C++20
0 / 100
1 ms328 KiB
#include <bits/stdc++.h>

#define int long long

using namespace std;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n + 1);
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    int dp[n + 1][k + 1];
    for(int i = 0; i <= n; i++) {
        for(int j = 0; j <= k; j++) {
            dp[i][j] = 0;
        }
    }
    vector<int> pref(n + 1, 0);
    for(int i = 1; i <= n; i++) {
        pref[i] = pref[i - 1] + a[i];
    }
    int prev[n + 1][k + 1];
    for(int i = 0; i <= n; i++) {
        for(int j = 0 ;j <= k; j++) {
            prev[i][j] = 0;
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= k; j++) {
            for(int x = i; x >= 1; x--) {
                //dp[i][j] = max(dp[i][j], dp[x - 1][j - 1] + (pref[i] - pref[x - 1]) * (pref[n] - pref[i]));
                if(dp[i][j] < dp[x - 1][j - 1] + (pref[i] - pref[x - 1]) * (pref[n] - pref[i])) {
                    prev[i][j] = x - 1;
                    dp[i][j] = dp[x - 1][j - 1] + (pref[i] - pref[x - 1]) * (pref[n] - pref[i]);
                }
            }
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        ans = max(ans, dp[i][k]);
    }
    int idx = - 1;
    for(int i = 1; i <= n; i++) {
        if(dp[i][k] == ans) {
            idx = i;
        }
    }
    vector<int> sol;
    while(idx > 0) {
        sol.push_back(idx);
        idx = prev[idx][k--];
    }
    reverse(sol.begin(), sol.end());
    cout << ans << "\n";
    for(auto it : sol) {
        cout << it << " ";
    }
}

#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...