#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
const int N = 100005;
const int K = 103;
const int inf=1e9;
int dp[N], ndp[N], lg[N];
int st[N][17];
int get(int l, int r){
int rng=r-l+1;
int lga=lg[rng];
return max(st[l][lga], st[r-(1<<lga)+1][lga]);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
int a[n];
for(int i=0; i< n; i++)cin >> a[i];
lg[1]=0;
for(int i=2; i<N; i++){
lg[i]=lg[i/2]+1;
}
int mx=0;
for(int i=0; i< n; i++){
st[i][0]=a[i];
mx=max(mx, a[i]);
dp[i]=mx;
}
for(int j=1; j<17; j++){
for(int i=0; i+(1<<j)<=n; i++){
st[i][j]=max(st[i][j-1], st[i+(1<<(j-1))][j-1]);
}
}
for(int j=2; j<=k; j++){
stack<pair<int, int>> stc;
stc.push({dp[0], 0});
ndp[0]=inf;
for(int i=1; i< n; i++){
if(stc.size()==1){
ndp[i]=stc.top().F + get(stc.top().S+1, i);
}
else{
while(stc.size()>1){
pair<int, int> f=stc.top();
stc.pop();
pair<int, int> s=stc.top();
if(f.F+get(f.S+1, i)<s.F+get(s.S+1, i)){
stc.push(f);
break;
}
}
}
ndp[i]=stc.top().F+get(stc.top().S+1, i);
if(dp[i]<stc.top().F+get(stc.top().S+1, i)){
stc.push({dp[i], i});
}
}
for(int i=0; i< n; i++){
dp[i]=ndp[i];
}
}
cout << ndp[n-1] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |