#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]);
if(a[i] < a[ind[temp][j]])
ind[i][j] = ind[temp][j];
}
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 |
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 |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 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 |
424 KB |
Output is correct |
18 |
Correct |
2 ms |
508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
348 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 |
376 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 |
380 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 |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
2 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 |
85 ms |
70908 KB |
Output is correct |
3 |
Correct |
86 ms |
71004 KB |
Output is correct |
4 |
Correct |
178 ms |
71032 KB |
Output is correct |
5 |
Correct |
619 ms |
159352 KB |
Output is correct |
6 |
Correct |
206 ms |
159340 KB |
Output is correct |
7 |
Correct |
352 ms |
159224 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 |
504 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 |
16376 KB |
Output is correct |
15 |
Correct |
19 ms |
16248 KB |
Output is correct |
16 |
Correct |
30 ms |
16248 KB |
Output is correct |
17 |
Correct |
79 ms |
70904 KB |
Output is correct |
18 |
Correct |
88 ms |
71032 KB |
Output is correct |
19 |
Correct |
192 ms |
71032 KB |
Output is correct |
20 |
Correct |
624 ms |
159224 KB |
Output is correct |
21 |
Correct |
181 ms |
159236 KB |
Output is correct |
22 |
Correct |
400 ms |
159364 KB |
Output is correct |
23 |
Correct |
170 ms |
159316 KB |
Output is correct |
24 |
Correct |
218 ms |
159236 KB |
Output is correct |
25 |
Correct |
659 ms |
159224 KB |
Output is correct |
26 |
Correct |
4 ms |
1912 KB |
Output is correct |
27 |
Correct |
4 ms |
1916 KB |
Output is correct |
28 |
Correct |
48 ms |
16248 KB |
Output is correct |
29 |
Correct |
20 ms |
16376 KB |
Output is correct |
30 |
Correct |
26 ms |
16376 KB |
Output is correct |
31 |
Correct |
78 ms |
71004 KB |
Output is correct |
32 |
Correct |
88 ms |
71160 KB |
Output is correct |
33 |
Correct |
211 ms |
71312 KB |
Output is correct |
34 |
Correct |
607 ms |
159688 KB |
Output is correct |
35 |
Correct |
182 ms |
159736 KB |
Output is correct |
36 |
Correct |
397 ms |
159740 KB |
Output is correct |
37 |
Correct |
22 ms |
16248 KB |
Output is correct |
38 |
Correct |
48 ms |
16376 KB |
Output is correct |
39 |
Correct |
4 ms |
1912 KB |
Output is correct |
40 |
Correct |
4 ms |
1912 KB |
Output is correct |
41 |
Correct |
2 ms |
380 KB |
Output is correct |
42 |
Correct |
4 ms |
376 KB |
Output is correct |