#include <bits/stdc++.h>
using namespace std;
constexpr size_t N = 300000;
int64_t a[N];
pair<int64_t, int64_t> dp(size_t n, int64_t lambda)
{
int64_t mu = 0, nu = 0, p = 0, cnt = 0;
for (size_t i = 0; i < n; ++i)
{
p += a[i];
if (mu + p - lambda > nu)
nu = mu + p - lambda, ++cnt;
mu = max(mu, nu - p);
}
return {nu, cnt};
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, k;
cin >> n >> k;
for (size_t i = 0; i < n; ++i)
cin >> a[i];
int64_t u = 0, v = 1LL << 55;
while (u < v)
{
if (dp(n, (u + v) / 2).second <= k)
v = (u + v) / 2;
else
u = (u + v) / 2 + 1;
}
auto const [objective_val, cnt] = dp(n, u);
cout << objective_val + k * u << '\n';
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:34:39: warning: comparison of integer expressions of different signedness: 'long int' and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
34 | if (dp(n, (u + v) / 2).second <= k)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
53 ms |
2508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
2548 KB |
Output is correct |
2 |
Correct |
46 ms |
2652 KB |
Output is correct |
3 |
Correct |
45 ms |
2572 KB |
Output is correct |
4 |
Incorrect |
47 ms |
2748 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
2628 KB |
Output is correct |
2 |
Correct |
58 ms |
2576 KB |
Output is correct |
3 |
Correct |
55 ms |
2492 KB |
Output is correct |
4 |
Correct |
55 ms |
2596 KB |
Output is correct |
5 |
Correct |
54 ms |
2528 KB |
Output is correct |
6 |
Correct |
56 ms |
2700 KB |
Output is correct |
7 |
Correct |
56 ms |
2612 KB |
Output is correct |
8 |
Correct |
59 ms |
2600 KB |
Output is correct |
9 |
Correct |
65 ms |
2648 KB |
Output is correct |
10 |
Correct |
57 ms |
2640 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 |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
53 ms |
2508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |