#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);
}
vector<ll> pre(N, 0);
rep(i, 1, n+1){
pre[i]=pre[i-1]+v[i];
}
rep(j, 2, k+1){
rep(i, 1, n+1){
dp2[i]=inf;
if(i<j){
continue;
}
else if(i==j){
dp2[i]=pre[i];
}
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 if(x[i]==i-1){
dp2[i]=min(dp2[i], dp2[x[i]]);
}
else{
dp2[i]=min(dp2[i], v[i]+que(x[i]+1, i-1, 1, n, 1));
dp2[i]=min(dp2[i], dp2[x[i]]);
}
}
}
rep(i, 0, 4*N){
T[i]=inf;
}
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 |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7352 KB |
Output is correct |
7 |
Correct |
5 ms |
7252 KB |
Output is correct |
8 |
Correct |
9 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
6 ms |
7400 KB |
Output is correct |
11 |
Correct |
6 ms |
7336 KB |
Output is correct |
12 |
Correct |
4 ms |
7352 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
5 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7352 KB |
Output is correct |
16 |
Correct |
5 ms |
7376 KB |
Output is correct |
17 |
Correct |
5 ms |
7252 KB |
Output is correct |
18 |
Correct |
4 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7296 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
3 ms |
7352 KB |
Output is correct |
4 |
Correct |
6 ms |
7252 KB |
Output is correct |
5 |
Correct |
4 ms |
7352 KB |
Output is correct |
6 |
Correct |
5 ms |
7252 KB |
Output is correct |
7 |
Correct |
5 ms |
7252 KB |
Output is correct |
8 |
Correct |
6 ms |
7252 KB |
Output is correct |
9 |
Correct |
5 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
6 ms |
7360 KB |
Output is correct |
12 |
Correct |
5 ms |
7252 KB |
Output is correct |
13 |
Correct |
5 ms |
7252 KB |
Output is correct |
14 |
Correct |
5 ms |
7252 KB |
Output is correct |
15 |
Correct |
5 ms |
7252 KB |
Output is correct |
16 |
Correct |
7 ms |
7360 KB |
Output is correct |
17 |
Correct |
7 ms |
7252 KB |
Output is correct |
18 |
Correct |
6 ms |
7252 KB |
Output is correct |
19 |
Correct |
6 ms |
7356 KB |
Output is correct |
20 |
Correct |
7 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7352 KB |
Output is correct |
7 |
Correct |
5 ms |
7252 KB |
Output is correct |
8 |
Correct |
9 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
6 ms |
7400 KB |
Output is correct |
11 |
Correct |
6 ms |
7336 KB |
Output is correct |
12 |
Correct |
4 ms |
7352 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
5 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7352 KB |
Output is correct |
16 |
Correct |
5 ms |
7376 KB |
Output is correct |
17 |
Correct |
5 ms |
7252 KB |
Output is correct |
18 |
Correct |
4 ms |
7252 KB |
Output is correct |
19 |
Correct |
4 ms |
7296 KB |
Output is correct |
20 |
Correct |
4 ms |
7252 KB |
Output is correct |
21 |
Correct |
3 ms |
7352 KB |
Output is correct |
22 |
Correct |
6 ms |
7252 KB |
Output is correct |
23 |
Correct |
4 ms |
7352 KB |
Output is correct |
24 |
Correct |
5 ms |
7252 KB |
Output is correct |
25 |
Correct |
5 ms |
7252 KB |
Output is correct |
26 |
Correct |
6 ms |
7252 KB |
Output is correct |
27 |
Correct |
5 ms |
7252 KB |
Output is correct |
28 |
Correct |
4 ms |
7252 KB |
Output is correct |
29 |
Correct |
6 ms |
7360 KB |
Output is correct |
30 |
Correct |
5 ms |
7252 KB |
Output is correct |
31 |
Correct |
5 ms |
7252 KB |
Output is correct |
32 |
Correct |
5 ms |
7252 KB |
Output is correct |
33 |
Correct |
5 ms |
7252 KB |
Output is correct |
34 |
Correct |
7 ms |
7360 KB |
Output is correct |
35 |
Correct |
7 ms |
7252 KB |
Output is correct |
36 |
Correct |
6 ms |
7252 KB |
Output is correct |
37 |
Correct |
6 ms |
7356 KB |
Output is correct |
38 |
Correct |
7 ms |
7252 KB |
Output is correct |
39 |
Correct |
11 ms |
7368 KB |
Output is correct |
40 |
Correct |
4 ms |
7356 KB |
Output is correct |
41 |
Correct |
4 ms |
7252 KB |
Output is correct |
42 |
Correct |
5 ms |
7252 KB |
Output is correct |
43 |
Correct |
5 ms |
7252 KB |
Output is correct |
44 |
Correct |
5 ms |
7344 KB |
Output is correct |
45 |
Correct |
3 ms |
7252 KB |
Output is correct |
46 |
Correct |
4 ms |
7380 KB |
Output is correct |
47 |
Correct |
4 ms |
7380 KB |
Output is correct |
48 |
Correct |
5 ms |
7356 KB |
Output is correct |
49 |
Correct |
4 ms |
7300 KB |
Output is correct |
50 |
Correct |
6 ms |
7252 KB |
Output is correct |
51 |
Correct |
9 ms |
7356 KB |
Output is correct |
52 |
Correct |
8 ms |
7316 KB |
Output is correct |
53 |
Correct |
18 ms |
7356 KB |
Output is correct |
54 |
Correct |
19 ms |
7252 KB |
Output is correct |
55 |
Correct |
12 ms |
7252 KB |
Output is correct |
56 |
Correct |
22 ms |
7252 KB |
Output is correct |
57 |
Correct |
20 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7372 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7252 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
4 ms |
7352 KB |
Output is correct |
7 |
Correct |
5 ms |
7252 KB |
Output is correct |
8 |
Correct |
9 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
6 ms |
7400 KB |
Output is correct |
11 |
Correct |
6 ms |
7336 KB |
Output is correct |
12 |
Correct |
4 ms |
7352 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
5 ms |
7252 KB |
Output is correct |
15 |
Correct |
4 ms |
7352 KB |
Output is correct |
16 |
Correct |
5 ms |
7376 KB |
Output is correct |
17 |
Correct |
5 ms |
7252 KB |
Output is correct |
18 |
Correct |
4 ms |
7252 KB |
Output is correct |
19 |
Correct |
4 ms |
7296 KB |
Output is correct |
20 |
Correct |
4 ms |
7252 KB |
Output is correct |
21 |
Correct |
3 ms |
7352 KB |
Output is correct |
22 |
Correct |
6 ms |
7252 KB |
Output is correct |
23 |
Correct |
4 ms |
7352 KB |
Output is correct |
24 |
Correct |
5 ms |
7252 KB |
Output is correct |
25 |
Correct |
5 ms |
7252 KB |
Output is correct |
26 |
Correct |
6 ms |
7252 KB |
Output is correct |
27 |
Correct |
5 ms |
7252 KB |
Output is correct |
28 |
Correct |
4 ms |
7252 KB |
Output is correct |
29 |
Correct |
6 ms |
7360 KB |
Output is correct |
30 |
Correct |
5 ms |
7252 KB |
Output is correct |
31 |
Correct |
5 ms |
7252 KB |
Output is correct |
32 |
Correct |
5 ms |
7252 KB |
Output is correct |
33 |
Correct |
5 ms |
7252 KB |
Output is correct |
34 |
Correct |
7 ms |
7360 KB |
Output is correct |
35 |
Correct |
7 ms |
7252 KB |
Output is correct |
36 |
Correct |
6 ms |
7252 KB |
Output is correct |
37 |
Correct |
6 ms |
7356 KB |
Output is correct |
38 |
Correct |
7 ms |
7252 KB |
Output is correct |
39 |
Correct |
11 ms |
7368 KB |
Output is correct |
40 |
Correct |
4 ms |
7356 KB |
Output is correct |
41 |
Correct |
4 ms |
7252 KB |
Output is correct |
42 |
Correct |
5 ms |
7252 KB |
Output is correct |
43 |
Correct |
5 ms |
7252 KB |
Output is correct |
44 |
Correct |
5 ms |
7344 KB |
Output is correct |
45 |
Correct |
3 ms |
7252 KB |
Output is correct |
46 |
Correct |
4 ms |
7380 KB |
Output is correct |
47 |
Correct |
4 ms |
7380 KB |
Output is correct |
48 |
Correct |
5 ms |
7356 KB |
Output is correct |
49 |
Correct |
4 ms |
7300 KB |
Output is correct |
50 |
Correct |
6 ms |
7252 KB |
Output is correct |
51 |
Correct |
9 ms |
7356 KB |
Output is correct |
52 |
Correct |
8 ms |
7316 KB |
Output is correct |
53 |
Correct |
18 ms |
7356 KB |
Output is correct |
54 |
Correct |
19 ms |
7252 KB |
Output is correct |
55 |
Correct |
12 ms |
7252 KB |
Output is correct |
56 |
Correct |
22 ms |
7252 KB |
Output is correct |
57 |
Correct |
20 ms |
7252 KB |
Output is correct |
58 |
Correct |
56 ms |
7424 KB |
Output is correct |
59 |
Correct |
22 ms |
7636 KB |
Output is correct |
60 |
Correct |
79 ms |
7656 KB |
Output is correct |
61 |
Correct |
644 ms |
7756 KB |
Output is correct |
62 |
Execution timed out |
1072 ms |
8028 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |