#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()
{
#ifndef ONLINE_JUDGE
freopen("new.inp","r",stdin);
freopen("new.out","w",stdout);
#endif // ONLINE_JUDGE
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] = 1e9;
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])
{
int last = st.top();
st.pop();
f[i-1][st.top()] = min(f[i-1][st.top()], f[i-1][last]);
}
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];
}
st.push(j);
}
}
cout<<dp[k][n];
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:42:16: warning: unused variable 'tmp' [-Wunused-variable]
42 | ll tmp = 1e18;
| ^~~
blocks.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen("new.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
blocks.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | freopen("new.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
164668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
164736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
164668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
164668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |