Submission #1292985

#TimeUsernameProblemLanguageResultExecution timeMemory
1292985kawhietK blocks (IZhO14_blocks)C++20
53 / 100
1096 ms2880 KiB
#include <bits/stdc++.h>
using namespace std;

vector<int> st, a;

void build(int id, int tl, int tr) {
    if (tl == tr) {
        st[id] = a[tl];
        return;
    }
    int x = (id << 1) + 1, y = x + 1, tm = (tl + tr) >> 1;
    build(x, tl, tm);
    build(y, tm + 1, tr);
    st[id] = max(st[x], st[y]);
}

int query(int id, int tl, int tr, int l, int r) {
    if (r < tl || tr < l) return 0;
    if (l <= tl && tr <= r) return st[id];
    int x = (id << 1) + 1, y = x + 1, tm = (tl + tr) >> 1;
    return max(query(x, tl, tm, l, r), query(y, tm + 1, tr, l, r));
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, k;
    cin >> n >> k;
    a.resize(n + 1, 0);
    st.assign(4 * n + 10, 0);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    build(0, 0, n);
    vector<vector<int64_t>> dp(n + 1, vector<int64_t>(k + 1, 1e18));
    dp[0][0] = 0;
    dp[1][1] = a[1];
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= min(i, k); j++) {
            for (int r = j - 1; r <= i - 1; r++) {
                dp[i][j] = min(dp[i][j], dp[r][j - 1] + query(0, 0, n, r + 1, i));
            }
        }
    }
    cout << dp[n][k] << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...