#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+6;
const int MAXK=128;
const int INF=2e9;
int n,k;
int a[MAXN];
int dp[2][MAXN];
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
dp[1][0]=INF;
for(int i=1;i<=n;i++)dp[1][i]=max(dp[1][i],a[i]);
for(int j=2;j<=k;j++)
{
stack<pair<int,int> >s;
dp[j%2][0]=INF;
dp[1-j%2][0]=INF;
for(int i=1;i<=n;i++)
{
int pr1=dp[1-j%2][i],pr2;
while(!s.empty()&&a[s.top().second]<=a[i])
{
pr1=min(pr1,s.top().first);
s.pop();
}
if(s.empty())pr2=INF;
else pr2=dp[1-j%2][s.top().second];
dp[j%2][i]=min(pr1+a[i],pr2);
s.push({pr1,i});
}
}
cout<<dp[k%2][n]<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |