#include<bits/stdc++.h>
using namespace std;
int main(){
// freopen("snakes.in", "r", stdin);
// freopen("snakes.out", "w", stdout);
int n, k;
cin >> n >> k;
int snakes[n + 1];
for(int i = 1;i <= n;i++) cin >> snakes[i];
int dp[n + 1][k + 1];
int max1 = -1;
int tot = 0;
for(int i = 1;i <= n;i++){
max1 = max(max1, snakes[i]);
dp[i][0] = max1 * i; // bat i con ma khong doi size lan nao
for(int j = 1;j <= k;j++){
dp[i][j] = INT_MAX;
int max2 = snakes[i];
// doi size khi bat den con thu l
for(int l = i - 1;l >= 0;l--){
dp[i][j] = min(dp[i][j], dp[l][j - 1] + (i - l) * max2);
max2 = max(max2, snakes[l]);
}
}
tot += snakes[i];
}
cout << dp[n][k] - tot << endl;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IG1haW4oKXsKCQovLwlmcmVvcGVuKCJzbmFrZXMuaW4iLCAiciIsIHN0ZGluKTsKLy8JZnJlb3Blbigic25ha2VzLm91dCIsICJ3Iiwgc3Rkb3V0KTsKCQoJaW50IG4sIGs7CgkKCWNpbiA+PiBuID4+IGs7CgkKCWludCBzbmFrZXNbbiArIDFdOwoJCglmb3IoaW50IGkgPSAxO2kgPD0gbjtpKyspIGNpbiA+PiBzbmFrZXNbaV07CgkKCWludCBkcFtuICsgMV1bayArIDFdOwoJCglpbnQgbWF4MSA9IC0xOyAgCgkKCWludCB0b3QgPSAwOyAgCgkKCWZvcihpbnQgaSA9IDE7aSA8PSBuO2krKyl7CgkJCgkJbWF4MSA9IG1heChtYXgxLCBzbmFrZXNbaV0pOyAgCgkJCgkJZHBbaV1bMF0gPSBtYXgxICogaTsgLy8gYmF0IGkgY29uIG1hIGtob25nIGRvaSBzaXplIGxhbiBuYW8gIAoJCQoJCWZvcihpbnQgaiA9IDE7aiA8PSBrO2orKyl7CgkJCWRwW2ldW2pdID0gSU5UX01BWDsgCgkJCWludCBtYXgyID0gc25ha2VzW2ld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