57902023-09-30 16:28:54zsomborMajomházcpp17Time limit exceeded 40/1003.082s10220 KiB
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
using ll = long long;

int n, k;
vector <ll> a(1e5 + 1, 0);
vector <ll> p(1e5 + 1, 0);
vector <ll> dp(1e5 + 1, 0);
vector <ll> cnt(1e5 + 1, -1);

ll sum(int l, int r) {
    return p[r] - p[l - 1];
}

pair <ll, ll> do_dp(ll lam) {
    dp[0] = -lam;
    int last = 0;
    for (int i = 1; i <= n; i++) {
        dp[i] = 1e18;
        for (int j = last; j < i; j++) {
            ll cur = dp[j] + lam + sum(j + 1, i) * (i - j);
            if (cur > dp[i]) break;
            dp[i] = cur;
            cnt[i] = cnt[j] + 1;
            last = j;
        }
    }
    return {cnt[n], dp[n]};
}

pair <ll, ll> do_dp2(ll lam) {
    dp[0] = -lam;
    int last = 0;
    for (int i = 1; i <= n; i++) {
        dp[i] = 1e18;
        for (int j = 0; j < i; j++) {
            ll cur = dp[j] + lam + sum(j + 1, i) * (i - j);
            if (cur<=dp[i]){
                dp[i]=cur;
                cnt[i]=cnt[j]+1;
            }
        }
    }
    return {cnt[n], dp[n]};
}

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

    /*fstream fajl;
    fajl.open("be2.txt");
    fajl >> n >> k;
    for (int i = 1; i <= n; i++) {
        fajl >> a[i];
        p[i] = p[i - 1] + a[i];
    }
    fajl.close();*/

    ll l = 0, r = 1e18, m;
    while (r - l > 1) {
        m = (l + r) / 2;
        (do_dp2(m).first >= k ? l = m : r = m);
    }
    cout << do_dp2(l).second - k * l;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms7848 KiB
2Accepted1.937s8012 KiB
subtask210/10
3Accepted4ms8372 KiB
4Accepted4ms8412 KiB
5Accepted4ms8668 KiB
6Accepted4ms8884 KiB
7Accepted4ms9168 KiB
subtask310/10
8Accepted28ms9036 KiB
9Accepted28ms9168 KiB
10Accepted29ms9456 KiB
11Accepted28ms9644 KiB
12Accepted28ms9652 KiB
subtask420/20
13Accepted2.115s9780 KiB
14Accepted2.266s9960 KiB
15Accepted2.388s9740 KiB
16Accepted2.477s10004 KiB
17Accepted2.493s9948 KiB
18Accepted2.502s10220 KiB
subtask50/29
19Time limit exceeded3.061s6524 KiB
20Time limit exceeded3.065s6476 KiB
21Time limit exceeded3.061s6476 KiB
22Time limit exceeded3.073s6692 KiB
23Time limit exceeded3.065s6680 KiB
subtask60/31
24Time limit exceeded3.081s6688 KiB
25Time limit exceeded3.078s6920 KiB
26Time limit exceeded3.078s6900 KiB
27Time limit exceeded3.082s6864 KiB
28Time limit exceeded3.058s6908 KiB
29Time limit exceeded3.076s7040 KiB
30Time limit exceeded3.049s7324 KiB
31Time limit exceeded3.065s7220 KiB
32Time limit exceeded3.056s7084 KiB
33Time limit exceeded3.062s7064 KiB
34Time limit exceeded3.053s7268 KiB
35Time limit exceeded3.042s7328 KiB
36Time limit exceeded3.076s7356 KiB
37Time limit exceeded3.065s7412 KiB
38Time limit exceeded3.069s7416 KiB
39Time limit exceeded3.025s7328 KiB
40Time limit exceeded3.061s7380 KiB