#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, mn = oo, a[N];
bool vis[101][101];
int dp[101][101];
int mx[101][101];
int pre[101];
void build() {
pre[0] = 0;
for (int i = 1; i <= n; i++) {
mx[i][i] = a[i];
pre[i] = pre[i - 1] + a[i];
for (int j = i + 1; j <= n; j++) {
mx[i][j] = max(mx[i][j - 1], a[j]);
}
}
}
int solve(int i, int j) {
if (i == j) return pre[i];
if (i > j) return oo;
if (j == 1) return mx[1][i];
if (vis[i][j]) return dp[i][j];
vis[i][j] = 1;
dp[i][j] = oo;
for (int x = i - 1; x >= 0; x--) {
dp[i][j] = min(dp[i][j], solve(x, j - 1) + mx[x + 1][i]);
}
return dp[i][j];
}
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];
}
build();
cout << solve(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... |