30642023-02-12 08:22:29CattMajomházcpp17Wrong answer 0/100633ms521904 KiB
#include <bits/stdc++.h>
using namespace std;
#define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using ll = long long;

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


int main() {

   InTheNameOfGod;

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

    vector<int> v(n), pref(n+1, 0);
    for(int &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(int 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
1Accepted3ms1828 KiB
2Wrong answer6ms5596 KiB
subtask20/10
3Wrong answer3ms2140 KiB
4Wrong answer3ms2268 KiB
5Wrong answer3ms2452 KiB
6Wrong answer3ms2668 KiB
7Wrong answer3ms3008 KiB
subtask30/10
8Wrong answer3ms3240 KiB
9Wrong answer3ms3360 KiB
10Wrong answer3ms3792 KiB
11Wrong answer3ms4156 KiB
12Wrong answer3ms4908 KiB
subtask40/20
13Wrong answer4ms5104 KiB
14Wrong answer4ms5512 KiB
15Wrong answer4ms6088 KiB
16Wrong answer8ms10892 KiB
17Wrong answer14ms20020 KiB
18Wrong answer75ms83052 KiB
subtask50/29
19Wrong answer20ms21784 KiB
20Wrong answer48ms37684 KiB
21Wrong answer104ms90740 KiB
22Wrong answer182ms169200 KiB
23Wrong answer356ms325792 KiB
subtask60/31
24Wrong answer34ms28016 KiB
25Wrong answer34ms28132 KiB
26Wrong answer35ms28244 KiB
27Wrong answer37ms34228 KiB
28Wrong answer37ms34160 KiB
29Wrong answer41ms40552 KiB
30Wrong answer56ms52972 KiB
31Wrong answer136ms103108 KiB
32Wrong answer345ms178404 KiB
33Wrong answer633ms334788 KiB
34Runtime error190ms521904 KiB
35Runtime error188ms521880 KiB
36Runtime error233ms521852 KiB
37Runtime error247ms521828 KiB
38Runtime error203ms521776 KiB
39Runtime error197ms521704 KiB
40Runtime error200ms521688 KiB