Submission #697889

#TimeUsernameProblemLanguageResultExecution timeMemory
697889bebraSplit the sequence (APIO14_sequence)C++17
28 / 100
2094 ms82884 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define dbg(x) cerr << #x << ": " << x << endl;



const int MAX_N = 1e5 + 1;
const int MAX_K = 200 + 1;
ll dp[MAX_N][MAX_K];
int p[MAX_N][MAX_K];
ll a[MAX_N];
ll pref[MAX_N];


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k;
    cin >> n >> k;
    ll total_sum = 0;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        pref[i] = pref[i - 1] + a[i];
        total_sum += a[i];
    }

    for (int j = 1; j <= k; ++j) {
        for (int i = 1; i <= n; ++i) {
            for (int d = 0; d < i; ++d) {
                ll s = total_sum - pref[d];
                ll x = pref[i] - pref[d];
                ll curr_cost = dp[d][j - 1] + x * (s - x);
                if (curr_cost >= dp[i][j]) {
                    dp[i][j] = curr_cost;
                    p[i][j] = d;
                }
            }
        }
    }

    ll ans = 0;
    int opt = -1;
    for (int i = 1; i <= n; ++i) {
        if (dp[i][k] >= ans) {
            ans = dp[i][k];
            opt = i;
        }
    }
    vector<int> splits;
    int curr_i = opt;
    int curr_k = k;
    while (curr_k > 0) {
        splits.push_back(curr_i);
        curr_i = p[curr_i][curr_k];
        --curr_k;
    }
    reverse(splits.begin(), splits.end());
    cout << ans << '\n';
    for (auto x : splits) {
        cout << x << ' ';
    }
    cout << '\n';
    
    return 0;
}

/*
7 3
4 1 3 4 0 2 3
*/
#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...