#include <iostream>
using ll = int64_t;
const int NMAX = 3e5;
int n, k;
int a[NMAX + 1];
ll sp[NMAX + 1];
ll ans;
void read() {
std::cin >> n >> k;
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
sp[i] = sp[i - 1] + a[i];
}
}
std::pair<ll, int> dp[NMAX + 1];
bool operator < (const std::pair<ll, int> & a, const std::pair<ll, int> & b) {
if (a.first != b.first)
return a.first < b.first;
return a.second > b.second;
}
bool operator > (const std::pair<ll, int> & a, const std::pair<ll, int> & b) {
if (a.first != b.first)
return a.first > b.first;
return a.second < b.second;
}
std::pair<ll, int> operator + (const std::pair<ll, int> & a, const std::pair<ll, int> & b) {
return std::make_pair(a.first + b.first, a.second + b.second);
}
std::pair<ll, int> ok(ll pen) {
int idx = 0;
for (int i = 1; i <= n; i++) {
//dp[i] = dp[j] + sp[i] - sp[j]
dp[i] = std::max(dp[i - 1], dp[idx] + std::make_pair(sp[i] - sp[idx] - pen, 1));
if (dp[i] + std::make_pair(-sp[i], 0) > dp[idx])
idx = i;
}
return dp[n];
}
void solve() {
ll st = 1;
ll dr = 3e14;
while (st <= dr) {
ll mij = (st + dr) / 2;
auto v = ok(mij);
if (v.second <= k) {
ans = v.first + mij * v.second;
dr = mij - 1;
} else {
st = mij + 1;
}
}
}
void write_ans() {
std::cout << ans << '\n';
}
int main() {
read();
solve();
write_ans();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
183 ms |
8160 KB |
Output is correct |
2 |
Correct |
177 ms |
8520 KB |
Output is correct |
3 |
Correct |
148 ms |
8640 KB |
Output is correct |
4 |
Correct |
148 ms |
8476 KB |
Output is correct |
5 |
Correct |
157 ms |
8520 KB |
Output is correct |
6 |
Correct |
144 ms |
8264 KB |
Output is correct |
7 |
Correct |
160 ms |
8324 KB |
Output is correct |
8 |
Correct |
147 ms |
8520 KB |
Output is correct |
9 |
Correct |
150 ms |
8264 KB |
Output is correct |
10 |
Correct |
151 ms |
8444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
8240 KB |
Output is correct |
2 |
Correct |
109 ms |
8408 KB |
Output is correct |
3 |
Correct |
111 ms |
8432 KB |
Output is correct |
4 |
Correct |
109 ms |
8268 KB |
Output is correct |
5 |
Correct |
142 ms |
8268 KB |
Output is correct |
6 |
Correct |
97 ms |
8264 KB |
Output is correct |
7 |
Correct |
105 ms |
8576 KB |
Output is correct |
8 |
Correct |
140 ms |
8448 KB |
Output is correct |
9 |
Correct |
130 ms |
8148 KB |
Output is correct |
10 |
Correct |
93 ms |
8520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
132 ms |
8380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
183 ms |
8160 KB |
Output is correct |
2 |
Correct |
177 ms |
8520 KB |
Output is correct |
3 |
Correct |
148 ms |
8640 KB |
Output is correct |
4 |
Correct |
148 ms |
8476 KB |
Output is correct |
5 |
Correct |
157 ms |
8520 KB |
Output is correct |
6 |
Correct |
144 ms |
8264 KB |
Output is correct |
7 |
Correct |
160 ms |
8324 KB |
Output is correct |
8 |
Correct |
147 ms |
8520 KB |
Output is correct |
9 |
Correct |
150 ms |
8264 KB |
Output is correct |
10 |
Correct |
151 ms |
8444 KB |
Output is correct |
11 |
Correct |
99 ms |
8240 KB |
Output is correct |
12 |
Correct |
109 ms |
8408 KB |
Output is correct |
13 |
Correct |
111 ms |
8432 KB |
Output is correct |
14 |
Correct |
109 ms |
8268 KB |
Output is correct |
15 |
Correct |
142 ms |
8268 KB |
Output is correct |
16 |
Correct |
97 ms |
8264 KB |
Output is correct |
17 |
Correct |
105 ms |
8576 KB |
Output is correct |
18 |
Correct |
140 ms |
8448 KB |
Output is correct |
19 |
Correct |
130 ms |
8148 KB |
Output is correct |
20 |
Correct |
93 ms |
8520 KB |
Output is correct |
21 |
Incorrect |
132 ms |
8380 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |