30652023-02-12 08:24:30CattMajomházcpp17Wrong answer 0/100685ms522988 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int maxN = 2e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;


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

    vector<ll> v(n), pref(n+1, 0);
    for(ll &i : v) cin >> i;
    for(int i = 1; i <= n; i++) {
        pref[i] = pref[i-1] + v[i-1];
    }

    vector<vector<ll>> dp(n, vector<ll>(k+1, INF)), prev(n, vector<ll>(k+1, 0));
    dp[0][0] = v[0];
    for(ll i = 1; i < n; i++) {
        dp[i][0] = dp[i-1][0] + pref[i+1] + (i) * v[i];
    }
    for(int i = 0; i <= k; i++) {
        dp[0][i] = v[0];
    }
    for(int j = 1; j <= k; j++) {
        for(ll i = 1; i < n; i++) {
            ll val1 = dp[i-1][j-1] + v[i];
            ll val2 = dp[i-1][j] + pref[i+1] - pref[prev[i-1][j]] + v[i] * (i- prev[i-1][j]);

            if(val1 < val2) {
                dp[i][j] = val1;
                prev[i][j] = i;
            }
            else {
                dp[i][j] = val2;
                prev[i][j] = prev[i-1][j];
            }
        }
    }

    cout << dp[n-1][k] << "\n";
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1812 KiB
2Wrong answer7ms5872 KiB
subtask20/10
3Wrong answer3ms2216 KiB
4Wrong answer3ms2428 KiB
5Wrong answer3ms2676 KiB
6Wrong answer3ms2760 KiB
7Wrong answer3ms2832 KiB
subtask30/10
8Wrong answer3ms2924 KiB
9Wrong answer3ms3232 KiB
10Wrong answer3ms3400 KiB
11Wrong answer3ms3752 KiB
12Wrong answer4ms4812 KiB
subtask40/20
13Wrong answer6ms4900 KiB
14Wrong answer6ms5296 KiB
15Wrong answer6ms5840 KiB
16Wrong answer9ms10612 KiB
17Wrong answer17ms20020 KiB
18Wrong answer90ms82812 KiB
subtask50/29
19Wrong answer35ms22168 KiB
20Wrong answer61ms37692 KiB
21Wrong answer158ms91140 KiB
22Wrong answer256ms169444 KiB
23Wrong answer384ms325836 KiB
subtask60/31
24Accepted59ms28872 KiB
25Wrong answer59ms28804 KiB
26Wrong answer59ms28800 KiB
27Wrong answer64ms34908 KiB
28Wrong answer63ms34948 KiB
29Wrong answer71ms41136 KiB
30Wrong answer94ms53624 KiB
31Wrong answer214ms103788 KiB
32Wrong answer381ms178912 KiB
33Wrong answer685ms335264 KiB
34Runtime error259ms522988 KiB
35Runtime error263ms522956 KiB
36Runtime error256ms522936 KiB
37Runtime error221ms522696 KiB
38Runtime error270ms522676 KiB
39Runtime error273ms522648 KiB
40Runtime error222ms522620 KiB