#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 3e5 + 5;
int a[maxn], N, K;
pair<ll, int> f[maxn][2];
pair<ll, int> best(pair<ll, int> a, pair<ll, int> b)
{
if(a.fi == b.fi) return a.se < b.se ? a : b;
return a.fi > b.fi ? a : b;
}
pair<ll, int> operator - (pair<ll, int> a, ll x)
{
return mp(a.fi - x, a.se + 1);
}
pair<ll, int> operator + (pair<ll, int> a, ll x)
{
return mp(a.fi + x, a.se);
}
pair<ll, int> solve(ll cost)
{
for(int i = 1; i <= N; ++i){
f[i][0] = best(f[i - 1][0], f[i - 1][1] - cost);
f[i][1] = best(f[i - 1][0], f[i - 1][1]) + a[i];
}
return best(f[N][0], f[N][1] - cost);
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
#endif // LOCAL
cin >> N >> K;
for(int i = 1; i <= N; ++i){
cin >> a[i];
}
ll l = 0, r = 1e15, mid;
while(l <= r){
mid = (l + r) / 2;
if(solve(mid).se > K) l = mid + 1;
else r = mid - 1;
}
cout << solve(l).fi + l * K;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
256 ms |
11000 KB |
Output is correct |
2 |
Correct |
259 ms |
11000 KB |
Output is correct |
3 |
Correct |
264 ms |
11176 KB |
Output is correct |
4 |
Correct |
262 ms |
11152 KB |
Output is correct |
5 |
Correct |
259 ms |
11000 KB |
Output is correct |
6 |
Correct |
257 ms |
11000 KB |
Output is correct |
7 |
Correct |
258 ms |
10872 KB |
Output is correct |
8 |
Correct |
264 ms |
11128 KB |
Output is correct |
9 |
Correct |
259 ms |
11000 KB |
Output is correct |
10 |
Correct |
258 ms |
11004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
10872 KB |
Output is correct |
2 |
Correct |
240 ms |
11136 KB |
Output is correct |
3 |
Correct |
233 ms |
10872 KB |
Output is correct |
4 |
Correct |
232 ms |
11128 KB |
Output is correct |
5 |
Correct |
271 ms |
10872 KB |
Output is correct |
6 |
Correct |
234 ms |
10872 KB |
Output is correct |
7 |
Correct |
236 ms |
11256 KB |
Output is correct |
8 |
Correct |
261 ms |
11128 KB |
Output is correct |
9 |
Correct |
258 ms |
10744 KB |
Output is correct |
10 |
Correct |
234 ms |
11132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
245 ms |
11000 KB |
Output is correct |
2 |
Correct |
245 ms |
13688 KB |
Output is correct |
3 |
Correct |
246 ms |
13816 KB |
Output is correct |
4 |
Correct |
243 ms |
13688 KB |
Output is correct |
5 |
Correct |
248 ms |
13816 KB |
Output is correct |
6 |
Correct |
248 ms |
13816 KB |
Output is correct |
7 |
Correct |
247 ms |
13944 KB |
Output is correct |
8 |
Correct |
247 ms |
13944 KB |
Output is correct |
9 |
Correct |
248 ms |
13944 KB |
Output is correct |
10 |
Correct |
247 ms |
13944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
20 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
20 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 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 |
5 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 |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
20 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 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 |
5 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 |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
7 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
6 ms |
384 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
25 |
Correct |
6 ms |
384 KB |
Output is correct |
26 |
Correct |
6 ms |
384 KB |
Output is correct |
27 |
Correct |
7 ms |
512 KB |
Output is correct |
28 |
Correct |
6 ms |
512 KB |
Output is correct |
29 |
Correct |
6 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
256 ms |
11000 KB |
Output is correct |
2 |
Correct |
259 ms |
11000 KB |
Output is correct |
3 |
Correct |
264 ms |
11176 KB |
Output is correct |
4 |
Correct |
262 ms |
11152 KB |
Output is correct |
5 |
Correct |
259 ms |
11000 KB |
Output is correct |
6 |
Correct |
257 ms |
11000 KB |
Output is correct |
7 |
Correct |
258 ms |
10872 KB |
Output is correct |
8 |
Correct |
264 ms |
11128 KB |
Output is correct |
9 |
Correct |
259 ms |
11000 KB |
Output is correct |
10 |
Correct |
258 ms |
11004 KB |
Output is correct |
11 |
Correct |
223 ms |
10872 KB |
Output is correct |
12 |
Correct |
240 ms |
11136 KB |
Output is correct |
13 |
Correct |
233 ms |
10872 KB |
Output is correct |
14 |
Correct |
232 ms |
11128 KB |
Output is correct |
15 |
Correct |
271 ms |
10872 KB |
Output is correct |
16 |
Correct |
234 ms |
10872 KB |
Output is correct |
17 |
Correct |
236 ms |
11256 KB |
Output is correct |
18 |
Correct |
261 ms |
11128 KB |
Output is correct |
19 |
Correct |
258 ms |
10744 KB |
Output is correct |
20 |
Correct |
234 ms |
11132 KB |
Output is correct |
21 |
Correct |
245 ms |
11000 KB |
Output is correct |
22 |
Correct |
245 ms |
13688 KB |
Output is correct |
23 |
Correct |
246 ms |
13816 KB |
Output is correct |
24 |
Correct |
243 ms |
13688 KB |
Output is correct |
25 |
Correct |
248 ms |
13816 KB |
Output is correct |
26 |
Correct |
248 ms |
13816 KB |
Output is correct |
27 |
Correct |
247 ms |
13944 KB |
Output is correct |
28 |
Correct |
247 ms |
13944 KB |
Output is correct |
29 |
Correct |
248 ms |
13944 KB |
Output is correct |
30 |
Correct |
247 ms |
13944 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
20 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
5 ms |
384 KB |
Output is correct |
40 |
Correct |
5 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 |
5 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 |
5 ms |
384 KB |
Output is correct |
51 |
Correct |
7 ms |
512 KB |
Output is correct |
52 |
Correct |
6 ms |
512 KB |
Output is correct |
53 |
Correct |
6 ms |
384 KB |
Output is correct |
54 |
Correct |
6 ms |
512 KB |
Output is correct |
55 |
Correct |
6 ms |
384 KB |
Output is correct |
56 |
Correct |
6 ms |
384 KB |
Output is correct |
57 |
Correct |
7 ms |
512 KB |
Output is correct |
58 |
Correct |
6 ms |
512 KB |
Output is correct |
59 |
Correct |
6 ms |
384 KB |
Output is correct |
60 |
Correct |
6 ms |
384 KB |
Output is correct |
61 |
Correct |
287 ms |
13628 KB |
Output is correct |
62 |
Correct |
289 ms |
14072 KB |
Output is correct |
63 |
Correct |
253 ms |
13688 KB |
Output is correct |
64 |
Correct |
288 ms |
13944 KB |
Output is correct |
65 |
Correct |
277 ms |
13944 KB |
Output is correct |
66 |
Correct |
278 ms |
13976 KB |
Output is correct |
67 |
Correct |
269 ms |
13688 KB |
Output is correct |
68 |
Correct |
254 ms |
13944 KB |
Output is correct |
69 |
Correct |
244 ms |
13560 KB |
Output is correct |
70 |
Correct |
243 ms |
13560 KB |
Output is correct |