#include <stdio.h>
#include <vector>
#define INF 0x7fffffff
using namespace std;
typedef pair <int,int> ppair;
vector <ppair> S;
int N, K, A[100001], D[2][100001];
void input(void)
{
int i;
scanf("%d %d",&N,&K);
for(i=1 ; i<=N ; i++)
scanf("%d",&A[i]);
}
void process(void)
{
int i, j, x, y;
for(i=1 ; i<=N ; i++)
{
D[1][i]=D[1][i-1];
if(D[1][i]<A[i])
D[1][i]=A[i];
}
for(i=2 ; i<=K ; i++)
{
S.clear();
for(j=i ; j<=N ; j++)
{
y=D[1-(i%2)][j-1];
while(!S.empty() && S.back().first<=A[j])
{
if(y>S.back().second)
y=S.back().second;
S.pop_back();
}
if(S.empty())
x=INF;
else
x=S.back().first+S.back().second;
if(x>A[j]+y)
{
x=A[j]+y;
S.push_back(make_pair(A[j],y));
}
D[i%2][j]=x;
}
}
}
void output(void)
{
printf("%d",D[K%2][N]);
}
int main(void)
{
input();
process();
output();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2376 KB |
Output is correct |
2 |
Correct |
0 ms |
2376 KB |
Output is correct |
3 |
Correct |
0 ms |
2376 KB |
Output is correct |
4 |
Correct |
0 ms |
2376 KB |
Output is correct |
5 |
Correct |
0 ms |
2376 KB |
Output is correct |
6 |
Correct |
0 ms |
2376 KB |
Output is correct |
7 |
Correct |
0 ms |
2376 KB |
Output is correct |
8 |
Correct |
0 ms |
2376 KB |
Output is correct |
9 |
Correct |
0 ms |
2376 KB |
Output is correct |
10 |
Correct |
0 ms |
2376 KB |
Output is correct |
11 |
Correct |
0 ms |
2376 KB |
Output is correct |
12 |
Correct |
0 ms |
2376 KB |
Output is correct |
13 |
Correct |
0 ms |
2376 KB |
Output is correct |
14 |
Correct |
0 ms |
2376 KB |
Output is correct |
15 |
Correct |
0 ms |
2376 KB |
Output is correct |
16 |
Correct |
0 ms |
2376 KB |
Output is correct |
17 |
Correct |
0 ms |
2376 KB |
Output is correct |
18 |
Correct |
0 ms |
2376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2376 KB |
Output is correct |
2 |
Correct |
0 ms |
2376 KB |
Output is correct |
3 |
Correct |
0 ms |
2376 KB |
Output is correct |
4 |
Correct |
0 ms |
2376 KB |
Output is correct |
5 |
Correct |
0 ms |
2376 KB |
Output is correct |
6 |
Correct |
0 ms |
2376 KB |
Output is correct |
7 |
Correct |
0 ms |
2376 KB |
Output is correct |
8 |
Correct |
0 ms |
2376 KB |
Output is correct |
9 |
Correct |
0 ms |
2376 KB |
Output is correct |
10 |
Correct |
0 ms |
2376 KB |
Output is correct |
11 |
Correct |
0 ms |
2376 KB |
Output is correct |
12 |
Correct |
0 ms |
2376 KB |
Output is correct |
13 |
Correct |
0 ms |
2376 KB |
Output is correct |
14 |
Correct |
0 ms |
2376 KB |
Output is correct |
15 |
Correct |
0 ms |
2376 KB |
Output is correct |
16 |
Correct |
0 ms |
2376 KB |
Output is correct |
17 |
Correct |
0 ms |
2376 KB |
Output is correct |
18 |
Correct |
0 ms |
2376 KB |
Output is correct |
19 |
Correct |
0 ms |
2376 KB |
Output is correct |
20 |
Correct |
0 ms |
2376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2376 KB |
Output is correct |
2 |
Correct |
0 ms |
2376 KB |
Output is correct |
3 |
Correct |
0 ms |
2376 KB |
Output is correct |
4 |
Correct |
0 ms |
2376 KB |
Output is correct |
5 |
Correct |
0 ms |
2376 KB |
Output is correct |
6 |
Correct |
0 ms |
2376 KB |
Output is correct |
7 |
Correct |
0 ms |
2376 KB |
Output is correct |
8 |
Correct |
0 ms |
2376 KB |
Output is correct |
9 |
Correct |
0 ms |
2376 KB |
Output is correct |
10 |
Correct |
0 ms |
2376 KB |
Output is correct |
11 |
Correct |
0 ms |
2376 KB |
Output is correct |
12 |
Correct |
0 ms |
2376 KB |
Output is correct |
13 |
Correct |
0 ms |
2376 KB |
Output is correct |
14 |
Correct |
0 ms |
2376 KB |
Output is correct |
15 |
Correct |
0 ms |
2376 KB |
Output is correct |
16 |
Correct |
0 ms |
2376 KB |
Output is correct |
17 |
Correct |
0 ms |
2376 KB |
Output is correct |
18 |
Correct |
0 ms |
2376 KB |
Output is correct |
19 |
Correct |
0 ms |
2376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2376 KB |
Output is correct |
2 |
Correct |
4 ms |
2376 KB |
Output is correct |
3 |
Correct |
12 ms |
2376 KB |
Output is correct |
4 |
Correct |
48 ms |
2376 KB |
Output is correct |
5 |
Correct |
136 ms |
2376 KB |
Output is correct |
6 |
Correct |
24 ms |
2376 KB |
Output is correct |
7 |
Correct |
60 ms |
2376 KB |
Output is correct |
8 |
Correct |
0 ms |
2376 KB |
Output is correct |
9 |
Correct |
0 ms |
2376 KB |
Output is correct |
10 |
Correct |
0 ms |
2376 KB |
Output is correct |
11 |
Correct |
0 ms |
2376 KB |
Output is correct |
12 |
Correct |
0 ms |
2376 KB |
Output is correct |
13 |
Correct |
0 ms |
2376 KB |
Output is correct |
14 |
Correct |
12 ms |
2376 KB |
Output is correct |
15 |
Correct |
0 ms |
2376 KB |
Output is correct |
16 |
Correct |
4 ms |
2376 KB |
Output is correct |
17 |
Correct |
4 ms |
2376 KB |
Output is correct |
18 |
Correct |
8 ms |
2376 KB |
Output is correct |
19 |
Correct |
40 ms |
2376 KB |
Output is correct |
20 |
Correct |
96 ms |
2376 KB |
Output is correct |
21 |
Correct |
12 ms |
2376 KB |
Output is correct |
22 |
Correct |
60 ms |
2376 KB |
Output is correct |
23 |
Correct |
8 ms |
2376 KB |
Output is correct |
24 |
Correct |
20 ms |
2376 KB |
Output is correct |
25 |
Correct |
116 ms |
2376 KB |
Output is correct |
26 |
Correct |
0 ms |
2376 KB |
Output is correct |
27 |
Correct |
0 ms |
2376 KB |
Output is correct |
28 |
Correct |
20 ms |
2376 KB |
Output is correct |
29 |
Correct |
0 ms |
2376 KB |
Output is correct |
30 |
Correct |
4 ms |
2376 KB |
Output is correct |
31 |
Correct |
8 ms |
2376 KB |
Output is correct |
32 |
Correct |
12 ms |
2508 KB |
Output is correct |
33 |
Correct |
60 ms |
2508 KB |
Output is correct |
34 |
Correct |
192 ms |
3148 KB |
Output is correct |
35 |
Correct |
20 ms |
3148 KB |
Output is correct |
36 |
Correct |
100 ms |
3148 KB |
Output is correct |
37 |
Correct |
0 ms |
2376 KB |
Output is correct |
38 |
Correct |
12 ms |
2376 KB |
Output is correct |
39 |
Correct |
0 ms |
2376 KB |
Output is correct |
40 |
Correct |
0 ms |
2376 KB |
Output is correct |
41 |
Correct |
0 ms |
2376 KB |
Output is correct |
42 |
Correct |
0 ms |
2376 KB |
Output is correct |