#include <iostream>
#include <fstream>
#include <climits>
using namespace std;
const int nmax=100005;
long long v[nmax],dp[2][nmax],mn[nmax];
int st[nmax];
int n,k,i,j,use,u;
int main()
{
// freopen("data.in","r",stdin);
ios_base::sync_with_stdio(false);
cin>>n>>k;
for(i=1;i<=n;i++)
{
cin>>v[i];
}
for(i=0;i<=n;i++)
dp[0][i]=1LL*1e15,dp[1][i]=1LL*1e15;
mn[0]=1LL*1e15;dp[0][0]=0;
for(int cnt=1;cnt<=k;cnt++)
{
use=1-use;u=0;
for(i=1;i<=n;i++)
{
while(u>0&&v[i]>=v[st[u]])
u--;
st[++u]=i;
mn[u]=min(mn[u-1],dp[1-use][st[u-1]]+v[i]);
if(i>=cnt&&u==1)
mn[u]=min(dp[1-use][cnt-1]+v[i],mn[u]);
dp[use][i]=1LL*mn[u];
}
for(i=0;i<=n;i++)
dp[1-use][i]=1LL*1e15;
}
cout<<dp[use][n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
472 KB |
Output is correct |
5 |
Correct |
2 ms |
552 KB |
Output is correct |
6 |
Correct |
2 ms |
556 KB |
Output is correct |
7 |
Correct |
2 ms |
576 KB |
Output is correct |
8 |
Correct |
2 ms |
580 KB |
Output is correct |
9 |
Incorrect |
2 ms |
584 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
608 KB |
Output is correct |
2 |
Correct |
2 ms |
608 KB |
Output is correct |
3 |
Correct |
2 ms |
608 KB |
Output is correct |
4 |
Correct |
2 ms |
612 KB |
Output is correct |
5 |
Correct |
2 ms |
616 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
3 ms |
680 KB |
Output is correct |
8 |
Correct |
2 ms |
796 KB |
Output is correct |
9 |
Incorrect |
2 ms |
796 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |