#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];
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)
{
stack<int>st;
int mx = 0;
fo(j,1,n)
{
mx = max(mx, a[j]);
if(i == 1)
dp[i][j] = f[i][j] = mx;
while(!st.empty() && a[st.top()] <= a[j])
{
dp[i][j] = min(dp[i][j], f[i-1][st.top()] + a[j]);
f[i][j] = min({f[i][j], dp[i][j], f[i][st.top()]});
st.pop();
}
if(!st.empty())
{
dp[i][j] = min(dp[i][j], f[i-1][st.top()] + a[j]);
f[i][j] = min({f[i][j], dp[i][j], f[i][st.top()]});
}
st.push(j);
// cout<<i<<' '<<j<<' '<<dp[i][j]<<'\n';
}
}
cout<<dp[k][n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
164600 KB |
Output is correct |
2 |
Correct |
63 ms |
164680 KB |
Output is correct |
3 |
Correct |
60 ms |
164704 KB |
Output is correct |
4 |
Correct |
61 ms |
164720 KB |
Output is correct |
5 |
Correct |
63 ms |
164644 KB |
Output is correct |
6 |
Correct |
61 ms |
164664 KB |
Output is correct |
7 |
Correct |
61 ms |
164640 KB |
Output is correct |
8 |
Correct |
60 ms |
164640 KB |
Output is correct |
9 |
Correct |
61 ms |
164620 KB |
Output is correct |
10 |
Incorrect |
60 ms |
164700 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
164700 KB |
Output is correct |
2 |
Correct |
63 ms |
164684 KB |
Output is correct |
3 |
Correct |
63 ms |
164684 KB |
Output is correct |
4 |
Correct |
60 ms |
164584 KB |
Output is correct |
5 |
Correct |
61 ms |
164672 KB |
Output is correct |
6 |
Correct |
60 ms |
164644 KB |
Output is correct |
7 |
Correct |
62 ms |
164616 KB |
Output is correct |
8 |
Correct |
64 ms |
164668 KB |
Output is correct |
9 |
Correct |
63 ms |
164656 KB |
Output is correct |
10 |
Incorrect |
59 ms |
164696 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
164600 KB |
Output is correct |
2 |
Correct |
63 ms |
164680 KB |
Output is correct |
3 |
Correct |
60 ms |
164704 KB |
Output is correct |
4 |
Correct |
61 ms |
164720 KB |
Output is correct |
5 |
Correct |
63 ms |
164644 KB |
Output is correct |
6 |
Correct |
61 ms |
164664 KB |
Output is correct |
7 |
Correct |
61 ms |
164640 KB |
Output is correct |
8 |
Correct |
60 ms |
164640 KB |
Output is correct |
9 |
Correct |
61 ms |
164620 KB |
Output is correct |
10 |
Incorrect |
60 ms |
164700 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
164600 KB |
Output is correct |
2 |
Correct |
63 ms |
164680 KB |
Output is correct |
3 |
Correct |
60 ms |
164704 KB |
Output is correct |
4 |
Correct |
61 ms |
164720 KB |
Output is correct |
5 |
Correct |
63 ms |
164644 KB |
Output is correct |
6 |
Correct |
61 ms |
164664 KB |
Output is correct |
7 |
Correct |
61 ms |
164640 KB |
Output is correct |
8 |
Correct |
60 ms |
164640 KB |
Output is correct |
9 |
Correct |
61 ms |
164620 KB |
Output is correct |
10 |
Incorrect |
60 ms |
164700 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |