#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define gc getchar
#define pc putchar
int in(){
char c=gc(); bool neg=false;
for(; c<'0'||'9'<c; c=gc())
if(c=='-') neg=true;
int rs=c-'0'; c=gc();
for(; '0'<=c && c<='9'; c=gc())
rs=(rs<<1)+(rs<<3)+(c-'0');
if(neg) rs=-rs;
return rs;
}
void out(ll _n){
ll N=_n, rev, count=0;
rev=N;
if(N==0) {pc('0'); return;}
while((rev%10ll)==0) {count++; rev/=10;}
rev=0;
while(N!=0) {rev=(rev<<3ll)+(rev<<1ll)+N%10; N/=10;}
while(rev!=0) {pc(rev%10ll+'0'); rev/=10;}
while(count--) pc('0');
}
int n, k, val[300005];
pair<ll, int> dp[300005][2];
pair<ll, int> solve(ll x){
for(int i=1; i<=n; i++){
if(dp[i-1][0].first>dp[i-1][1].first-x)
dp[i][0]=dp[i-1][0];
else if(dp[i-1][0].first<dp[i-1][1].first-x)
dp[i][0]={dp[i-1][1].first-x, dp[i-1][1].second+1};
else if(dp[i-1][0].second<dp[i-1][1].second+1)
dp[i][0]=dp[i-1][0];
else dp[i][0]={dp[i-1][1].first-x, dp[i-1][1].second+1};
if(dp[i-1][0].first>dp[i-1][1].first)
dp[i][1]=dp[i-1][0];
else if(dp[i-1][0].first<dp[i-1][1].first)
dp[i][1]=dp[i-1][1];
else if(dp[i-1][0].second<dp[i-1][1].second)
dp[i][1]=dp[i-1][0];
else dp[i][1]=dp[i-1][1];
dp[i][1]={dp[i][1].first+val[i], dp[i][1].second};
}
pair<ll, int> ans;
if(dp[n][0].first>dp[n][1].first-x)
ans=dp[n][0];
else if(dp[n][0].first<dp[n][1].first-x)
ans={dp[n][1].first-x, dp[n][1].second+1};
else if(dp[n][0].second<dp[n][1].second+1)
ans=dp[n][0];
else ans={dp[n][1].first-x, dp[n][1].second+1};
return ans;
}
int main(){
n=in(), k=in();
for(int i=1; i<=n; i++) val[i]=in();
ll l=0, r=3e14, md;
while(l<=r){
md=(l+r)/2;
// cout<<md<<" "<<solve(md).first<<" "<<solve(md).second<<endl;
if(solve(md).second>k) l=md+1;
else r=md-1;
}
// cout<<l<<endl;
ll ans=solve(l).first+l*(ll)k;
out(ans); pc('\n'); return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
10488 KB |
Output is correct |
2 |
Correct |
131 ms |
10872 KB |
Output is correct |
3 |
Correct |
132 ms |
11000 KB |
Output is correct |
4 |
Correct |
133 ms |
11012 KB |
Output is correct |
5 |
Correct |
131 ms |
10872 KB |
Output is correct |
6 |
Correct |
129 ms |
10616 KB |
Output is correct |
7 |
Correct |
128 ms |
10616 KB |
Output is correct |
8 |
Correct |
130 ms |
10872 KB |
Output is correct |
9 |
Correct |
129 ms |
10616 KB |
Output is correct |
10 |
Correct |
132 ms |
10744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
10744 KB |
Output is correct |
2 |
Correct |
113 ms |
11004 KB |
Output is correct |
3 |
Correct |
108 ms |
10752 KB |
Output is correct |
4 |
Correct |
110 ms |
10752 KB |
Output is correct |
5 |
Correct |
128 ms |
10616 KB |
Output is correct |
6 |
Correct |
111 ms |
10748 KB |
Output is correct |
7 |
Correct |
113 ms |
11104 KB |
Output is correct |
8 |
Correct |
133 ms |
10880 KB |
Output is correct |
9 |
Correct |
129 ms |
10680 KB |
Output is correct |
10 |
Correct |
110 ms |
10752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
10940 KB |
Output is correct |
2 |
Correct |
147 ms |
10872 KB |
Output is correct |
3 |
Correct |
148 ms |
11000 KB |
Output is correct |
4 |
Correct |
146 ms |
10872 KB |
Output is correct |
5 |
Correct |
150 ms |
11128 KB |
Output is correct |
6 |
Correct |
150 ms |
11076 KB |
Output is correct |
7 |
Correct |
157 ms |
11128 KB |
Output is correct |
8 |
Correct |
146 ms |
11000 KB |
Output is correct |
9 |
Correct |
152 ms |
11128 KB |
Output is correct |
10 |
Correct |
149 ms |
11128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
6 ms |
384 KB |
Output is correct |
23 |
Correct |
6 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
6 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
10488 KB |
Output is correct |
2 |
Correct |
131 ms |
10872 KB |
Output is correct |
3 |
Correct |
132 ms |
11000 KB |
Output is correct |
4 |
Correct |
133 ms |
11012 KB |
Output is correct |
5 |
Correct |
131 ms |
10872 KB |
Output is correct |
6 |
Correct |
129 ms |
10616 KB |
Output is correct |
7 |
Correct |
128 ms |
10616 KB |
Output is correct |
8 |
Correct |
130 ms |
10872 KB |
Output is correct |
9 |
Correct |
129 ms |
10616 KB |
Output is correct |
10 |
Correct |
132 ms |
10744 KB |
Output is correct |
11 |
Correct |
109 ms |
10744 KB |
Output is correct |
12 |
Correct |
113 ms |
11004 KB |
Output is correct |
13 |
Correct |
108 ms |
10752 KB |
Output is correct |
14 |
Correct |
110 ms |
10752 KB |
Output is correct |
15 |
Correct |
128 ms |
10616 KB |
Output is correct |
16 |
Correct |
111 ms |
10748 KB |
Output is correct |
17 |
Correct |
113 ms |
11104 KB |
Output is correct |
18 |
Correct |
133 ms |
10880 KB |
Output is correct |
19 |
Correct |
129 ms |
10680 KB |
Output is correct |
20 |
Correct |
110 ms |
10752 KB |
Output is correct |
21 |
Correct |
152 ms |
10940 KB |
Output is correct |
22 |
Correct |
147 ms |
10872 KB |
Output is correct |
23 |
Correct |
148 ms |
11000 KB |
Output is correct |
24 |
Correct |
146 ms |
10872 KB |
Output is correct |
25 |
Correct |
150 ms |
11128 KB |
Output is correct |
26 |
Correct |
150 ms |
11076 KB |
Output is correct |
27 |
Correct |
157 ms |
11128 KB |
Output is correct |
28 |
Correct |
146 ms |
11000 KB |
Output is correct |
29 |
Correct |
152 ms |
11128 KB |
Output is correct |
30 |
Correct |
149 ms |
11128 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
4 ms |
384 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
4 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
512 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
4 ms |
384 KB |
Output is correct |
40 |
Correct |
4 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
5 ms |
384 KB |
Output is correct |
43 |
Correct |
5 ms |
384 KB |
Output is correct |
44 |
Correct |
4 ms |
384 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
384 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
5 ms |
384 KB |
Output is correct |
49 |
Correct |
5 ms |
384 KB |
Output is correct |
50 |
Correct |
4 ms |
384 KB |
Output is correct |
51 |
Correct |
5 ms |
384 KB |
Output is correct |
52 |
Correct |
6 ms |
384 KB |
Output is correct |
53 |
Correct |
6 ms |
384 KB |
Output is correct |
54 |
Correct |
5 ms |
384 KB |
Output is correct |
55 |
Correct |
5 ms |
384 KB |
Output is correct |
56 |
Correct |
6 ms |
384 KB |
Output is correct |
57 |
Correct |
6 ms |
384 KB |
Output is correct |
58 |
Correct |
6 ms |
384 KB |
Output is correct |
59 |
Correct |
5 ms |
384 KB |
Output is correct |
60 |
Correct |
5 ms |
384 KB |
Output is correct |
61 |
Correct |
179 ms |
10876 KB |
Output is correct |
62 |
Correct |
183 ms |
13944 KB |
Output is correct |
63 |
Correct |
161 ms |
13560 KB |
Output is correct |
64 |
Correct |
187 ms |
13944 KB |
Output is correct |
65 |
Correct |
195 ms |
13816 KB |
Output is correct |
66 |
Correct |
187 ms |
13816 KB |
Output is correct |
67 |
Correct |
190 ms |
13816 KB |
Output is correct |
68 |
Correct |
155 ms |
13816 KB |
Output is correct |
69 |
Correct |
148 ms |
13560 KB |
Output is correct |
70 |
Correct |
148 ms |
13496 KB |
Output is correct |