This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = a; i <= b; i++)
const int maxn = 1e5 + 7;
const int maxk = 1e2 + 7;
int dp[maxn][maxk];  // tổng trọng số nhỏ nhất xét đến vt thứ i đã sử dụng ít hơn k cách chia nhóm
int n, a[maxn], k;
int f[maxn][20];
int getMax(int l, int r) {
    int j = log2(r - l + 1);
    return max(f[l][j], f[r - (1 << j) + 1][j]);
}
int main() {
    
    cin >> n >> k;
    FOR(i, 1, n) {
        cin >> a[i];
        f[i][0] = a[i];
    }
    for (int j = 1; (1 << j) <= n; j++) {
        for (int i = 1; i + (1 << j) - 1 <= n; i++) {
            f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
        }
    }
    memset(dp, 0x3f, sizeof dp);
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= k; j++) {
            for (int m = 1; m <= i; m++) {
                dp[i][j] = min(dp[i][j], dp[i - m][j - 1] + getMax(i - m + 1, i));
            }
        }
    }
    cout << dp[n][k] << endl;
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |