#include <bits/stdc++.h>
using namespace std ;
int readInt ()
{
bool minus = false;
int result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-') minus = true; else result = ch-'0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = result*10 + (ch - '0');
}
if (minus)
return -result;
else
return result;
}
int main()
{
int n , k ;
n = readInt() ;
k = readInt() ;
int arr[n+1] ;
for(int i = 1 ; i <= n ; ++i)
arr[i] = readInt() ;
int dp[n+1][k+1] ;
int chosen[n+1][k+1] ;
dp[0][1] = 0 ;
for(int i = 1 ; i <= n ; ++i)
dp[i][1] = max(arr[i] , dp[i-1][1]) , chosen[i][1] = dp[i][1];
for(int j = 2 ; j <= k ; ++j)
{
stack<int>s ;
for(int i = j ; i <= n ; ++i)
{
dp[i][j] = dp[i-1][j-1] + arr[i] ;
chosen[i][j] = arr[i] ;
while(s.size() > 0)
{
int idx = s.top() ;
if(arr[idx] <= arr[i])
{
if(dp[idx][j] - chosen[idx][j] + max(chosen[idx][j] , arr[i]) < dp[i][j])
{
dp[i][j] = dp[idx][j] - chosen[idx][j] + max(chosen[idx][j] , arr[i]) ;
}
s.pop() ;
}
else
break ;
}
if(s.size() > 0)
{
int idx = s.top() ;
if(dp[idx][j] < dp[i][j])
{
dp[i][j] = dp[idx][j] ;
chosen[i][j] = chosen[idx][j] ;
}
}
s.push(i) ;
}
}
printf("%d" , dp[n][k]) ;
return 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
412 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
316 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
356 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
3 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2432 KB |
Output is correct |
2 |
Correct |
7 ms |
1152 KB |
Output is correct |
3 |
Correct |
16 ms |
3200 KB |
Output is correct |
4 |
Correct |
282 ms |
27384 KB |
Output is correct |
5 |
Correct |
719 ms |
79864 KB |
Output is correct |
6 |
Correct |
14 ms |
3788 KB |
Output is correct |
7 |
Correct |
285 ms |
36736 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
53 ms |
8192 KB |
Output is correct |
15 |
Correct |
4 ms |
640 KB |
Output is correct |
16 |
Correct |
11 ms |
2432 KB |
Output is correct |
17 |
Correct |
5 ms |
1152 KB |
Output is correct |
18 |
Correct |
14 ms |
3328 KB |
Output is correct |
19 |
Correct |
296 ms |
27384 KB |
Output is correct |
20 |
Correct |
823 ms |
79864 KB |
Output is correct |
21 |
Correct |
17 ms |
3840 KB |
Output is correct |
22 |
Correct |
283 ms |
36752 KB |
Output is correct |
23 |
Correct |
9 ms |
2304 KB |
Output is correct |
24 |
Correct |
37 ms |
9344 KB |
Output is correct |
25 |
Correct |
779 ms |
79808 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
30 ms |
8320 KB |
Output is correct |
29 |
Correct |
3 ms |
640 KB |
Output is correct |
30 |
Correct |
8 ms |
2432 KB |
Output is correct |
31 |
Correct |
5 ms |
1152 KB |
Output is correct |
32 |
Correct |
12 ms |
3328 KB |
Output is correct |
33 |
Correct |
227 ms |
27408 KB |
Output is correct |
34 |
Correct |
758 ms |
80008 KB |
Output is correct |
35 |
Correct |
21 ms |
3968 KB |
Output is correct |
36 |
Correct |
302 ms |
36932 KB |
Output is correct |
37 |
Correct |
3 ms |
512 KB |
Output is correct |
38 |
Correct |
29 ms |
8320 KB |
Output is correct |
39 |
Correct |
3 ms |
384 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |