#include <bits/stdc++.h>
#define f first
#define s second
#define m_p make_pair
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
#define int long long
#define fast_resp ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const ll inf=1e18;
signed main(){
fast_resp;
int n,k;
cin>>n>>k;
vec<int>a(n);
for(auto &z : a) cin>>z;
vec<ll>pref(n,0);
for(int i=0;i<n;i++) pref[i]=(i?pref[i-1]:0)+a[i];
auto check=[&](ll x){
vec<pll>dp(n+1,m_p(-inf,-1));
pll mx={0,0};
dp[0]={0,0};
// cout<<"CHECK "<<x<<endl;
for(int i=0;i<n;i++){
dp[i+1]=dp[i];
umax(dp[i+1],m_p(dp[i].f-x,dp[i].s+1));
umax(dp[i+1],m_p(mx.f+pref[i]-x,mx.s+1));
umax(mx,m_p(dp[i+1].f-pref[i],dp[i+1].s));
// cout<<"I "<<dp[i+1].f<<endl;
}
// cout<<"W "<<dp[n].f<<' '<<dp[n].s<<endl;
return dp[n];
};
ll l=0,r=1e18;
while(l!=r){
ll tm=(l+r)/2;
if(check(tm).s<k) r=tm;
else l=tm+1;
}
--l;
// cout<<k<<' '<<l<<endl;
cout<<check(l).f+1ll*k*l;
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
9412 KB |
Output is correct |
2 |
Correct |
99 ms |
9560 KB |
Output is correct |
3 |
Correct |
93 ms |
9720 KB |
Output is correct |
4 |
Correct |
102 ms |
9660 KB |
Output is correct |
5 |
Correct |
108 ms |
9552 KB |
Output is correct |
6 |
Correct |
106 ms |
9420 KB |
Output is correct |
7 |
Correct |
95 ms |
9356 KB |
Output is correct |
8 |
Correct |
99 ms |
9608 KB |
Output is correct |
9 |
Correct |
98 ms |
9440 KB |
Output is correct |
10 |
Correct |
106 ms |
9416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
9460 KB |
Output is correct |
2 |
Correct |
82 ms |
9676 KB |
Output is correct |
3 |
Correct |
86 ms |
9460 KB |
Output is correct |
4 |
Correct |
99 ms |
9544 KB |
Output is correct |
5 |
Correct |
107 ms |
9404 KB |
Output is correct |
6 |
Correct |
79 ms |
9456 KB |
Output is correct |
7 |
Correct |
81 ms |
9724 KB |
Output is correct |
8 |
Correct |
90 ms |
9632 KB |
Output is correct |
9 |
Correct |
108 ms |
9396 KB |
Output is correct |
10 |
Correct |
82 ms |
9636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
9556 KB |
Output is correct |
2 |
Correct |
122 ms |
12472 KB |
Output is correct |
3 |
Correct |
140 ms |
12588 KB |
Output is correct |
4 |
Correct |
124 ms |
12476 KB |
Output is correct |
5 |
Correct |
112 ms |
12588 KB |
Output is correct |
6 |
Correct |
110 ms |
12616 KB |
Output is correct |
7 |
Correct |
121 ms |
12688 KB |
Output is correct |
8 |
Correct |
161 ms |
12584 KB |
Output is correct |
9 |
Correct |
116 ms |
12748 KB |
Output is correct |
10 |
Correct |
115 ms |
12724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
360 KB |
Output is correct |
23 |
Correct |
1 ms |
316 KB |
Output is correct |
24 |
Correct |
1 ms |
360 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
9412 KB |
Output is correct |
2 |
Correct |
99 ms |
9560 KB |
Output is correct |
3 |
Correct |
93 ms |
9720 KB |
Output is correct |
4 |
Correct |
102 ms |
9660 KB |
Output is correct |
5 |
Correct |
108 ms |
9552 KB |
Output is correct |
6 |
Correct |
106 ms |
9420 KB |
Output is correct |
7 |
Correct |
95 ms |
9356 KB |
Output is correct |
8 |
Correct |
99 ms |
9608 KB |
Output is correct |
9 |
Correct |
98 ms |
9440 KB |
Output is correct |
10 |
Correct |
106 ms |
9416 KB |
Output is correct |
11 |
Correct |
81 ms |
9460 KB |
Output is correct |
12 |
Correct |
82 ms |
9676 KB |
Output is correct |
13 |
Correct |
86 ms |
9460 KB |
Output is correct |
14 |
Correct |
99 ms |
9544 KB |
Output is correct |
15 |
Correct |
107 ms |
9404 KB |
Output is correct |
16 |
Correct |
79 ms |
9456 KB |
Output is correct |
17 |
Correct |
81 ms |
9724 KB |
Output is correct |
18 |
Correct |
90 ms |
9632 KB |
Output is correct |
19 |
Correct |
108 ms |
9396 KB |
Output is correct |
20 |
Correct |
82 ms |
9636 KB |
Output is correct |
21 |
Correct |
110 ms |
9556 KB |
Output is correct |
22 |
Correct |
122 ms |
12472 KB |
Output is correct |
23 |
Correct |
140 ms |
12588 KB |
Output is correct |
24 |
Correct |
124 ms |
12476 KB |
Output is correct |
25 |
Correct |
112 ms |
12588 KB |
Output is correct |
26 |
Correct |
110 ms |
12616 KB |
Output is correct |
27 |
Correct |
121 ms |
12688 KB |
Output is correct |
28 |
Correct |
161 ms |
12584 KB |
Output is correct |
29 |
Correct |
116 ms |
12748 KB |
Output is correct |
30 |
Correct |
115 ms |
12724 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
320 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
320 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
324 KB |
Output is correct |
48 |
Correct |
1 ms |
320 KB |
Output is correct |
49 |
Correct |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
52 |
Correct |
1 ms |
360 KB |
Output is correct |
53 |
Correct |
1 ms |
316 KB |
Output is correct |
54 |
Correct |
1 ms |
360 KB |
Output is correct |
55 |
Correct |
1 ms |
364 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
344 KB |
Output is correct |
59 |
Correct |
2 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
157 ms |
12460 KB |
Output is correct |
62 |
Correct |
151 ms |
12776 KB |
Output is correct |
63 |
Correct |
135 ms |
12364 KB |
Output is correct |
64 |
Correct |
163 ms |
12680 KB |
Output is correct |
65 |
Correct |
152 ms |
12612 KB |
Output is correct |
66 |
Correct |
159 ms |
12620 KB |
Output is correct |
67 |
Correct |
177 ms |
12452 KB |
Output is correct |
68 |
Correct |
115 ms |
12684 KB |
Output is correct |
69 |
Correct |
110 ms |
12528 KB |
Output is correct |
70 |
Correct |
122 ms |
12384 KB |
Output is correct |