#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = INT_MAX / 2;
const int MAXN = 3e5 + 5;
int n, k;
ll arr[MAXN], dp[MAXN][2];
int cnt[MAXN][2];
ll answer;
bool eval(ll c)
{
dp[0][1] = -INF;
for (int i = 1; i <= n; i++)
{
if (dp[i - 1][0] >= dp[i - 1][1])
{
dp[i][0] = dp[i - 1][0];
cnt[i][0] = cnt[i - 1][0];
}
else
{
dp[i][0] = dp[i - 1][1];
cnt[i][0] = cnt[i - 1][1];
}
if (dp[i - 1][0] - c >= dp[i - 1][1])
{
dp[i][1] = arr[i] + dp[i - 1][0] - c;
cnt[i][1] = cnt[i - 1][0] + 1;
}
else
{
dp[i][1] = arr[i] + dp[i - 1][1];
cnt[i][1] = cnt[i - 1][1];
}
}
ll optv = LLONG_MIN;
int optx = INT_MIN;
for (int i = 0; i <= n; i++)
for (int ii = 0; ii < 2; ii++)
{
ll nv = dp[i][ii] + c * cnt[i][ii];
int nx = cnt[i][ii];
if (nv > optv)
{
optv = nv;
optx = nx;
}
if (nv == optv)
optx = max(optx, nx);
}
if (optx <= k)
answer = max(answer, optv);
return optx <= k;
}
void solve()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> arr[i];
ll lo = 0, hi = INF;
while (lo <= hi)
{
ll mi = lo + (hi - lo) / 2;
if (eval(mi))
hi = mi - 1;
else
lo = mi + 1;
}
cout << answer << '\n';
}
int main()
{
// ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
11344 KB |
Output is correct |
2 |
Correct |
107 ms |
11604 KB |
Output is correct |
3 |
Correct |
101 ms |
11408 KB |
Output is correct |
4 |
Correct |
130 ms |
11344 KB |
Output is correct |
5 |
Correct |
103 ms |
11480 KB |
Output is correct |
6 |
Correct |
101 ms |
11088 KB |
Output is correct |
7 |
Correct |
103 ms |
11284 KB |
Output is correct |
8 |
Correct |
106 ms |
11384 KB |
Output is correct |
9 |
Correct |
103 ms |
11344 KB |
Output is correct |
10 |
Correct |
111 ms |
11316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
10528 KB |
Output is correct |
2 |
Correct |
70 ms |
10472 KB |
Output is correct |
3 |
Correct |
65 ms |
10324 KB |
Output is correct |
4 |
Correct |
71 ms |
10428 KB |
Output is correct |
5 |
Correct |
101 ms |
11072 KB |
Output is correct |
6 |
Correct |
62 ms |
10320 KB |
Output is correct |
7 |
Correct |
67 ms |
10580 KB |
Output is correct |
8 |
Correct |
104 ms |
11600 KB |
Output is correct |
9 |
Correct |
106 ms |
11268 KB |
Output is correct |
10 |
Correct |
68 ms |
10268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
176 ms |
11604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
11344 KB |
Output is correct |
2 |
Correct |
107 ms |
11604 KB |
Output is correct |
3 |
Correct |
101 ms |
11408 KB |
Output is correct |
4 |
Correct |
130 ms |
11344 KB |
Output is correct |
5 |
Correct |
103 ms |
11480 KB |
Output is correct |
6 |
Correct |
101 ms |
11088 KB |
Output is correct |
7 |
Correct |
103 ms |
11284 KB |
Output is correct |
8 |
Correct |
106 ms |
11384 KB |
Output is correct |
9 |
Correct |
103 ms |
11344 KB |
Output is correct |
10 |
Correct |
111 ms |
11316 KB |
Output is correct |
11 |
Correct |
65 ms |
10528 KB |
Output is correct |
12 |
Correct |
70 ms |
10472 KB |
Output is correct |
13 |
Correct |
65 ms |
10324 KB |
Output is correct |
14 |
Correct |
71 ms |
10428 KB |
Output is correct |
15 |
Correct |
101 ms |
11072 KB |
Output is correct |
16 |
Correct |
62 ms |
10320 KB |
Output is correct |
17 |
Correct |
67 ms |
10580 KB |
Output is correct |
18 |
Correct |
104 ms |
11600 KB |
Output is correct |
19 |
Correct |
106 ms |
11268 KB |
Output is correct |
20 |
Correct |
68 ms |
10268 KB |
Output is correct |
21 |
Incorrect |
176 ms |
11604 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |