#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define all(a) a.begin() , a.end()
#define F first
#define S second
using namespace std;
using ll = long long;
const ll N = 3e5+5 , inf = 2e9 + 7;
const ll INF = 300000000000000 , mod = 1e9+7 , P = 6547;
ll n , k , a[N];
pair<ll,ll> dp[N][2];
ll get(ll md){
dp[0][0] = {0,0};
dp[0][1] = {-INF,0};
for(ll i = 1; i <= n; i++){
dp[i][0] = max(dp[i-1][0] , dp[i-1][1]);
pair<ll,ll> c = {dp[i-1][0].F+md+a[i] , dp[i-1][0].S+1};
pair<ll,ll> b = {dp[i-1][1].F+a[i] , dp[i-1][1].S};
dp[i][1] = max(c , b);
}
return (max(dp[n][0],dp[n][1]).S >= k);
}
void solve(){
cin >> n >> k;
for(ll i = 1; i <= n; i++) cin >> a[i];
ll l = -INF , r = 0 , res = 0;
while(l <= r){
ll md = (l+r) >> 1;
if(get(md)){
res = md;
r = md-1;
} else {
l = md+1;
}
}
get(res);
cout << max(dp[n][1].F , dp[n][0].F)-res*k << "\n";
}
/*
*/
signed main(){
ios;
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
14676 KB |
Output is correct |
2 |
Correct |
74 ms |
14788 KB |
Output is correct |
3 |
Correct |
67 ms |
14932 KB |
Output is correct |
4 |
Correct |
74 ms |
15304 KB |
Output is correct |
5 |
Correct |
65 ms |
14804 KB |
Output is correct |
6 |
Correct |
69 ms |
14916 KB |
Output is correct |
7 |
Correct |
64 ms |
14740 KB |
Output is correct |
8 |
Correct |
65 ms |
15012 KB |
Output is correct |
9 |
Correct |
68 ms |
14924 KB |
Output is correct |
10 |
Correct |
65 ms |
14932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
13144 KB |
Output is correct |
2 |
Correct |
57 ms |
13316 KB |
Output is correct |
3 |
Correct |
57 ms |
13148 KB |
Output is correct |
4 |
Correct |
59 ms |
13148 KB |
Output is correct |
5 |
Correct |
62 ms |
14680 KB |
Output is correct |
6 |
Correct |
54 ms |
13148 KB |
Output is correct |
7 |
Correct |
58 ms |
13096 KB |
Output is correct |
8 |
Correct |
64 ms |
14984 KB |
Output is correct |
9 |
Correct |
64 ms |
14684 KB |
Output is correct |
10 |
Correct |
65 ms |
13292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
14936 KB |
Output is correct |
2 |
Correct |
82 ms |
15112 KB |
Output is correct |
3 |
Correct |
84 ms |
15184 KB |
Output is correct |
4 |
Correct |
82 ms |
15004 KB |
Output is correct |
5 |
Correct |
83 ms |
14940 KB |
Output is correct |
6 |
Correct |
84 ms |
15196 KB |
Output is correct |
7 |
Correct |
83 ms |
15220 KB |
Output is correct |
8 |
Correct |
82 ms |
15164 KB |
Output is correct |
9 |
Correct |
84 ms |
15228 KB |
Output is correct |
10 |
Correct |
84 ms |
15224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2516 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2400 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2516 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2400 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2516 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
2 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
14676 KB |
Output is correct |
2 |
Correct |
74 ms |
14788 KB |
Output is correct |
3 |
Correct |
67 ms |
14932 KB |
Output is correct |
4 |
Correct |
74 ms |
15304 KB |
Output is correct |
5 |
Correct |
65 ms |
14804 KB |
Output is correct |
6 |
Correct |
69 ms |
14916 KB |
Output is correct |
7 |
Correct |
64 ms |
14740 KB |
Output is correct |
8 |
Correct |
65 ms |
15012 KB |
Output is correct |
9 |
Correct |
68 ms |
14924 KB |
Output is correct |
10 |
Correct |
65 ms |
14932 KB |
Output is correct |
11 |
Correct |
56 ms |
13144 KB |
Output is correct |
12 |
Correct |
57 ms |
13316 KB |
Output is correct |
13 |
Correct |
57 ms |
13148 KB |
Output is correct |
14 |
Correct |
59 ms |
13148 KB |
Output is correct |
15 |
Correct |
62 ms |
14680 KB |
Output is correct |
16 |
Correct |
54 ms |
13148 KB |
Output is correct |
17 |
Correct |
58 ms |
13096 KB |
Output is correct |
18 |
Correct |
64 ms |
14984 KB |
Output is correct |
19 |
Correct |
64 ms |
14684 KB |
Output is correct |
20 |
Correct |
65 ms |
13292 KB |
Output is correct |
21 |
Correct |
83 ms |
14936 KB |
Output is correct |
22 |
Correct |
82 ms |
15112 KB |
Output is correct |
23 |
Correct |
84 ms |
15184 KB |
Output is correct |
24 |
Correct |
82 ms |
15004 KB |
Output is correct |
25 |
Correct |
83 ms |
14940 KB |
Output is correct |
26 |
Correct |
84 ms |
15196 KB |
Output is correct |
27 |
Correct |
83 ms |
15220 KB |
Output is correct |
28 |
Correct |
82 ms |
15164 KB |
Output is correct |
29 |
Correct |
84 ms |
15228 KB |
Output is correct |
30 |
Correct |
84 ms |
15224 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
1 ms |
2516 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2400 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2516 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
2 ms |
2396 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
1 ms |
2396 KB |
Output is correct |
61 |
Correct |
114 ms |
15108 KB |
Output is correct |
62 |
Correct |
114 ms |
15236 KB |
Output is correct |
63 |
Correct |
95 ms |
14940 KB |
Output is correct |
64 |
Correct |
115 ms |
15188 KB |
Output is correct |
65 |
Correct |
116 ms |
15188 KB |
Output is correct |
66 |
Correct |
117 ms |
15176 KB |
Output is correct |
67 |
Correct |
114 ms |
15100 KB |
Output is correct |
68 |
Correct |
85 ms |
15124 KB |
Output is correct |
69 |
Correct |
81 ms |
14932 KB |
Output is correct |
70 |
Correct |
81 ms |
14940 KB |
Output is correct |