#include <bits/stdc++.h>
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define int long long
using namespace std;
const int N = 3e5 + 17;
int n, k;
long long a[N];
pair <long long, int> f[N], m;
pair <int, int> calc(int c)
{
pair <int, int> m = {0, 0};
f[0] = {0, 0};
for (int i=1; i<=n; i++)
{
f[i]=f[i-1];
if (m.first + a[i] - c > f[i].first || (m.first + a[i] - c == f[i].first && m.second + 1 < f[i].second))
{
f[i].first = m.first + a[i]-c;
f[i].second = m.second + 1;
}
if (f[i].first - a[i] > m.first || (f[i].first - a[i] == m.first && f[i].second < m.second))
{
m.first = f[i].first - a[i];
m.second = f[i].second;
}
}
return f[n];
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
a[i] += a[i - 1];
}
long long l = 1, r = 3e14;
while (l < r)
{
long long mid = l + r + 1 >> 1;
m = calc (mid);
if (m.second >= k)
{
l = mid;
continue;
}
r = mid - 1;
}
m = calc(l);
long long ans = l * m.second + m.first;
cout << ans;
}
Compilation message
feast.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization ("O3")
|
feast.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
feast.cpp: In function 'int32_t main()':
feast.cpp:46:31: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | long long mid = l + r + 1 >> 1;
| ~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
7248 KB |
Output is correct |
2 |
Correct |
56 ms |
9696 KB |
Output is correct |
3 |
Correct |
50 ms |
7484 KB |
Output is correct |
4 |
Correct |
58 ms |
7244 KB |
Output is correct |
5 |
Correct |
50 ms |
7200 KB |
Output is correct |
6 |
Correct |
56 ms |
7252 KB |
Output is correct |
7 |
Correct |
46 ms |
8520 KB |
Output is correct |
8 |
Correct |
45 ms |
7240 KB |
Output is correct |
9 |
Correct |
48 ms |
9276 KB |
Output is correct |
10 |
Correct |
51 ms |
7628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
7248 KB |
Output is correct |
2 |
Correct |
36 ms |
7248 KB |
Output is correct |
3 |
Correct |
44 ms |
7248 KB |
Output is correct |
4 |
Correct |
40 ms |
7248 KB |
Output is correct |
5 |
Correct |
46 ms |
7248 KB |
Output is correct |
6 |
Correct |
39 ms |
7168 KB |
Output is correct |
7 |
Correct |
45 ms |
8484 KB |
Output is correct |
8 |
Correct |
47 ms |
7248 KB |
Output is correct |
9 |
Correct |
47 ms |
7248 KB |
Output is correct |
10 |
Correct |
40 ms |
7248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
7240 KB |
Output is correct |
2 |
Correct |
62 ms |
7248 KB |
Output is correct |
3 |
Correct |
63 ms |
7316 KB |
Output is correct |
4 |
Correct |
64 ms |
7332 KB |
Output is correct |
5 |
Correct |
64 ms |
7248 KB |
Output is correct |
6 |
Correct |
65 ms |
7284 KB |
Output is correct |
7 |
Correct |
65 ms |
7240 KB |
Output is correct |
8 |
Correct |
64 ms |
7240 KB |
Output is correct |
9 |
Correct |
69 ms |
7244 KB |
Output is correct |
10 |
Correct |
73 ms |
7332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
508 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
508 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
2 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
7248 KB |
Output is correct |
2 |
Correct |
56 ms |
9696 KB |
Output is correct |
3 |
Correct |
50 ms |
7484 KB |
Output is correct |
4 |
Correct |
58 ms |
7244 KB |
Output is correct |
5 |
Correct |
50 ms |
7200 KB |
Output is correct |
6 |
Correct |
56 ms |
7252 KB |
Output is correct |
7 |
Correct |
46 ms |
8520 KB |
Output is correct |
8 |
Correct |
45 ms |
7240 KB |
Output is correct |
9 |
Correct |
48 ms |
9276 KB |
Output is correct |
10 |
Correct |
51 ms |
7628 KB |
Output is correct |
11 |
Correct |
37 ms |
7248 KB |
Output is correct |
12 |
Correct |
36 ms |
7248 KB |
Output is correct |
13 |
Correct |
44 ms |
7248 KB |
Output is correct |
14 |
Correct |
40 ms |
7248 KB |
Output is correct |
15 |
Correct |
46 ms |
7248 KB |
Output is correct |
16 |
Correct |
39 ms |
7168 KB |
Output is correct |
17 |
Correct |
45 ms |
8484 KB |
Output is correct |
18 |
Correct |
47 ms |
7248 KB |
Output is correct |
19 |
Correct |
47 ms |
7248 KB |
Output is correct |
20 |
Correct |
40 ms |
7248 KB |
Output is correct |
21 |
Correct |
65 ms |
7240 KB |
Output is correct |
22 |
Correct |
62 ms |
7248 KB |
Output is correct |
23 |
Correct |
63 ms |
7316 KB |
Output is correct |
24 |
Correct |
64 ms |
7332 KB |
Output is correct |
25 |
Correct |
64 ms |
7248 KB |
Output is correct |
26 |
Correct |
65 ms |
7284 KB |
Output is correct |
27 |
Correct |
65 ms |
7240 KB |
Output is correct |
28 |
Correct |
64 ms |
7240 KB |
Output is correct |
29 |
Correct |
69 ms |
7244 KB |
Output is correct |
30 |
Correct |
73 ms |
7332 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
336 KB |
Output is correct |
44 |
Correct |
1 ms |
336 KB |
Output is correct |
45 |
Correct |
1 ms |
336 KB |
Output is correct |
46 |
Correct |
1 ms |
336 KB |
Output is correct |
47 |
Correct |
1 ms |
336 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
1 ms |
336 KB |
Output is correct |
50 |
Correct |
1 ms |
336 KB |
Output is correct |
51 |
Correct |
1 ms |
508 KB |
Output is correct |
52 |
Correct |
1 ms |
336 KB |
Output is correct |
53 |
Correct |
2 ms |
508 KB |
Output is correct |
54 |
Correct |
1 ms |
336 KB |
Output is correct |
55 |
Correct |
1 ms |
336 KB |
Output is correct |
56 |
Correct |
2 ms |
336 KB |
Output is correct |
57 |
Correct |
1 ms |
336 KB |
Output is correct |
58 |
Correct |
1 ms |
336 KB |
Output is correct |
59 |
Correct |
1 ms |
336 KB |
Output is correct |
60 |
Correct |
1 ms |
336 KB |
Output is correct |
61 |
Correct |
108 ms |
9032 KB |
Output is correct |
62 |
Correct |
98 ms |
7504 KB |
Output is correct |
63 |
Correct |
77 ms |
7248 KB |
Output is correct |
64 |
Correct |
115 ms |
8676 KB |
Output is correct |
65 |
Correct |
106 ms |
9032 KB |
Output is correct |
66 |
Correct |
106 ms |
10324 KB |
Output is correct |
67 |
Correct |
106 ms |
10192 KB |
Output is correct |
68 |
Correct |
69 ms |
9032 KB |
Output is correct |
69 |
Correct |
63 ms |
9800 KB |
Output is correct |
70 |
Correct |
61 ms |
9800 KB |
Output is correct |