#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 << 41;
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 |
54 ms |
2580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
2588 KB |
Output is correct |
2 |
Correct |
40 ms |
2644 KB |
Output is correct |
3 |
Correct |
48 ms |
2516 KB |
Output is correct |
4 |
Incorrect |
39 ms |
2516 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
2636 KB |
Output is correct |
2 |
Correct |
48 ms |
2488 KB |
Output is correct |
3 |
Correct |
53 ms |
2620 KB |
Output is correct |
4 |
Correct |
52 ms |
2528 KB |
Output is correct |
5 |
Correct |
47 ms |
2504 KB |
Output is correct |
6 |
Correct |
50 ms |
2508 KB |
Output is correct |
7 |
Correct |
49 ms |
2572 KB |
Output is correct |
8 |
Correct |
52 ms |
2524 KB |
Output is correct |
9 |
Correct |
52 ms |
2636 KB |
Output is correct |
10 |
Correct |
49 ms |
2568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 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 |
0 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 |
54 ms |
2580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |