//{{{
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VLL = vector<LL>;
using vi = vector<int>;
using pii = pair<int, int>;
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
#ifdef LOCAL
#include "prettyprint.hpp"
// clang-format off
void _print() { cerr << "]\033[0m\n"; }
template <typename T> void __print(T x) { cerr << x; }
template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); }
#define debug(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m["; _print(x)
#define debug_arr(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m" << (x) << "\033[0m\n"
#else
#define debug(x...)
#define debug_arr(x...)
#endif
// clang-format on
//}}}
const int N = 3e5 + 10;
// const int N = 10;
int n, k;
int a[N];
LL s[N];
LL dp[N], cnt[N];
LL minf[N], minf_p[N];
int check(LL c)
{
dp[0] = 0, cnt[0] = 0;
minf[0] = dp[0] - s[0];
minf_p[0] = 0;
for (int i = 1; i <= n; i++)
{
dp[i] = dp[i - 1];
cnt[i] = cnt[i - 1];
if (minf[i - 1] + s[i] - c > dp[i])
{
dp[i] = minf[i - 1] + s[i] - c;
cnt[i] = cnt[minf_p[i - 1]] + 1;
}
minf[i] = minf[i - 1];
minf_p[i] = minf_p[i - 1];
if (dp[i] - s[i] > minf[i])
{
minf[i] = dp[i] - s[i];
minf_p[i] = i;
}
}
// if (cnt[n] == 0) return k + 1;
return cnt[n];
}
int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
// freopen("out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> n >> k)
{
for (int i = 1; i <= n; ++i) cin >> a[i];
s[0] = 0;
for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + a[i];
LL lo = -1e18, hi = 1e18;
while (lo + 1 < hi)
{
LL mid = lo + (hi - lo) / 2;
// debug(lo, hi, mid);
// cout << lo << " " << hi << " " << mid << " ";
if (check(mid) <= k) hi = mid;
else
lo = mid;
// cout << cnt[n] << endl;
// if (hi - lo < 1e-6) break;
// debug(lo, hi, cnt[n]);
}
// debug(hi);
// for (int p = -100; p <= 100; p++)
//{
// check(p);
// debug(p, cnt[n]);
//}
// debug("======");
// double lo = -2;
// check(lo);
// debug_arr(dp);
// debug_arr(cnt);
check(hi);
// debug_arr(dp);
// debug_arr(cnt);
LL ans = dp[n] + hi * cnt[n];
ans = max(ans, 0ll);
cout << ans << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
102 ms |
15852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
14228 KB |
Output is correct |
2 |
Correct |
96 ms |
14444 KB |
Output is correct |
3 |
Correct |
90 ms |
14132 KB |
Output is correct |
4 |
Incorrect |
85 ms |
14248 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
16320 KB |
Output is correct |
2 |
Correct |
131 ms |
15980 KB |
Output is correct |
3 |
Correct |
124 ms |
16236 KB |
Output is correct |
4 |
Correct |
132 ms |
16108 KB |
Output is correct |
5 |
Correct |
151 ms |
16108 KB |
Output is correct |
6 |
Correct |
126 ms |
16272 KB |
Output is correct |
7 |
Correct |
126 ms |
16236 KB |
Output is correct |
8 |
Correct |
136 ms |
16364 KB |
Output is correct |
9 |
Correct |
125 ms |
16364 KB |
Output is correct |
10 |
Correct |
125 ms |
16236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
102 ms |
15852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |