#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);
for (int i = 1; i <= n; i++) {
// cout << dp[i][0].ff - dp[i][0].ss * ans << ' ' << dp[i][0].ss << " " << dp[i][1].ff - dp[i][1].ss * ans << ' ' << dp[i][1].ss << '\n';
}
// cout << 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 |
126 ms |
14412 KB |
Output is correct |
2 |
Correct |
124 ms |
14764 KB |
Output is correct |
3 |
Correct |
125 ms |
14920 KB |
Output is correct |
4 |
Correct |
135 ms |
14960 KB |
Output is correct |
5 |
Correct |
123 ms |
14744 KB |
Output is correct |
6 |
Correct |
120 ms |
14396 KB |
Output is correct |
7 |
Correct |
131 ms |
14488 KB |
Output is correct |
8 |
Correct |
133 ms |
14788 KB |
Output is correct |
9 |
Correct |
123 ms |
14536 KB |
Output is correct |
10 |
Correct |
122 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
12896 KB |
Output is correct |
2 |
Correct |
130 ms |
13096 KB |
Output is correct |
3 |
Correct |
110 ms |
12748 KB |
Output is correct |
4 |
Correct |
111 ms |
12984 KB |
Output is correct |
5 |
Correct |
122 ms |
14508 KB |
Output is correct |
6 |
Correct |
114 ms |
12876 KB |
Output is correct |
7 |
Correct |
122 ms |
13104 KB |
Output is correct |
8 |
Correct |
135 ms |
14844 KB |
Output is correct |
9 |
Correct |
118 ms |
14452 KB |
Output is correct |
10 |
Correct |
112 ms |
13100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
14948 KB |
Output is correct |
2 |
Correct |
149 ms |
14804 KB |
Output is correct |
3 |
Correct |
137 ms |
14828 KB |
Output is correct |
4 |
Correct |
137 ms |
14684 KB |
Output is correct |
5 |
Correct |
148 ms |
14908 KB |
Output is correct |
6 |
Correct |
139 ms |
15088 KB |
Output is correct |
7 |
Correct |
133 ms |
15052 KB |
Output is correct |
8 |
Correct |
132 ms |
14900 KB |
Output is correct |
9 |
Correct |
145 ms |
15180 KB |
Output is correct |
10 |
Correct |
144 ms |
15056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 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 |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 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 |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 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 |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
428 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
14412 KB |
Output is correct |
2 |
Correct |
124 ms |
14764 KB |
Output is correct |
3 |
Correct |
125 ms |
14920 KB |
Output is correct |
4 |
Correct |
135 ms |
14960 KB |
Output is correct |
5 |
Correct |
123 ms |
14744 KB |
Output is correct |
6 |
Correct |
120 ms |
14396 KB |
Output is correct |
7 |
Correct |
131 ms |
14488 KB |
Output is correct |
8 |
Correct |
133 ms |
14788 KB |
Output is correct |
9 |
Correct |
123 ms |
14536 KB |
Output is correct |
10 |
Correct |
122 ms |
14684 KB |
Output is correct |
11 |
Correct |
118 ms |
12896 KB |
Output is correct |
12 |
Correct |
130 ms |
13096 KB |
Output is correct |
13 |
Correct |
110 ms |
12748 KB |
Output is correct |
14 |
Correct |
111 ms |
12984 KB |
Output is correct |
15 |
Correct |
122 ms |
14508 KB |
Output is correct |
16 |
Correct |
114 ms |
12876 KB |
Output is correct |
17 |
Correct |
122 ms |
13104 KB |
Output is correct |
18 |
Correct |
135 ms |
14844 KB |
Output is correct |
19 |
Correct |
118 ms |
14452 KB |
Output is correct |
20 |
Correct |
112 ms |
13100 KB |
Output is correct |
21 |
Correct |
141 ms |
14948 KB |
Output is correct |
22 |
Correct |
149 ms |
14804 KB |
Output is correct |
23 |
Correct |
137 ms |
14828 KB |
Output is correct |
24 |
Correct |
137 ms |
14684 KB |
Output is correct |
25 |
Correct |
148 ms |
14908 KB |
Output is correct |
26 |
Correct |
139 ms |
15088 KB |
Output is correct |
27 |
Correct |
133 ms |
15052 KB |
Output is correct |
28 |
Correct |
132 ms |
14900 KB |
Output is correct |
29 |
Correct |
145 ms |
15180 KB |
Output is correct |
30 |
Correct |
144 ms |
15056 KB |
Output is correct |
31 |
Correct |
0 ms |
328 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
328 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
328 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 |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
324 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
328 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 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 |
2 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
2 ms |
428 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
2 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
2 ms |
340 KB |
Output is correct |
60 |
Correct |
2 ms |
340 KB |
Output is correct |
61 |
Correct |
173 ms |
14740 KB |
Output is correct |
62 |
Correct |
166 ms |
15180 KB |
Output is correct |
63 |
Correct |
143 ms |
14668 KB |
Output is correct |
64 |
Correct |
182 ms |
14888 KB |
Output is correct |
65 |
Correct |
165 ms |
14984 KB |
Output is correct |
66 |
Correct |
162 ms |
14924 KB |
Output is correct |
67 |
Correct |
159 ms |
14724 KB |
Output is correct |
68 |
Correct |
146 ms |
15012 KB |
Output is correct |
69 |
Correct |
137 ms |
14720 KB |
Output is correct |
70 |
Correct |
151 ms |
14644 KB |
Output is correct |