#include<stdio.h>
int max(int a, int b){return a>b?a:b;}
int min(int a, int b){return a<b?a:b;}
int dp[101][100001];
int IT[300001];
int IT2[300001];
int b = 1;
int a[100001];
int bb[100001];
int getmin(int f, int r);
int main()
{
int n, k;
int i, j;
scanf("%d%d", &n, &k);
for(b=1; b<=n; b*=2);
for(i=1; i<=n; i++) scanf("%d", a+i);
int stk[100001], top=-1;
for(i=1; i<=n; i++)
{
while(top != -1 && a[stk[top]] < a[i])top--;
if(top == -1) bb[i] = -1;
else bb[i] = stk[top];
stk[++top] = i;
}
for(i=1; i<=n; i++) dp[1][i] = max(a[i], dp[1][i-1]);
for(i=2; i<=k; i++)
{
for(j=1; j<=n; j++) IT[b+j] = dp[i-1][j];
for(j=b; j>=1; j--) IT[j] = min(IT[j*2], IT[j*2+1]);
for(j=1; j<=2*b; j++) IT2[j] = 200000000;
for(j=1; j<i; j++) dp[i][j] = 200000000;
for(j=i; j<=n; j++)
{
if(bb[j] == -1) dp[i][j] = getmin(i-1, j-1) + a[j];
else
{
if(bb[j] < i)
{
dp[i][j] = getmin(i-1, j-1) + a[j];
}
else
{
dp[i][j] = min(getmin(bb[j], j-1) + a[j], dp[i][j-1]);
}
}
}
}
printf("%d", dp[k][n]);
return 0;
}
int getmin(int f, int r)
{
f+=b; r+=b;
int re = 200000000;
while(f<r)
{
if(f%2 == 1) re = min(re, IT[f++]);
if(r%2 == 0) re = min(re, IT[r--]);
f/=2; r/=2;
}
if(f==r) re = min(re, IT[f]);
return re;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
43932 KB |
Output is correct |
2 |
Correct |
0 ms |
43928 KB |
Output is correct |
3 |
Correct |
0 ms |
43928 KB |
Output is correct |
4 |
Correct |
0 ms |
43932 KB |
Output is correct |
5 |
Correct |
0 ms |
43928 KB |
Output is correct |
6 |
Correct |
0 ms |
43932 KB |
Output is correct |
7 |
Correct |
0 ms |
43932 KB |
Output is correct |
8 |
Correct |
0 ms |
43928 KB |
Output is correct |
9 |
Correct |
0 ms |
43928 KB |
Output is correct |
10 |
Incorrect |
0 ms |
43932 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |