#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(ll i=ll(a); i<ll(b); i++)
#define repn(i, a, b) for(ll i=ll(b)-1; i>=ll(a); i--)
#define pb push_back
#define si size()
#define ff first
#define ss second
#define be begin()
#define en end()
const ll N=(1e5)+10;
const ll K=110;
const ll inf=(1e9)+10;
vector<ll> v(N);
vector<ll> x(N, -1);
vector<ll> s;
vector<vector<ll>> dp(N, vector<ll> (K, inf));
vector<vector<ll>> T(K, vector<ll> (4*N, inf));
void upd(ll j, ll in, ll val, ll l, ll r, ll v1){
if(l==r && r==in){
T[j][v1]=val;
return;
}
if(l>r) return;
ll mid=(l+r)/2;
if(in<=mid){
upd(j, in, val, l, mid, 2*v1);
}
else{
upd(j, in, val, mid+1, r, 2*v1+1);
}
T[j][v1]=min(T[j][2*v1], T[j][2*v1+1]);
}
ll que(ll j, ll l, ll r, ll tl, ll tr, ll v1){
if(l>r) return inf;
if(l==tl && r==tr) return T[j][v1];
ll mid=(tl+tr)/2;
return min(que(j, l, min(r, mid), tl, mid, 2*v1), que(j, max(mid+1, l), r, mid+1, tr, 2*v1+1));
}
void solve(){
ll n, k;
cin>>n>>k;
rep(i, 1, n+1){
cin>>v[i];
}
ll o=0;
s.pb(1);
rep(i, 2, n+1){
if(v[s[o]]>=v[i]){
x[i]=s[o];
}
else{
while(true){
if(o<0) break;
if(v[s[o]]>=v[i]) break;
s.pop_back();
o--;
}
if(o!=-1){
x[i]=s[o];
}
}
o++;
s.pb(i);
}
dp[0][0]=0;
dp[1][1]=v[1];
upd(1, 1, dp[1][1], 1, n, 1);
rep(i, 2, n+1){
dp[i][1]=max(dp[i-1][1], v[i]);
upd(1, i, dp[i][1], 1, n, 1);
}
rep(i, 2, n+1){
rep(j, 2, k+1){
if(i<j){
upd(j, i, dp[i][j], 1, n, 1);
}
else{
dp[i][j]=v[i]+dp[i-1][j-1];
if(x[i]==-1){
dp[i][j]=min(dp[i][j], v[i]+que(j-1, 1, i-1, 1, n, 1));
}
else if(x[i]==i-1){
dp[i][j]=min(dp[i][j], dp[x[i]][j]);
}
else{
dp[i][j]=min(dp[i][j], v[i]+que(j-1, x[i]+1, i-1, 1, n, 1));
dp[i][j]=min(dp[i][j], dp[x[i]][j]);
}
upd(j, i, dp[i][j], 1, n, 1);
}
}
}
cout<<dp[n][k];
}
int main(){
ios_base::sync_with_stdio();
cin.tie(0);
ll t=1;
//~ cin>>t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
134 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
135 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
134 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
134 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |