#include <bits/stdc++.h>
using namespace std;
const long long MXN=1e5+10;
const long long MXK=1e2+10;
const long long INF=1e18;
long long dp[MXK][MXN];
long long a[MXN];
int main()
{
cin.tie(0);
ios_base::sync_with_stdio(0);
long long n,k;
cin>>n>>k;
for(long long i=1;i<=n;i++)
{
cin>>a[i];
}
for(long long i=0;i<=k;i++)
{
for(long long j=0;j<=n;j++)
{
dp[i][j]=INF;
}
}
dp[1][1]=a[1];
for(long long i=2;i<=n;i++)
{
dp[1][i]=max(dp[1][i-1],a[i]);
}
for(long long i=2;i<=k;i++)
{
stack < pair < long long , long long > > st;
for(long long j=1;j<=n;i++)
{
long long tmp=dp[i-1][j-1];
while(!st.empty())
{
if(a[st.top().second]<a[j])
{
tmp=min(tmp,st.top().first);
st.pop();
}
else
{
break;
}
}
if(!st.empty() && i<=j)
{
dp[i][j]=dp[i][st.top().first];
}
st.push({j,min(tmp,dp[i-1][j])});
if(i<=j)
{
dp[i][j]=min(dp[i][j],tmp+a[j]);
}
}
}
cout<<dp[n][k]<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |