#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;
a[0] = 1e18;
fo(i,1,k)
{
stack<int>st;
st.push(0);
int mx = 0;
fo(j,1,n)
{
mx = max(mx, a[j]);
if(i == 1)
dp[i][j] = f[i][j] = mx;
ll tmp = 1e18;
while(!st.empty() && a[st.top()] <= a[j])
{
if(i <= j)
{
dp[i][j] = min(dp[i][j], f[i-1][st.top()] + a[j]);
tmp = min(tmp, f[i][st.top()]);
}
st.pop();
}
if(i <= j)
{
dp[i][j] = min({dp[i][j], dp[i][st.top()], f[i-1][st.top()] + a[j]});
f[i][j] = dp[i][j];
}
f[i][st.top()] = min(f[i][st.top()], tmp);
st.push(j);
// cout<<i<<' '<<j<<' '<<dp[i][j]<<'\n';
}
}
cout<<dp[k][n];
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:27:12: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
27 | a[0] = 1e18;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
164612 KB |
Output is correct |
2 |
Correct |
59 ms |
164704 KB |
Output is correct |
3 |
Correct |
64 ms |
164636 KB |
Output is correct |
4 |
Correct |
67 ms |
164600 KB |
Output is correct |
5 |
Correct |
66 ms |
164684 KB |
Output is correct |
6 |
Correct |
68 ms |
164580 KB |
Output is correct |
7 |
Correct |
59 ms |
164684 KB |
Output is correct |
8 |
Correct |
61 ms |
164608 KB |
Output is correct |
9 |
Correct |
62 ms |
164640 KB |
Output is correct |
10 |
Incorrect |
74 ms |
164812 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
164600 KB |
Output is correct |
2 |
Correct |
65 ms |
164668 KB |
Output is correct |
3 |
Correct |
59 ms |
164616 KB |
Output is correct |
4 |
Correct |
61 ms |
164608 KB |
Output is correct |
5 |
Correct |
64 ms |
164684 KB |
Output is correct |
6 |
Correct |
61 ms |
164700 KB |
Output is correct |
7 |
Correct |
67 ms |
164712 KB |
Output is correct |
8 |
Correct |
65 ms |
164648 KB |
Output is correct |
9 |
Correct |
64 ms |
164608 KB |
Output is correct |
10 |
Incorrect |
62 ms |
164620 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
164612 KB |
Output is correct |
2 |
Correct |
59 ms |
164704 KB |
Output is correct |
3 |
Correct |
64 ms |
164636 KB |
Output is correct |
4 |
Correct |
67 ms |
164600 KB |
Output is correct |
5 |
Correct |
66 ms |
164684 KB |
Output is correct |
6 |
Correct |
68 ms |
164580 KB |
Output is correct |
7 |
Correct |
59 ms |
164684 KB |
Output is correct |
8 |
Correct |
61 ms |
164608 KB |
Output is correct |
9 |
Correct |
62 ms |
164640 KB |
Output is correct |
10 |
Incorrect |
74 ms |
164812 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
164612 KB |
Output is correct |
2 |
Correct |
59 ms |
164704 KB |
Output is correct |
3 |
Correct |
64 ms |
164636 KB |
Output is correct |
4 |
Correct |
67 ms |
164600 KB |
Output is correct |
5 |
Correct |
66 ms |
164684 KB |
Output is correct |
6 |
Correct |
68 ms |
164580 KB |
Output is correct |
7 |
Correct |
59 ms |
164684 KB |
Output is correct |
8 |
Correct |
61 ms |
164608 KB |
Output is correct |
9 |
Correct |
62 ms |
164640 KB |
Output is correct |
10 |
Incorrect |
74 ms |
164812 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |