//{{{
#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;
LL a[N];
LL s[N];
LL dp[N];
LL cnt[N];
LL minf[N];
LL 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;
// debug(i, dp[i]);
cnt[i] = cnt[minf_p[i - 1]] + 1;
}
else if (minf[i - 1] + s[i] - c == dp[i])
{
if (cnt[i] < cnt[minf_p[i - 1]] + 1)
{
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];
}
// const int NN = 1e3 + 10;
// LL dp2[NN][NN];
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];
// clr(dp2, 0x80);
// dp2[0][0] = 0;
// for (int i = 1; i <= n; i++)
//{
// dp2[i][0] = 0;
// for (int j = 1; j <= i; j++)
//{
// dp2[i][j] = dp2[i - 1][j];
// for (int p = 0; p < i; p++)
//{
// dp2[i][j] = max(dp2[i][j], dp2[p][j - 1] + s[i] - s[p]);
//}
//}
//}
// for (int p = 1; p <= n; ++p)
//{
// debug(p, dp2[n][p]);
//}
// for (int kk = max(1, k - 15); kk <= min(1000, k + 15); kk++)
//{
// debug(kk, dp2[n][kk], dp2[n][kk] - dp2[n][kk - 1]);
//// cout << kk << "," << dp2[n][kk] << endl;
//}
// for (int i = -10; i <= 1; i++)
//{
// int v = check(i);
// debug(i, v, cnt[n], dp[n]);
//}
LL ans = LLONG_MIN;
LL lo = 0, hi = 1e15;
while (lo <= hi)
{
// debug(lo, hi);
LL mid = lo + (hi - lo) / 2;
if (check(mid) >= k)
{
debug(mid, dp[n] + mid * k);
ans = max(ans, dp[n] + mid * k);
lo = mid + 1;
}
else
{
hi = mid - 1;
}
}
if (ans == LLONG_MIN)
{
check(0);
ans = dp[n];
}
// LL c = lo;
// if (check(hi) >= k) c = hi;
// debug(lo, hi, c);
// int v = check(c);
// debug(c, v);
// debug(cnt[n], k, dp[n]);
// LL ans2 = ans;
// v = check(0);
// if (cnt[n] <= k && dp[n] > ans) ans2 = dp[n];
// debug(cnt[n], dp[n], ans);
// ans = max(ans, ans2);
cout << ans << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
14188 KB |
Output is correct |
2 |
Correct |
107 ms |
14572 KB |
Output is correct |
3 |
Correct |
101 ms |
14828 KB |
Output is correct |
4 |
Correct |
100 ms |
14700 KB |
Output is correct |
5 |
Correct |
100 ms |
14572 KB |
Output is correct |
6 |
Correct |
100 ms |
14316 KB |
Output is correct |
7 |
Correct |
97 ms |
14316 KB |
Output is correct |
8 |
Correct |
101 ms |
14572 KB |
Output is correct |
9 |
Correct |
99 ms |
14444 KB |
Output is correct |
10 |
Correct |
100 ms |
14572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
77 ms |
14188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
109 ms |
14444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
14188 KB |
Output is correct |
2 |
Correct |
107 ms |
14572 KB |
Output is correct |
3 |
Correct |
101 ms |
14828 KB |
Output is correct |
4 |
Correct |
100 ms |
14700 KB |
Output is correct |
5 |
Correct |
100 ms |
14572 KB |
Output is correct |
6 |
Correct |
100 ms |
14316 KB |
Output is correct |
7 |
Correct |
97 ms |
14316 KB |
Output is correct |
8 |
Correct |
101 ms |
14572 KB |
Output is correct |
9 |
Correct |
99 ms |
14444 KB |
Output is correct |
10 |
Correct |
100 ms |
14572 KB |
Output is correct |
11 |
Incorrect |
77 ms |
14188 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |