Submission #134914

#TimeUsernameProblemLanguageResultExecution timeMemory
134914fredbrSplit the sequence (APIO14_sequence)C++17
50 / 100
219 ms3960 KiB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int const maxn = 1010;
int const maxk = 210;
ll const inf = 0x3f3f3f3f3f3f3f3f;

ll p[maxn];
ll dp[maxk][maxn], opt[maxk][maxn];

ll solve(int k, int n) {
    if (dp[k][n] >= 0) return dp[k][n];
    
    if (k == 0) return 0;
    if (n == 1) return -inf;

    ll& d = dp[k][n];
    ll& op = opt[k][n];

    d = -inf, op = 1;

    for (int i = k; i < n; i++) {
        ll cost = p[i] * (p[n]-p[i]) + solve(k-1, i);
        
        if (cost > d) {
            op = i;
            d = cost;
        }
    }

    return d;
}

vector<ll> recover(int k, int n) {
    vector<ll> res;

    while (k != 0) {
        res.push_back(opt[k][n]);
        n = opt[k][n];
        k--;
    }

    reverse(res.begin(), res.end());

    return res;
}

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

    memset(dp, -1, sizeof(dp));

    int n, k;
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        p[i] += p[i-1];
    }

    ll ans = solve(k, n);

    auto res = recover(k, n);

    cout << ans << "\n";

    for (ll i : res) cout << i << " ";
    cout << "\n";
}
#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...