# include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
int n, a[N], k, dp[102][N];
int stk[N], pt, pf[N];
int main(){
cin >> n >> k;
for(int i = 1; i <= n; i ++)
cin >> a[i];
memset(dp, 0x3f3f3f3f, sizeof(dp));
dp[1][1] = a[1];
for(int i = 2; i <= n; i ++){
dp[1][i] = max(dp[1][i - 1], a[i]);
}
for(int i = 2; i <= k; i ++){
pt = 0;
for(int j = 0; j <= n; j ++)
pf[j] = 1e9;
for(int j = i; j <= n; j ++){
while(pt >= 1 && a[ stk[pt] ] <= a[j])
pt --;
stk[++ pt] = j;
pf[pt] = dp[i - 1][ max(stk[pt - 1] + 1, i - 1) ] + a[j];
if(pt > 1)
pf[pt] = min(pf[pt], pf[pt - 1]);
dp[i][j] = pf[pt];
}
}
cout << dp[k][n] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
40312 KB |
Output is correct |
2 |
Correct |
35 ms |
40184 KB |
Output is correct |
3 |
Correct |
35 ms |
40184 KB |
Output is correct |
4 |
Correct |
35 ms |
40312 KB |
Output is correct |
5 |
Correct |
35 ms |
40184 KB |
Output is correct |
6 |
Correct |
36 ms |
40440 KB |
Output is correct |
7 |
Correct |
35 ms |
40228 KB |
Output is correct |
8 |
Correct |
36 ms |
40312 KB |
Output is correct |
9 |
Correct |
35 ms |
40312 KB |
Output is correct |
10 |
Incorrect |
35 ms |
40312 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
40268 KB |
Output is correct |
2 |
Correct |
35 ms |
40188 KB |
Output is correct |
3 |
Correct |
35 ms |
40404 KB |
Output is correct |
4 |
Correct |
35 ms |
40284 KB |
Output is correct |
5 |
Correct |
39 ms |
40276 KB |
Output is correct |
6 |
Correct |
41 ms |
40312 KB |
Output is correct |
7 |
Correct |
42 ms |
40312 KB |
Output is correct |
8 |
Correct |
35 ms |
40236 KB |
Output is correct |
9 |
Correct |
36 ms |
40312 KB |
Output is correct |
10 |
Incorrect |
35 ms |
40312 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
40388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
40312 KB |
Output is correct |
2 |
Correct |
60 ms |
40696 KB |
Output is correct |
3 |
Correct |
63 ms |
40952 KB |
Output is correct |
4 |
Incorrect |
95 ms |
40824 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |