#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include "bits/stdc++.h"
using namespace std;
template<typename T>
using V = vector<T>;
using ll = long long;
using pi = pair<ll, int>;
#define ckmax(a,b) a = max(a,b)
inline std::pair<long long, int> operator-(const std::pair<long long, int>& p1, const std::pair<long long, int>& p2) {
return {p1.first - p2.first, p1.second - p2.second};
}
pi solve(V<ll>&arr, ll k, ll p) {
V<pi> dp(2,{-1e18,0});
dp[0] = {0,0};
pi pa = make_pair(p,-1);
for (ll i = 1; i <= arr.size(); ++i) {
auto newDp = dp;
dp[1] = {-1e18,0};
dp[0] = {0,0};
for (ll l = 0; l < 2; ++l) {
ckmax(newDp[l], max(max(dp[1], dp[0]) - pa, dp[1]));
}
ckmax(newDp[0], dp[0]);
newDp[1].first += arr[i - 1];
dp = newDp;
}
return max(dp[0], dp[1]);
}
int main() {
ll n, k; cin >> n >> k;
V<ll> arr(n);
for (ll i = 0; i < n; ++i) {
cin >> arr[i];
}
ll l = 0, r = 1e18;
int iter = 0;
while (r > l) {
ll mid = l + (r - l + 1) / 2;
if (solve(arr, k, mid).second >= k)
l = mid;
else
r = mid - 1;
}
cout << solve(arr,k,l).first + l * k << "\n";
}
Compilation message
feast.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
3 | #pragma GCC optimization ("O3")
|
feast.cpp:4: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
4 | #pragma GCC optimization ("unroll-loops")
|
feast.cpp: In function 'pi solve(V<long long int>&, ll, ll)':
feast.cpp:23:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (ll i = 1; i <= arr.size(); ++i) {
| ~~^~~~~~~~~~~~~
feast.cpp: In function 'int main()':
feast.cpp:50:9: warning: unused variable 'iter' [-Wunused-variable]
50 | int iter = 0;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
428 ms |
5364 KB |
Output is correct |
2 |
Correct |
429 ms |
5712 KB |
Output is correct |
3 |
Correct |
437 ms |
5456 KB |
Output is correct |
4 |
Correct |
427 ms |
5456 KB |
Output is correct |
5 |
Correct |
436 ms |
5556 KB |
Output is correct |
6 |
Correct |
413 ms |
5496 KB |
Output is correct |
7 |
Correct |
427 ms |
5456 KB |
Output is correct |
8 |
Correct |
447 ms |
5456 KB |
Output is correct |
9 |
Correct |
436 ms |
5512 KB |
Output is correct |
10 |
Correct |
428 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
387 ms |
3676 KB |
Output is correct |
2 |
Correct |
414 ms |
3900 KB |
Output is correct |
3 |
Incorrect |
391 ms |
3816 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
462 ms |
5716 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 |
428 ms |
5364 KB |
Output is correct |
2 |
Correct |
429 ms |
5712 KB |
Output is correct |
3 |
Correct |
437 ms |
5456 KB |
Output is correct |
4 |
Correct |
427 ms |
5456 KB |
Output is correct |
5 |
Correct |
436 ms |
5556 KB |
Output is correct |
6 |
Correct |
413 ms |
5496 KB |
Output is correct |
7 |
Correct |
427 ms |
5456 KB |
Output is correct |
8 |
Correct |
447 ms |
5456 KB |
Output is correct |
9 |
Correct |
436 ms |
5512 KB |
Output is correct |
10 |
Correct |
428 ms |
5456 KB |
Output is correct |
11 |
Correct |
387 ms |
3676 KB |
Output is correct |
12 |
Correct |
414 ms |
3900 KB |
Output is correct |
13 |
Incorrect |
391 ms |
3816 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |