#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=(1e8)+10;
vector<ll> v(N);
vector<ll> x(N, -1);
vector<ll> s;
vector<ll> dp1(N, inf), dp2(N, inf);
vector<ll> T(4*N, inf);
void upd(ll in, ll val, ll l, ll r, ll v1){
if(l==r && r==in){
T[v1]=val;
return;
}
if(l>r) return;
ll mid=(l+r)/2;
if(in<=mid){
upd(in, val, l, mid, 2*v1);
}
else{
upd(in, val, mid+1, r, 2*v1+1);
}
T[v1]=min(T[2*v1], T[2*v1+1]);
}
ll que(ll l, ll r, ll tl, ll tr, ll v1){
if(l>r) return inf;
if(l==tl && r==tr) return T[v1];
ll mid=(tl+tr)/2;
return min(que(l, min(r, mid), tl, mid, 2*v1), que(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);
}
dp1[1]=v[1];
upd(1, dp1[1], 1, n, 1);
rep(i, 2, n+1){
dp1[i]=max(dp1[i-1], v[i]);
upd(i, dp1[i], 1, n, 1);
}
rep(j, 2, k+1){
rep(i, 1, n+1){
dp2[i]=inf;
if(i<j){
continue;
}
else{
dp2[i]=v[i]+dp1[i-1];
if(x[i]==-1){
dp2[i]=min(dp2[i], v[i]+que(1, i-1, 1, n, 1));
}
else{
dp2[i]=min(dp2[i], dp2[x[i]]);
}
}
}
rep(i, 1, n+1){
upd(i, dp2[i], 1, n, 1);
}
swap(dp1, dp2);
}
cout<<dp1[n];
}
int main(){
ios_base::sync_with_stdio();
cin.tie(0);
ll t=1;
//~ cin>>t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
4 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6484 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
3 ms |
6484 KB |
Output is correct |
8 |
Correct |
4 ms |
6484 KB |
Output is correct |
9 |
Correct |
3 ms |
6484 KB |
Output is correct |
10 |
Correct |
3 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6484 KB |
Output is correct |
12 |
Correct |
3 ms |
6484 KB |
Output is correct |
13 |
Incorrect |
3 ms |
6484 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6484 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
3 ms |
6484 KB |
Output is correct |
8 |
Correct |
3 ms |
6484 KB |
Output is correct |
9 |
Correct |
3 ms |
6484 KB |
Output is correct |
10 |
Correct |
3 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6484 KB |
Output is correct |
12 |
Incorrect |
3 ms |
6572 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
4 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6484 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
3 ms |
6484 KB |
Output is correct |
8 |
Correct |
4 ms |
6484 KB |
Output is correct |
9 |
Correct |
3 ms |
6484 KB |
Output is correct |
10 |
Correct |
3 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6484 KB |
Output is correct |
12 |
Correct |
3 ms |
6484 KB |
Output is correct |
13 |
Incorrect |
3 ms |
6484 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
4 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6484 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
3 ms |
6484 KB |
Output is correct |
8 |
Correct |
4 ms |
6484 KB |
Output is correct |
9 |
Correct |
3 ms |
6484 KB |
Output is correct |
10 |
Correct |
3 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6484 KB |
Output is correct |
12 |
Correct |
3 ms |
6484 KB |
Output is correct |
13 |
Incorrect |
3 ms |
6484 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |