#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long double db;
typedef pair<LL, int> PLI;
const int INF = 1e9 + 47;
PLI f(VI& a, db lambda)
{
int n = SZ(a);
PLI dp0 = {0, 0};
PLI dp1 = {-47ll * INF, 0};
FOR (i, 0, n)
{
PLI ndp0 = max(dp0, dp1);
PLI ndp1 = max(PLI(dp0.F + a[i] - lambda, dp0.S + 1), PLI(dp1.F + a[i], dp1.S));
dp0 = ndp0;
dp1 = ndp1;
//cerr << i << ' ' << dp0.F << ' ' << dp0.S << ' ' << dp1.F << ' ' << dp1.S << '\n';
}
return max(dp0, dp1);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
VI a(n);
FOR (i, 0, n)
cin >> a[i];
LL l = -1, r = INF;
while (l + 1 < r)
{
LL m = (l + r) / 2;
auto [val, cnt] = f(a, m);
if (cnt > k)
l = m;
else
r = m;
}
//cerr << r << '\n';
auto [val, cnt] = f(a, r);
cout << LL(val + cnt * r) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
1372 KB |
Output is correct |
2 |
Correct |
93 ms |
1628 KB |
Output is correct |
3 |
Correct |
94 ms |
1628 KB |
Output is correct |
4 |
Correct |
94 ms |
1628 KB |
Output is correct |
5 |
Correct |
87 ms |
1372 KB |
Output is correct |
6 |
Correct |
94 ms |
1372 KB |
Output is correct |
7 |
Correct |
91 ms |
1372 KB |
Output is correct |
8 |
Correct |
90 ms |
1628 KB |
Output is correct |
9 |
Correct |
92 ms |
1372 KB |
Output is correct |
10 |
Correct |
96 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
1372 KB |
Output is correct |
2 |
Correct |
98 ms |
1628 KB |
Output is correct |
3 |
Correct |
76 ms |
1372 KB |
Output is correct |
4 |
Correct |
83 ms |
1372 KB |
Output is correct |
5 |
Correct |
102 ms |
1372 KB |
Output is correct |
6 |
Correct |
84 ms |
1372 KB |
Output is correct |
7 |
Correct |
83 ms |
1628 KB |
Output is correct |
8 |
Correct |
93 ms |
1384 KB |
Output is correct |
9 |
Correct |
88 ms |
1368 KB |
Output is correct |
10 |
Correct |
88 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
129 ms |
1624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
1372 KB |
Output is correct |
2 |
Correct |
93 ms |
1628 KB |
Output is correct |
3 |
Correct |
94 ms |
1628 KB |
Output is correct |
4 |
Correct |
94 ms |
1628 KB |
Output is correct |
5 |
Correct |
87 ms |
1372 KB |
Output is correct |
6 |
Correct |
94 ms |
1372 KB |
Output is correct |
7 |
Correct |
91 ms |
1372 KB |
Output is correct |
8 |
Correct |
90 ms |
1628 KB |
Output is correct |
9 |
Correct |
92 ms |
1372 KB |
Output is correct |
10 |
Correct |
96 ms |
1372 KB |
Output is correct |
11 |
Correct |
84 ms |
1372 KB |
Output is correct |
12 |
Correct |
98 ms |
1628 KB |
Output is correct |
13 |
Correct |
76 ms |
1372 KB |
Output is correct |
14 |
Correct |
83 ms |
1372 KB |
Output is correct |
15 |
Correct |
102 ms |
1372 KB |
Output is correct |
16 |
Correct |
84 ms |
1372 KB |
Output is correct |
17 |
Correct |
83 ms |
1628 KB |
Output is correct |
18 |
Correct |
93 ms |
1384 KB |
Output is correct |
19 |
Correct |
88 ms |
1368 KB |
Output is correct |
20 |
Correct |
88 ms |
1628 KB |
Output is correct |
21 |
Incorrect |
129 ms |
1624 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |