//{{{
#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];
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;
}
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];
// double lo = 0, hi = 1e18;
LL lo = 0, hi = 1e15;
while (lo + 1 < hi)
// for (int o = 1; o <= 100; o++)
{
LL mid = lo + (hi - lo) / 2;
// double mid = (lo + hi) / 2.0;
// 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-2) break;
// debug(lo, hi, cnt[n]);
}
// debug(lo, hi);
// 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);
// double hi = -0.9999;
check(hi);
// debug_arr(dp);
// debug_arr(cnt);
LL ans = dp[n] + hi * cnt[n];
// debug(ans);
// ans = max(ans, 0.0);
// ans = LL(ans);
cout << ans << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
14004 KB |
Output is correct |
2 |
Correct |
104 ms |
16056 KB |
Output is correct |
3 |
Correct |
105 ms |
16108 KB |
Output is correct |
4 |
Correct |
101 ms |
15980 KB |
Output is correct |
5 |
Correct |
100 ms |
15980 KB |
Output is correct |
6 |
Correct |
98 ms |
15636 KB |
Output is correct |
7 |
Correct |
102 ms |
15724 KB |
Output is correct |
8 |
Correct |
103 ms |
15980 KB |
Output is correct |
9 |
Correct |
114 ms |
15724 KB |
Output is correct |
10 |
Correct |
103 ms |
15992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
14060 KB |
Output is correct |
2 |
Correct |
82 ms |
14316 KB |
Output is correct |
3 |
Correct |
80 ms |
14060 KB |
Output is correct |
4 |
Correct |
81 ms |
14188 KB |
Output is correct |
5 |
Correct |
100 ms |
15724 KB |
Output is correct |
6 |
Correct |
99 ms |
14060 KB |
Output is correct |
7 |
Correct |
101 ms |
14548 KB |
Output is correct |
8 |
Correct |
112 ms |
16108 KB |
Output is correct |
9 |
Correct |
101 ms |
15724 KB |
Output is correct |
10 |
Correct |
83 ms |
14316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
14188 KB |
Output is correct |
2 |
Correct |
108 ms |
15980 KB |
Output is correct |
3 |
Correct |
107 ms |
16108 KB |
Output is correct |
4 |
Correct |
107 ms |
15980 KB |
Output is correct |
5 |
Correct |
119 ms |
16236 KB |
Output is correct |
6 |
Correct |
107 ms |
16364 KB |
Output is correct |
7 |
Correct |
108 ms |
16236 KB |
Output is correct |
8 |
Correct |
105 ms |
16108 KB |
Output is correct |
9 |
Correct |
108 ms |
16264 KB |
Output is correct |
10 |
Correct |
113 ms |
16236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
2 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
2 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
14004 KB |
Output is correct |
2 |
Correct |
104 ms |
16056 KB |
Output is correct |
3 |
Correct |
105 ms |
16108 KB |
Output is correct |
4 |
Correct |
101 ms |
15980 KB |
Output is correct |
5 |
Correct |
100 ms |
15980 KB |
Output is correct |
6 |
Correct |
98 ms |
15636 KB |
Output is correct |
7 |
Correct |
102 ms |
15724 KB |
Output is correct |
8 |
Correct |
103 ms |
15980 KB |
Output is correct |
9 |
Correct |
114 ms |
15724 KB |
Output is correct |
10 |
Correct |
103 ms |
15992 KB |
Output is correct |
11 |
Correct |
74 ms |
14060 KB |
Output is correct |
12 |
Correct |
82 ms |
14316 KB |
Output is correct |
13 |
Correct |
80 ms |
14060 KB |
Output is correct |
14 |
Correct |
81 ms |
14188 KB |
Output is correct |
15 |
Correct |
100 ms |
15724 KB |
Output is correct |
16 |
Correct |
99 ms |
14060 KB |
Output is correct |
17 |
Correct |
101 ms |
14548 KB |
Output is correct |
18 |
Correct |
112 ms |
16108 KB |
Output is correct |
19 |
Correct |
101 ms |
15724 KB |
Output is correct |
20 |
Correct |
83 ms |
14316 KB |
Output is correct |
21 |
Correct |
108 ms |
14188 KB |
Output is correct |
22 |
Correct |
108 ms |
15980 KB |
Output is correct |
23 |
Correct |
107 ms |
16108 KB |
Output is correct |
24 |
Correct |
107 ms |
15980 KB |
Output is correct |
25 |
Correct |
119 ms |
16236 KB |
Output is correct |
26 |
Correct |
107 ms |
16364 KB |
Output is correct |
27 |
Correct |
108 ms |
16236 KB |
Output is correct |
28 |
Correct |
105 ms |
16108 KB |
Output is correct |
29 |
Correct |
108 ms |
16264 KB |
Output is correct |
30 |
Correct |
113 ms |
16236 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
1 ms |
492 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
1 ms |
364 KB |
Output is correct |
50 |
Correct |
1 ms |
492 KB |
Output is correct |
51 |
Correct |
1 ms |
492 KB |
Output is correct |
52 |
Correct |
2 ms |
492 KB |
Output is correct |
53 |
Correct |
2 ms |
492 KB |
Output is correct |
54 |
Correct |
1 ms |
492 KB |
Output is correct |
55 |
Correct |
2 ms |
492 KB |
Output is correct |
56 |
Correct |
1 ms |
492 KB |
Output is correct |
57 |
Correct |
1 ms |
492 KB |
Output is correct |
58 |
Correct |
1 ms |
492 KB |
Output is correct |
59 |
Correct |
2 ms |
492 KB |
Output is correct |
60 |
Correct |
1 ms |
492 KB |
Output is correct |
61 |
Correct |
150 ms |
16000 KB |
Output is correct |
62 |
Correct |
147 ms |
16384 KB |
Output is correct |
63 |
Correct |
125 ms |
15852 KB |
Output is correct |
64 |
Correct |
150 ms |
16236 KB |
Output is correct |
65 |
Correct |
170 ms |
16364 KB |
Output is correct |
66 |
Correct |
151 ms |
16108 KB |
Output is correct |
67 |
Correct |
155 ms |
15852 KB |
Output is correct |
68 |
Correct |
118 ms |
16364 KB |
Output is correct |
69 |
Correct |
110 ms |
16112 KB |
Output is correct |
70 |
Correct |
105 ms |
15852 KB |
Output is correct |