#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)+1;
const ll K=101;
const ll inf=(1e8)+1;
vector<ll> v(N);
vector<ll> x(N, -1);
vector<ll> s;
vector<ll> dp1(N, inf), dp2(N, inf);
vector<ll> T(2*N, inf);
void construct(ll n){
rep(i, 0, n){
T[n+i]=v[i];
}
repn(i, 1, n){
T[i]=min(T[2*i], T[2*i+1]);
}
}
void upd(ll in, ll val, ll n){
in+=n;
T[in]=val;
while(in>1){
in/=2;
T[in]=min(T[2*in], T[2*in+1]);
}
}
ll que(ll l, ll r, ll n){
l+=n, r+=n;
ll mn=inf;
while(l<r){
if(l%2==1){
mn=min(mn, T[l]);
l++;
}
if(r%2==1){
r--;
mn=min(mn, T[r]);
}
l/=2;
r/=2;
}
return mn;
}
void solve(){
ll n, k;
cin>>n>>k;
rep(i, 0, n){
cin>>v[i];
}
ll o=0;
s.pb(1);
rep(i, 1, n){
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[0]=v[0];
upd(0, dp1[0], n);
rep(i, 1, n){
dp1[i]=max(dp1[i-1], v[i]);
upd(i, dp1[i], n);
}
rep(j, 2, k+1){
rep(i, 0, n){
dp2[i]=inf;
if(i+1<j){
continue;
}
else{
dp2[i]=v[i]+dp1[i-1];
if(x[i]==-1){
dp2[i]=min(dp2[i], v[i]+que(0, i-1, n));
}
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, n));
dp2[i]=min(dp2[i], dp2[x[i]]);
}
}
}
rep(i, 0, n){
upd(i, dp2[i], n);
}
swap(dp1, dp2);
}
cout<<dp1[n-1];
}
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 |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
5024 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
5020 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5020 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
3 ms |
5024 KB |
Output is correct |
4 |
Correct |
4 ms |
5016 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
4912 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5024 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
5020 KB |
Output is correct |
16 |
Correct |
3 ms |
5032 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
5024 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
5020 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5020 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
5024 KB |
Output is correct |
22 |
Correct |
4 ms |
5016 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
4 ms |
5020 KB |
Output is correct |
25 |
Correct |
3 ms |
4912 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
5024 KB |
Output is correct |
29 |
Correct |
3 ms |
4948 KB |
Output is correct |
30 |
Correct |
3 ms |
4948 KB |
Output is correct |
31 |
Correct |
3 ms |
4948 KB |
Output is correct |
32 |
Correct |
3 ms |
5016 KB |
Output is correct |
33 |
Correct |
3 ms |
5020 KB |
Output is correct |
34 |
Correct |
3 ms |
5032 KB |
Output is correct |
35 |
Correct |
3 ms |
5016 KB |
Output is correct |
36 |
Correct |
3 ms |
4948 KB |
Output is correct |
37 |
Correct |
3 ms |
4948 KB |
Output is correct |
38 |
Correct |
3 ms |
4948 KB |
Output is correct |
39 |
Correct |
3 ms |
4948 KB |
Output is correct |
40 |
Correct |
3 ms |
4948 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
4 ms |
4948 KB |
Output is correct |
43 |
Correct |
3 ms |
4948 KB |
Output is correct |
44 |
Correct |
3 ms |
4972 KB |
Output is correct |
45 |
Correct |
3 ms |
4948 KB |
Output is correct |
46 |
Correct |
3 ms |
4964 KB |
Output is correct |
47 |
Correct |
2 ms |
5024 KB |
Output is correct |
48 |
Correct |
4 ms |
4948 KB |
Output is correct |
49 |
Correct |
3 ms |
4948 KB |
Output is correct |
50 |
Correct |
3 ms |
4948 KB |
Output is correct |
51 |
Correct |
3 ms |
4948 KB |
Output is correct |
52 |
Correct |
2 ms |
4948 KB |
Output is correct |
53 |
Correct |
3 ms |
4948 KB |
Output is correct |
54 |
Correct |
3 ms |
5020 KB |
Output is correct |
55 |
Correct |
4 ms |
4948 KB |
Output is correct |
56 |
Correct |
3 ms |
4948 KB |
Output is correct |
57 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
5024 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
5020 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5020 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5024 KB |
Output is correct |
21 |
Correct |
3 ms |
5024 KB |
Output is correct |
22 |
Correct |
4 ms |
5016 KB |
Output is correct |
23 |
Correct |
3 ms |
4948 KB |
Output is correct |
24 |
Correct |
4 ms |
5020 KB |
Output is correct |
25 |
Correct |
3 ms |
4912 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
5024 KB |
Output is correct |
29 |
Correct |
3 ms |
4948 KB |
Output is correct |
30 |
Correct |
3 ms |
4948 KB |
Output is correct |
31 |
Correct |
3 ms |
4948 KB |
Output is correct |
32 |
Correct |
3 ms |
5016 KB |
Output is correct |
33 |
Correct |
3 ms |
5020 KB |
Output is correct |
34 |
Correct |
3 ms |
5032 KB |
Output is correct |
35 |
Correct |
3 ms |
5016 KB |
Output is correct |
36 |
Correct |
3 ms |
4948 KB |
Output is correct |
37 |
Correct |
3 ms |
4948 KB |
Output is correct |
38 |
Correct |
3 ms |
4948 KB |
Output is correct |
39 |
Correct |
3 ms |
4948 KB |
Output is correct |
40 |
Correct |
3 ms |
4948 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
4 ms |
4948 KB |
Output is correct |
43 |
Correct |
3 ms |
4948 KB |
Output is correct |
44 |
Correct |
3 ms |
4972 KB |
Output is correct |
45 |
Correct |
3 ms |
4948 KB |
Output is correct |
46 |
Correct |
3 ms |
4964 KB |
Output is correct |
47 |
Correct |
2 ms |
5024 KB |
Output is correct |
48 |
Correct |
4 ms |
4948 KB |
Output is correct |
49 |
Correct |
3 ms |
4948 KB |
Output is correct |
50 |
Correct |
3 ms |
4948 KB |
Output is correct |
51 |
Correct |
3 ms |
4948 KB |
Output is correct |
52 |
Correct |
2 ms |
4948 KB |
Output is correct |
53 |
Correct |
3 ms |
4948 KB |
Output is correct |
54 |
Correct |
3 ms |
5020 KB |
Output is correct |
55 |
Correct |
4 ms |
4948 KB |
Output is correct |
56 |
Correct |
3 ms |
4948 KB |
Output is correct |
57 |
Correct |
3 ms |
4948 KB |
Output is correct |
58 |
Correct |
23 ms |
5076 KB |
Output is correct |
59 |
Correct |
18 ms |
5276 KB |
Output is correct |
60 |
Correct |
36 ms |
5312 KB |
Output is correct |
61 |
Correct |
256 ms |
5296 KB |
Output is correct |
62 |
Correct |
777 ms |
5688 KB |
Output is correct |
63 |
Correct |
60 ms |
5680 KB |
Output is correct |
64 |
Correct |
350 ms |
5716 KB |
Output is correct |
65 |
Correct |
3 ms |
5020 KB |
Output is correct |
66 |
Correct |
3 ms |
4948 KB |
Output is correct |
67 |
Correct |
4 ms |
4984 KB |
Output is correct |
68 |
Correct |
3 ms |
5020 KB |
Output is correct |
69 |
Correct |
3 ms |
4948 KB |
Output is correct |
70 |
Correct |
4 ms |
4948 KB |
Output is correct |
71 |
Correct |
48 ms |
5024 KB |
Output is correct |
72 |
Correct |
6 ms |
5076 KB |
Output is correct |
73 |
Correct |
16 ms |
5036 KB |
Output is correct |
74 |
Correct |
16 ms |
5288 KB |
Output is correct |
75 |
Correct |
30 ms |
5312 KB |
Output is correct |
76 |
Correct |
196 ms |
5324 KB |
Output is correct |
77 |
Correct |
579 ms |
5708 KB |
Output is correct |
78 |
Correct |
44 ms |
5676 KB |
Output is correct |
79 |
Correct |
268 ms |
5676 KB |
Output is correct |
80 |
Correct |
35 ms |
5588 KB |
Output is correct |
81 |
Correct |
85 ms |
5680 KB |
Output is correct |
82 |
Correct |
576 ms |
5680 KB |
Output is correct |
83 |
Correct |
3 ms |
5076 KB |
Output is correct |
84 |
Correct |
3 ms |
4948 KB |
Output is correct |
85 |
Correct |
43 ms |
5076 KB |
Output is correct |
86 |
Correct |
6 ms |
5092 KB |
Output is correct |
87 |
Correct |
14 ms |
5076 KB |
Output is correct |
88 |
Correct |
16 ms |
5640 KB |
Output is correct |
89 |
Correct |
28 ms |
5588 KB |
Output is correct |
90 |
Correct |
168 ms |
5708 KB |
Output is correct |
91 |
Correct |
487 ms |
6408 KB |
Output is correct |
92 |
Correct |
45 ms |
6216 KB |
Output is correct |
93 |
Correct |
233 ms |
6264 KB |
Output is correct |
94 |
Correct |
6 ms |
5076 KB |
Output is correct |
95 |
Correct |
66 ms |
5076 KB |
Output is correct |
96 |
Correct |
4 ms |
4948 KB |
Output is correct |
97 |
Correct |
3 ms |
4948 KB |
Output is correct |
98 |
Correct |
3 ms |
5004 KB |
Output is correct |
99 |
Correct |
3 ms |
4948 KB |
Output is correct |