#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define pb push_back
#define meta int tm = (tl + tr) / 2, x = i * 2 + 1, y = x + 1
const int N = 2e5 + 7;
const int oo = 1e9;
const int mod = 1e9 + 7;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
int n, k, a[N], pre[N], dp[107][N], mx;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pre[i] = pre[i - 1] + a[i];
}
for (int i = 1; i <= k; i++) {
mx = a[i];
dp[i][i] = pre[i];
for (int j = i + 1; j <= n; j++) {
int x = dp[i - 1][j] + a[j];
int y = dp[i][j - 1] - ((mx <= a[j]) * (mx - a[j]));
if (x < y) mx = a[j];
else mx = max (mx, a[j]);
dp[i][j] = min (x, y);
mx = max (mx, a[j]);
}
}
cout << dp[k][n];
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... |