#include <bits/stdc++.h>
using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
template <class T>
using Vec = std::vector<T>;
constexpr i32 inf32 = (u32) ~0 >> 2;
constexpr i64 inf64 = (u64) ~0 >> 2;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
usize N;
std::cin >> N;
Vec<u32> A(N);
for (auto &x: A) {
std::cin >> x;
}
usize Q;
std::cin >> Q;
if (Q <= 20) {
while (Q--) {
usize L, R;
std::cin >> L >> R;
L -= 1;
R -= 1;
std::stack<std::pair<u32, usize>> stack;
Vec<u32> accum(N);
accum[R] = A[R];
for (usize i = R; i --> L;) {
accum[i] = std::max(A[i], accum[i + 1]);
}
u32 ans = 0;
for (usize m = L; m < R; ++m) {
while (!stack.empty()) {
const auto [v, i] = stack.top();
if (m + m - i <= R) {
ans = std::max(ans, v + A[m] + accum[m + m - i]);
}
if (v > A[m]) {
break;
}
stack.pop();
}
stack.emplace(A[m], m);
}
std::cout << ans << '\n';
}
}
else if (N <= 5000) {
Vec<Vec<u32>> dp(N, Vec<u32>(N, 0));
for (usize l = 0; l < N; ++l) {
u32 m = 0;
for (usize r = l + 2; r < N; ++r) {
m = std::max(m, A[(l + r) / 2]);
dp[l][r] = A[l] + m + A[r];
}
}
for (usize len = 2; len < N; ++len) {
for (usize l = 0; l + len < N; ++l) {
dp[l][l + len] = std::max({ dp[l][l + len], dp[l + 1][l + len], dp[l][l + len - 1] });
}
}
while (Q--) {
usize l, r;
std::cin >> l >> r;
l -= 1;
r -= 1;
std::cout << dp[l][r] << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
492 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 |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
492 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 |
402 ms |
104684 KB |
Output is correct |
12 |
Correct |
409 ms |
104428 KB |
Output is correct |
13 |
Correct |
395 ms |
104512 KB |
Output is correct |
14 |
Correct |
400 ms |
104604 KB |
Output is correct |
15 |
Correct |
401 ms |
104684 KB |
Output is correct |
16 |
Correct |
396 ms |
104088 KB |
Output is correct |
17 |
Correct |
393 ms |
103916 KB |
Output is correct |
18 |
Correct |
416 ms |
103964 KB |
Output is correct |
19 |
Correct |
402 ms |
104556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
3096 KB |
Output is correct |
2 |
Correct |
21 ms |
3692 KB |
Output is correct |
3 |
Correct |
25 ms |
6892 KB |
Output is correct |
4 |
Correct |
24 ms |
3692 KB |
Output is correct |
5 |
Correct |
24 ms |
3692 KB |
Output is correct |
6 |
Correct |
20 ms |
3052 KB |
Output is correct |
7 |
Correct |
19 ms |
2924 KB |
Output is correct |
8 |
Correct |
19 ms |
2924 KB |
Output is correct |
9 |
Correct |
21 ms |
3180 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 |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
492 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 |
402 ms |
104684 KB |
Output is correct |
12 |
Correct |
409 ms |
104428 KB |
Output is correct |
13 |
Correct |
395 ms |
104512 KB |
Output is correct |
14 |
Correct |
400 ms |
104604 KB |
Output is correct |
15 |
Correct |
401 ms |
104684 KB |
Output is correct |
16 |
Correct |
396 ms |
104088 KB |
Output is correct |
17 |
Correct |
393 ms |
103916 KB |
Output is correct |
18 |
Correct |
416 ms |
103964 KB |
Output is correct |
19 |
Correct |
402 ms |
104556 KB |
Output is correct |
20 |
Correct |
24 ms |
3096 KB |
Output is correct |
21 |
Correct |
21 ms |
3692 KB |
Output is correct |
22 |
Correct |
25 ms |
6892 KB |
Output is correct |
23 |
Correct |
24 ms |
3692 KB |
Output is correct |
24 |
Correct |
24 ms |
3692 KB |
Output is correct |
25 |
Correct |
20 ms |
3052 KB |
Output is correct |
26 |
Correct |
19 ms |
2924 KB |
Output is correct |
27 |
Correct |
19 ms |
2924 KB |
Output is correct |
28 |
Correct |
21 ms |
3180 KB |
Output is correct |
29 |
Incorrect |
50 ms |
7020 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |