#pragma GCC optimize("O3", "unroll-loops") // "Ofast"
#pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt")
#include <bits/stdc++.h>
//#define int long long
#define vt vector
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define ff first
#define ss second
#define dbg(x) cerr << #x << " = " << x << '\n'
#define bit(x, i) ((x) >> (i) & 1)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvt = vt< vt<int> >;
const int N = 1e6 + 5, mod = 1e9 + 7, B = 500;
const ll inf = 1e18 + 7, LIM = (1ll << 60);
const double eps = 1e-6;
int n, k;
ll a[N], p[N];
pll dp[N][2];
ll calc (ll x) {
for (int i = 1; i <= n; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
ll res = dp[i - 1][0].ff - x;
int kol = dp[i - 1][0].ss - 1;
dp[i][1] = max(dp[i - 1][1], {res, kol});
dp[i][1].ff += a[i];
dp[i][1] = max(dp[i][1], {a[i], 0});
}
return max(dp[n][0], dp[n][1]).ss * -1;
}
bool check (ll x) {
return (calc(x) < k);
}
void solve () {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
ll l = 0, r = inf, ans = 0;
while (l <= r) {
ll mid = l + r >> 1;
if (check(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
calc(ans);
cout << max(dp[n][0], dp[n][1]).ff + ans * (k - 1);
cout << '\n';
}
bool testcases = 0;
signed main() {
#ifdef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
cin.tie(0) -> sync_with_stdio(0);
int test = 1;
if (testcases) cin >> test;
for (int cs = 1; cs <= test; cs++) {
solve();
}
}
Compilation message
feast.cpp: In function 'void solve()':
feast.cpp:51:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | ll mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
11724 KB |
Output is correct |
2 |
Correct |
123 ms |
11908 KB |
Output is correct |
3 |
Correct |
123 ms |
12044 KB |
Output is correct |
4 |
Correct |
124 ms |
11964 KB |
Output is correct |
5 |
Correct |
123 ms |
11892 KB |
Output is correct |
6 |
Correct |
118 ms |
11604 KB |
Output is correct |
7 |
Correct |
119 ms |
11596 KB |
Output is correct |
8 |
Correct |
122 ms |
11928 KB |
Output is correct |
9 |
Correct |
117 ms |
11724 KB |
Output is correct |
10 |
Correct |
120 ms |
11848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
11732 KB |
Output is correct |
2 |
Correct |
116 ms |
12040 KB |
Output is correct |
3 |
Correct |
111 ms |
11756 KB |
Output is correct |
4 |
Correct |
111 ms |
11832 KB |
Output is correct |
5 |
Correct |
123 ms |
11604 KB |
Output is correct |
6 |
Correct |
112 ms |
11692 KB |
Output is correct |
7 |
Correct |
110 ms |
12056 KB |
Output is correct |
8 |
Correct |
123 ms |
11988 KB |
Output is correct |
9 |
Correct |
118 ms |
11624 KB |
Output is correct |
10 |
Correct |
113 ms |
11880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
11856 KB |
Output is correct |
2 |
Correct |
129 ms |
11780 KB |
Output is correct |
3 |
Correct |
127 ms |
11852 KB |
Output is correct |
4 |
Correct |
128 ms |
11740 KB |
Output is correct |
5 |
Correct |
129 ms |
11836 KB |
Output is correct |
6 |
Correct |
132 ms |
11956 KB |
Output is correct |
7 |
Correct |
129 ms |
11976 KB |
Output is correct |
8 |
Correct |
129 ms |
12004 KB |
Output is correct |
9 |
Correct |
132 ms |
12040 KB |
Output is correct |
10 |
Correct |
132 ms |
12004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
11724 KB |
Output is correct |
2 |
Correct |
123 ms |
11908 KB |
Output is correct |
3 |
Correct |
123 ms |
12044 KB |
Output is correct |
4 |
Correct |
124 ms |
11964 KB |
Output is correct |
5 |
Correct |
123 ms |
11892 KB |
Output is correct |
6 |
Correct |
118 ms |
11604 KB |
Output is correct |
7 |
Correct |
119 ms |
11596 KB |
Output is correct |
8 |
Correct |
122 ms |
11928 KB |
Output is correct |
9 |
Correct |
117 ms |
11724 KB |
Output is correct |
10 |
Correct |
120 ms |
11848 KB |
Output is correct |
11 |
Correct |
108 ms |
11732 KB |
Output is correct |
12 |
Correct |
116 ms |
12040 KB |
Output is correct |
13 |
Correct |
111 ms |
11756 KB |
Output is correct |
14 |
Correct |
111 ms |
11832 KB |
Output is correct |
15 |
Correct |
123 ms |
11604 KB |
Output is correct |
16 |
Correct |
112 ms |
11692 KB |
Output is correct |
17 |
Correct |
110 ms |
12056 KB |
Output is correct |
18 |
Correct |
123 ms |
11988 KB |
Output is correct |
19 |
Correct |
118 ms |
11624 KB |
Output is correct |
20 |
Correct |
113 ms |
11880 KB |
Output is correct |
21 |
Correct |
133 ms |
11856 KB |
Output is correct |
22 |
Correct |
129 ms |
11780 KB |
Output is correct |
23 |
Correct |
127 ms |
11852 KB |
Output is correct |
24 |
Correct |
128 ms |
11740 KB |
Output is correct |
25 |
Correct |
129 ms |
11836 KB |
Output is correct |
26 |
Correct |
132 ms |
11956 KB |
Output is correct |
27 |
Correct |
129 ms |
11976 KB |
Output is correct |
28 |
Correct |
129 ms |
12004 KB |
Output is correct |
29 |
Correct |
132 ms |
12040 KB |
Output is correct |
30 |
Correct |
132 ms |
12004 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 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 |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
2 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
159 ms |
11760 KB |
Output is correct |
62 |
Correct |
157 ms |
12052 KB |
Output is correct |
63 |
Correct |
139 ms |
11808 KB |
Output is correct |
64 |
Correct |
169 ms |
11960 KB |
Output is correct |
65 |
Correct |
163 ms |
11952 KB |
Output is correct |
66 |
Correct |
159 ms |
11920 KB |
Output is correct |
67 |
Correct |
158 ms |
11656 KB |
Output is correct |
68 |
Correct |
131 ms |
11972 KB |
Output is correct |
69 |
Correct |
127 ms |
11660 KB |
Output is correct |
70 |
Correct |
128 ms |
11696 KB |
Output is correct |