#include<bits/stdc++.h>
using namespace std;
void readInput()
{
}
void solve()
{
}
int a[100004],n,k,lef[100004],minn[100004],maxx[100004],dp[104][100004];
int INF=2e8;
stack<int> s;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
readInput();
solve();
cin>>n>>k;
for(int i=1; i<=n; i++)
cin>>a[i];
for(int i=1; i<=n; i++)
{
minn[i]=INF;
maxx[i]=INF;
}
s.push(1);
int maxxx=0;
for(int i=1; i<=n; i++)
{
maxxx=max(maxxx,a[i]);
dp[1][i]=maxxx;
}
for(int i=2; i<=n; i++)
{
while(!s.empty())
{
if(a[s.top()]>a[i])
{
// dp[1][i]=a[s.top()];
lef[i]=s.top();
int c=min(maxx[s.top()],min(minn[i],dp[1][i]));
minn[i]=min(minn[i],min(dp[1][s.top()],maxx[s.top()]));
maxx[s.top()]=c;
break;
}
else
minn[i]=min(min(minn[i],minn[s.top()]),min(dp[1][s.top()],maxx[s.top()])),s.pop();
}
s.push(i);
//if(dp[1][i]==0) dp[1][i]=a[i];
}
dp[1][0]=INF;
for(int i=2; i<=k; i++)
{
dp[i][0]=INF;
for(int j=1; j<=n; j++)
{
dp[i][j]=dp[i][lef[j]];
dp[i][j]=min(dp[i][j],minn[j]+a[j]);
minn[j]=INF;
maxx[j]=INF;
}
while(!s.empty())
s.pop();
s.push(1);
for(int j=2; j<=n; j++)
{
while(!s.empty())
{
if(a[s.top()]>a[j])
{
// dp[1][i]=a[s.top()];
int c=min(maxx[s.top()],min(minn[j],dp[i][j]));
minn[j]=min(minn[j],min(dp[i][s.top()],maxx[s.top()]));
maxx[s.top()]=c;
break;
}
else
minn[j]=min(min(minn[j],minn[s.top()]),min(dp[i][s.top()],maxx[s.top()])),s.pop();
}
s.push(j);
//if(dp[1][i]==0) dp[1][i]=a[i];
}
}
cout<<dp[k][n];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
896 KB |
Output is correct |
16 |
Correct |
1 ms |
896 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
896 KB |
Output is correct |
19 |
Correct |
1 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1664 KB |
Output is correct |
2 |
Correct |
6 ms |
1536 KB |
Output is correct |
3 |
Correct |
12 ms |
2560 KB |
Output is correct |
4 |
Correct |
79 ms |
14968 KB |
Output is correct |
5 |
Correct |
225 ms |
41720 KB |
Output is correct |
6 |
Correct |
17 ms |
3840 KB |
Output is correct |
7 |
Correct |
108 ms |
20344 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
672 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
452 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
32 ms |
4984 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
9 ms |
1664 KB |
Output is correct |
17 |
Correct |
6 ms |
1536 KB |
Output is correct |
18 |
Correct |
14 ms |
2560 KB |
Output is correct |
19 |
Correct |
113 ms |
14840 KB |
Output is correct |
20 |
Correct |
327 ms |
41744 KB |
Output is correct |
21 |
Correct |
19 ms |
3712 KB |
Output is correct |
22 |
Correct |
152 ms |
20216 KB |
Output is correct |
23 |
Correct |
13 ms |
2944 KB |
Output is correct |
24 |
Correct |
42 ms |
6528 KB |
Output is correct |
25 |
Correct |
321 ms |
41720 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
29 ms |
4984 KB |
Output is correct |
29 |
Correct |
2 ms |
640 KB |
Output is correct |
30 |
Correct |
8 ms |
1664 KB |
Output is correct |
31 |
Correct |
6 ms |
1664 KB |
Output is correct |
32 |
Correct |
13 ms |
2688 KB |
Output is correct |
33 |
Correct |
94 ms |
14968 KB |
Output is correct |
34 |
Correct |
275 ms |
41976 KB |
Output is correct |
35 |
Correct |
18 ms |
3968 KB |
Output is correct |
36 |
Correct |
123 ms |
20324 KB |
Output is correct |
37 |
Correct |
2 ms |
640 KB |
Output is correct |
38 |
Correct |
23 ms |
4992 KB |
Output is correct |
39 |
Correct |
1 ms |
512 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
0 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |