#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 100006
int inf =10000000000006;
int dp[N][101];
int ind[N][101];
int pre[N];
int32_t main()
{
int n, k;
cin>>n>>k;
vector < int > a(n+1);
for(int i=1;i<=n;i++)
cin>>a[i];
a[0] = inf;
int cur = 1;
for(int i=1;i<=n;i++)
{
if( a[cur] < a[i])
cur = i;
dp[i][1] = a[cur];
ind[i][1] = cur;
}
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] + a[i];
ind[i][j] = i;
while(s.size())
{
int temp = s.top();
if( a[temp] < a[i] )
{
if(dp[temp][j] - a[ind[temp][j]] + max(a[ind[temp][j]], a[i]) < dp[i][j])
{
dp[i][j] = dp[temp][j] - a[ind[temp][j]] + max(a[ind[temp][j]], a[i]);
}
s.pop();
}
else
{
break;
}
}
if(s.size())
{
int cur = s.top();
if( dp[i][j] > dp[cur][j])
{
dp[i][j] = dp[cur][j];
ind[i][j] = ind[cur][j];
}
}
s.push(i);
}
}
cout<<dp[n][k]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
248 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
508 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
508 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
4 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
16248 KB |
Output is correct |
2 |
Correct |
78 ms |
71260 KB |
Output is correct |
3 |
Correct |
87 ms |
71288 KB |
Output is correct |
4 |
Correct |
177 ms |
71288 KB |
Output is correct |
5 |
Correct |
582 ms |
160152 KB |
Output is correct |
6 |
Correct |
182 ms |
160156 KB |
Output is correct |
7 |
Correct |
368 ms |
159968 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
500 KB |
Output is correct |
12 |
Correct |
4 ms |
1912 KB |
Output is correct |
13 |
Correct |
4 ms |
1912 KB |
Output is correct |
14 |
Correct |
52 ms |
16248 KB |
Output is correct |
15 |
Correct |
20 ms |
16248 KB |
Output is correct |
16 |
Correct |
30 ms |
16248 KB |
Output is correct |
17 |
Correct |
77 ms |
71292 KB |
Output is correct |
18 |
Correct |
87 ms |
71288 KB |
Output is correct |
19 |
Correct |
210 ms |
71288 KB |
Output is correct |
20 |
Correct |
695 ms |
159964 KB |
Output is correct |
21 |
Correct |
187 ms |
159932 KB |
Output is correct |
22 |
Correct |
364 ms |
160088 KB |
Output is correct |
23 |
Correct |
171 ms |
159992 KB |
Output is correct |
24 |
Correct |
217 ms |
159864 KB |
Output is correct |
25 |
Correct |
683 ms |
159972 KB |
Output is correct |
26 |
Correct |
4 ms |
1912 KB |
Output is correct |
27 |
Correct |
4 ms |
1912 KB |
Output is correct |
28 |
Correct |
48 ms |
16376 KB |
Output is correct |
29 |
Correct |
20 ms |
16248 KB |
Output is correct |
30 |
Correct |
30 ms |
16248 KB |
Output is correct |
31 |
Correct |
87 ms |
71288 KB |
Output is correct |
32 |
Correct |
100 ms |
71500 KB |
Output is correct |
33 |
Correct |
182 ms |
71476 KB |
Output is correct |
34 |
Correct |
631 ms |
160556 KB |
Output is correct |
35 |
Correct |
182 ms |
160264 KB |
Output is correct |
36 |
Correct |
432 ms |
160404 KB |
Output is correct |
37 |
Correct |
20 ms |
16248 KB |
Output is correct |
38 |
Correct |
45 ms |
16248 KB |
Output is correct |
39 |
Correct |
4 ms |
1912 KB |
Output is correct |
40 |
Correct |
4 ms |
1900 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |