#include <bits/stdc++.h>
using namespace std;
#define s second
#define f first
typedef long long ll;
const int N = 100005, M = 105;
int n, k, a[N], dp[M][N], ch[M][N];
int main()
{
cin >> n >> k;
for(int i=1; i<=n; i++) cin >> a[i];
memset(dp, 0x3f3f3f3f, sizeof dp);
dp[0][0] = 0;
for(int i=1; i<=k; i++)
{
stack<int> s;
for(int j=i; j<=n; j++)
{
dp[i][j] = dp[i-1][j-1]+ a[j], ch[i][j] = a[j];
while(!s.empty())
{
int u = s.top();
if(a[u] <= a[j])
{
if(dp[i][j] > dp[i][u] - ch[i][u] + max(ch[i][u], a[j]))
dp[i][j] = dp[i][u] - ch[i][u] + max(ch[i][u], a[j]), ch[i][j] = max(ch[i][u], a[j]);
}
else break;
s.pop();
}
if(!s.empty())
{
int u = s.top();
if(dp[i][u] < dp[i][j])
{
dp[i][j] = dp[i][u];
ch[i][j] = ch[i][u];
}
}
s.push(j);
}
}
cout << dp[k][n] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
41464 KB |
Output is correct |
2 |
Correct |
33 ms |
41444 KB |
Output is correct |
3 |
Correct |
33 ms |
41440 KB |
Output is correct |
4 |
Correct |
35 ms |
41464 KB |
Output is correct |
5 |
Correct |
33 ms |
41464 KB |
Output is correct |
6 |
Correct |
38 ms |
41464 KB |
Output is correct |
7 |
Correct |
34 ms |
41464 KB |
Output is correct |
8 |
Correct |
34 ms |
41464 KB |
Output is correct |
9 |
Correct |
36 ms |
41464 KB |
Output is correct |
10 |
Correct |
35 ms |
41468 KB |
Output is correct |
11 |
Correct |
42 ms |
41464 KB |
Output is correct |
12 |
Correct |
35 ms |
41512 KB |
Output is correct |
13 |
Correct |
35 ms |
41444 KB |
Output is correct |
14 |
Correct |
37 ms |
41464 KB |
Output is correct |
15 |
Correct |
39 ms |
41460 KB |
Output is correct |
16 |
Correct |
35 ms |
41464 KB |
Output is correct |
17 |
Correct |
35 ms |
41444 KB |
Output is correct |
18 |
Correct |
36 ms |
41464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
41468 KB |
Output is correct |
2 |
Correct |
36 ms |
41472 KB |
Output is correct |
3 |
Correct |
34 ms |
41464 KB |
Output is correct |
4 |
Correct |
33 ms |
41472 KB |
Output is correct |
5 |
Correct |
35 ms |
41524 KB |
Output is correct |
6 |
Correct |
34 ms |
41464 KB |
Output is correct |
7 |
Correct |
34 ms |
41464 KB |
Output is correct |
8 |
Correct |
37 ms |
41472 KB |
Output is correct |
9 |
Correct |
34 ms |
41472 KB |
Output is correct |
10 |
Correct |
35 ms |
41384 KB |
Output is correct |
11 |
Correct |
33 ms |
41436 KB |
Output is correct |
12 |
Correct |
32 ms |
41464 KB |
Output is correct |
13 |
Correct |
36 ms |
41472 KB |
Output is correct |
14 |
Correct |
35 ms |
41464 KB |
Output is correct |
15 |
Correct |
34 ms |
41464 KB |
Output is correct |
16 |
Correct |
35 ms |
41472 KB |
Output is correct |
17 |
Correct |
36 ms |
41464 KB |
Output is correct |
18 |
Correct |
32 ms |
41472 KB |
Output is correct |
19 |
Correct |
34 ms |
41464 KB |
Output is correct |
20 |
Correct |
41 ms |
41592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
41720 KB |
Output is correct |
2 |
Correct |
33 ms |
41464 KB |
Output is correct |
3 |
Correct |
35 ms |
41464 KB |
Output is correct |
4 |
Correct |
39 ms |
41592 KB |
Output is correct |
5 |
Correct |
42 ms |
41460 KB |
Output is correct |
6 |
Correct |
41 ms |
41464 KB |
Output is correct |
7 |
Correct |
36 ms |
41464 KB |
Output is correct |
8 |
Correct |
40 ms |
41464 KB |
Output is correct |
9 |
Correct |
37 ms |
41464 KB |
Output is correct |
10 |
Correct |
36 ms |
41464 KB |
Output is correct |
11 |
Correct |
34 ms |
41464 KB |
Output is correct |
12 |
Correct |
33 ms |
41464 KB |
Output is correct |
13 |
Correct |
36 ms |
41600 KB |
Output is correct |
14 |
Correct |
37 ms |
41504 KB |
Output is correct |
15 |
Correct |
39 ms |
41976 KB |
Output is correct |
16 |
Correct |
37 ms |
41976 KB |
Output is correct |
17 |
Correct |
42 ms |
41720 KB |
Output is correct |
18 |
Correct |
35 ms |
41984 KB |
Output is correct |
19 |
Correct |
37 ms |
41856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
42488 KB |
Output is correct |
2 |
Correct |
53 ms |
41720 KB |
Output is correct |
3 |
Correct |
54 ms |
42872 KB |
Output is correct |
4 |
Correct |
98 ms |
55160 KB |
Output is correct |
5 |
Correct |
215 ms |
81016 KB |
Output is correct |
6 |
Correct |
73 ms |
43000 KB |
Output is correct |
7 |
Correct |
134 ms |
60116 KB |
Output is correct |
8 |
Correct |
36 ms |
41464 KB |
Output is correct |
9 |
Correct |
33 ms |
41720 KB |
Output is correct |
10 |
Correct |
34 ms |
41700 KB |
Output is correct |
11 |
Correct |
34 ms |
41728 KB |
Output is correct |
12 |
Correct |
37 ms |
41472 KB |
Output is correct |
13 |
Correct |
35 ms |
41472 KB |
Output is correct |
14 |
Correct |
60 ms |
45944 KB |
Output is correct |
15 |
Correct |
40 ms |
41720 KB |
Output is correct |
16 |
Correct |
43 ms |
42616 KB |
Output is correct |
17 |
Correct |
53 ms |
42104 KB |
Output is correct |
18 |
Correct |
65 ms |
43128 KB |
Output is correct |
19 |
Correct |
119 ms |
55420 KB |
Output is correct |
20 |
Correct |
264 ms |
81612 KB |
Output is correct |
21 |
Correct |
74 ms |
43640 KB |
Output is correct |
22 |
Correct |
166 ms |
60152 KB |
Output is correct |
23 |
Correct |
75 ms |
42872 KB |
Output is correct |
24 |
Correct |
84 ms |
46440 KB |
Output is correct |
25 |
Correct |
264 ms |
81756 KB |
Output is correct |
26 |
Correct |
32 ms |
41464 KB |
Output is correct |
27 |
Correct |
35 ms |
41464 KB |
Output is correct |
28 |
Correct |
65 ms |
45932 KB |
Output is correct |
29 |
Correct |
44 ms |
41592 KB |
Output is correct |
30 |
Correct |
42 ms |
42748 KB |
Output is correct |
31 |
Correct |
49 ms |
42104 KB |
Output is correct |
32 |
Correct |
57 ms |
43128 KB |
Output is correct |
33 |
Correct |
103 ms |
55544 KB |
Output is correct |
34 |
Correct |
218 ms |
81832 KB |
Output is correct |
35 |
Correct |
75 ms |
43856 KB |
Output is correct |
36 |
Correct |
137 ms |
60296 KB |
Output is correct |
37 |
Correct |
38 ms |
41592 KB |
Output is correct |
38 |
Correct |
55 ms |
45832 KB |
Output is correct |
39 |
Correct |
34 ms |
41464 KB |
Output is correct |
40 |
Correct |
35 ms |
41472 KB |
Output is correct |
41 |
Correct |
38 ms |
41532 KB |
Output is correct |
42 |
Correct |
34 ms |
41436 KB |
Output is correct |