#include <bits/stdc++.h>
#define ll long long
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fod(i,a,b) for(int i=a;i>=b;--i)
#define ii pair<ll,ll>
#define iii pair<ll,ii>
#define fi first
#define se second
#define oo 1e18
#define bit(x,i) (((x)>>(i))&1)
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int K = 1e2 + 5;
int n,k,a[N];
ll dp[K][N],f[K][N],L[N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>k;
fo(i,1,n)
cin>>a[i];
memset(dp,0x3f,sizeof(dp));
memset(f,0x3f,sizeof(f));
dp[0][0] = 0;
fo(i,1,k)
{
int mx = 0;
fo(j,1,n)
{
mx = max(mx, a[j]);
if(i == 1)
dp[i][j] = f[i][j] = mx;
L[j] = j-1;
while(L[j] > 0 && a[L[j]] <= a[j])
{
f[i][L[L[j]]] = min(f[i-1][L[L[j]]], f[i-1][L[j]]);
L[j] = L[L[j]];
}
if(i <= j)
{
dp[i][j] = min({dp[i][j], dp[i][L[j]], f[i-1][L[j]] + a[j]});
f[i][j] = dp[i][j];
}
}
}
cout<<dp[k][n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
164684 KB |
Output is correct |
2 |
Correct |
59 ms |
164584 KB |
Output is correct |
3 |
Correct |
58 ms |
164592 KB |
Output is correct |
4 |
Incorrect |
72 ms |
164636 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
164852 KB |
Output is correct |
2 |
Correct |
63 ms |
164592 KB |
Output is correct |
3 |
Correct |
63 ms |
164672 KB |
Output is correct |
4 |
Incorrect |
57 ms |
164624 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
164684 KB |
Output is correct |
2 |
Correct |
59 ms |
164584 KB |
Output is correct |
3 |
Correct |
58 ms |
164592 KB |
Output is correct |
4 |
Incorrect |
72 ms |
164636 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
164684 KB |
Output is correct |
2 |
Correct |
59 ms |
164584 KB |
Output is correct |
3 |
Correct |
58 ms |
164592 KB |
Output is correct |
4 |
Incorrect |
72 ms |
164636 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |