#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
using namespace std;
const int N = 100005;
ll n, k, amax = 0;
ll a[N + 2], dp[N + 2][150], l[N + 2];
stack <int> s;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
//freopen("blocks.inp", "r", stdin);
//freopen("blocks.out", "w", stdout);
cin >> n >> k;
for(int i = 1; i <= n; i++) {
cin >> a[i];
dp[i][1] = max(dp[i - 1][1], a[i]);
}
s.push(a[1]);
for(int i = 2; i <= n; i++)
{
while(!s.empty() && a[s.top()] < a[i])
s.pop();
if(s.empty())
l[i] = i;
else
l[i] = s.top();
s.push(i);
}
for(int i = 1; i <= n; i++)
{
for(int d = 2; d <= k; d++)
dp[i][d] = INT_MAX;
}
for(int d = 2; d <= k; d++)
{
for(int i = d; i <= n; i++)
{
amax = a[i];
if(l[i] == i)
for(int j = i - 1; j >= d - 1; j--)
dp[i][d] = min(dp[i][d], dp[j][d - 1] + a[i]);
else {
for(int j = i - 1; j >= l[i]; j--) {
dp[i][d] = min(dp[i][d], dp[j][d - 1] + amax);
amax = max(amax, a[j]);
}
}
}
}
cout << dp[n][k];
return 0;
}
/*
5 2
1 2 3 4 5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
2 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
2 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
2 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Runtime error |
2 ms |
604 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |