/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define eb emplace_back
#define PI acos(-1.0)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define BIT(x, i) (1 & ((x) >> (i)))
#define MASK(x) (1LL << (x))
#define task "tnc"
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
typedef long double ld;
const ll INF = 1e14;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
const int mo = 998244353;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using pii = pair<pair<ll, ll>, ll>;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll n, k;
ll a[maxn];
ll dp[maxn];
ll trace[maxn];
ll pre[maxn];
ll f(ll mid)
{
// dp[i] = max(dp[i-1],dp[j]+dp[i]-pre[j]+pre[i]-mid);
dp[0] = 0;
ll ma = 0;
ll vt = 0;
trace[0] = 0;
for (int i = 1; i <= n; i++)
{
dp[i] = 0;
trace[i] = 0;
}
for (int i = 1; i <= n; i++)
{
if (dp[i - 1] <= ma + pre[i] - mid)
{
dp[i] = ma + pre[i] - mid;
trace[i] = trace[vt] + 1;
}
else
{
dp[i] = dp[i - 1];
trace[i] = trace[i - 1];
}
if (ma <= dp[i] - pre[i])
{
ma = dp[i] - pre[i];
vt = i;
}
}
return trace[n];
}
void solve()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
pre[i] = pre[i - 1] + a[i];
}
ll l = -INF;
ll r = INF;
ll ans;
ll ans1;
while (l <= r)
{
ll mid = (l + r) / 2;
if (f(mid) >= k)
{
ans = dp[n];
ans1 = mid;
l = mid + 1;
}
else
{
r = mid - 1;
}
}
if (ans1 <= 0)
{
f(0);
cout << dp[n];
}
else
{
cout << ans + ans1 * k;
}
}
signed main()
{
cin.tie(0), cout.tie(0)->sync_with_stdio(0);
solve();
}
Compilation message
feast.cpp: In function 'void solve()':
feast.cpp:108:28: warning: 'ans1' may be used uninitialized in this function [-Wmaybe-uninitialized]
108 | cout << ans + ans1 * k;
| ~~~~~^~~
feast.cpp:108:30: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
108 | cout << ans + ans1 * k;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
9404 KB |
Output is correct |
2 |
Correct |
47 ms |
9620 KB |
Output is correct |
3 |
Correct |
48 ms |
9716 KB |
Output is correct |
4 |
Correct |
52 ms |
9668 KB |
Output is correct |
5 |
Correct |
47 ms |
9608 KB |
Output is correct |
6 |
Correct |
46 ms |
9428 KB |
Output is correct |
7 |
Correct |
46 ms |
9420 KB |
Output is correct |
8 |
Correct |
46 ms |
9564 KB |
Output is correct |
9 |
Correct |
46 ms |
9472 KB |
Output is correct |
10 |
Correct |
51 ms |
9556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
9524 KB |
Output is correct |
2 |
Correct |
54 ms |
9716 KB |
Output is correct |
3 |
Correct |
55 ms |
9504 KB |
Output is correct |
4 |
Correct |
46 ms |
9508 KB |
Output is correct |
5 |
Correct |
61 ms |
11564 KB |
Output is correct |
6 |
Correct |
57 ms |
10520 KB |
Output is correct |
7 |
Correct |
46 ms |
10876 KB |
Output is correct |
8 |
Correct |
58 ms |
11696 KB |
Output is correct |
9 |
Correct |
58 ms |
11480 KB |
Output is correct |
10 |
Correct |
50 ms |
10888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
9516 KB |
Output is correct |
2 |
Correct |
69 ms |
9464 KB |
Output is correct |
3 |
Correct |
69 ms |
9600 KB |
Output is correct |
4 |
Correct |
68 ms |
9428 KB |
Output is correct |
5 |
Correct |
69 ms |
9588 KB |
Output is correct |
6 |
Correct |
70 ms |
9544 KB |
Output is correct |
7 |
Correct |
70 ms |
9684 KB |
Output is correct |
8 |
Correct |
70 ms |
9600 KB |
Output is correct |
9 |
Correct |
71 ms |
9716 KB |
Output is correct |
10 |
Correct |
78 ms |
9692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 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 |
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 |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 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 |
52 ms |
9404 KB |
Output is correct |
2 |
Correct |
47 ms |
9620 KB |
Output is correct |
3 |
Correct |
48 ms |
9716 KB |
Output is correct |
4 |
Correct |
52 ms |
9668 KB |
Output is correct |
5 |
Correct |
47 ms |
9608 KB |
Output is correct |
6 |
Correct |
46 ms |
9428 KB |
Output is correct |
7 |
Correct |
46 ms |
9420 KB |
Output is correct |
8 |
Correct |
46 ms |
9564 KB |
Output is correct |
9 |
Correct |
46 ms |
9472 KB |
Output is correct |
10 |
Correct |
51 ms |
9556 KB |
Output is correct |
11 |
Correct |
52 ms |
9524 KB |
Output is correct |
12 |
Correct |
54 ms |
9716 KB |
Output is correct |
13 |
Correct |
55 ms |
9504 KB |
Output is correct |
14 |
Correct |
46 ms |
9508 KB |
Output is correct |
15 |
Correct |
61 ms |
11564 KB |
Output is correct |
16 |
Correct |
57 ms |
10520 KB |
Output is correct |
17 |
Correct |
46 ms |
10876 KB |
Output is correct |
18 |
Correct |
58 ms |
11696 KB |
Output is correct |
19 |
Correct |
58 ms |
11480 KB |
Output is correct |
20 |
Correct |
50 ms |
10888 KB |
Output is correct |
21 |
Correct |
71 ms |
9516 KB |
Output is correct |
22 |
Correct |
69 ms |
9464 KB |
Output is correct |
23 |
Correct |
69 ms |
9600 KB |
Output is correct |
24 |
Correct |
68 ms |
9428 KB |
Output is correct |
25 |
Correct |
69 ms |
9588 KB |
Output is correct |
26 |
Correct |
70 ms |
9544 KB |
Output is correct |
27 |
Correct |
70 ms |
9684 KB |
Output is correct |
28 |
Correct |
70 ms |
9600 KB |
Output is correct |
29 |
Correct |
71 ms |
9716 KB |
Output is correct |
30 |
Correct |
78 ms |
9692 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
0 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
336 KB |
Output is correct |
47 |
Correct |
1 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 |
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 |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 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 |
107 ms |
11524 KB |
Output is correct |
62 |
Correct |
106 ms |
11784 KB |
Output is correct |
63 |
Correct |
84 ms |
11424 KB |
Output is correct |
64 |
Correct |
107 ms |
11584 KB |
Output is correct |
65 |
Correct |
104 ms |
11704 KB |
Output is correct |
66 |
Correct |
103 ms |
11560 KB |
Output is correct |
67 |
Correct |
102 ms |
11504 KB |
Output is correct |
68 |
Correct |
74 ms |
11592 KB |
Output is correct |
69 |
Correct |
75 ms |
11532 KB |
Output is correct |
70 |
Correct |
72 ms |
11480 KB |
Output is correct |