#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define bigint __int128
#define emb emplace_back
#define pb push_back
#define pii pair <int, int>
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define Task ""
#define MASK(k) (1ull << k)
#define bitcnt(k) __builtin_popcount(k)
#define testBit(n, k) ((n >> k) & 1)
#define flipBit(n, k) (n ^ (1ll << k))
#define offBit(n, k) (n & ~MASK(k))
#define onBit(n, k) (n | (1ll << k))
template <class T> bool minimize(T &a, T b) {if (a > b) {a = b; return true;} return false;}
template <class T> bool maximize(T &a, T b) {if (a < b) {a = b; return true;} return false;}
const int N = 5e5 + 5, lim = 60, mod = 1e9 + 7;
const ll INF = 1e18;
int n, q, a[N];
int l[N], r[N];
namespace sub2 {
int f[5005][5005];
void solve() {
for (int x = 1; x <= n - 2; x++) {
for (int z = x + 2, y = x + 1, ma = a[y]; z <= n; z++) {
while ((y + 1) * 2 <= x + z) maximize(ma, a[++y]);
f[x][z] = a[x] + ma + a[z];
}
}
for (int len = 4; len <= n; len++)
for (int x = 1, z = len; z <= n; x++, z++)
maximize(f[x][z], max(f[x][z - 1], f[x + 1][z]));
for (int i = 1; i <= q; i++)
cout << f[l[i]][r[i]] << '\n';
}
}
namespace sub3 {
int ri[N], sufMa[N];
vector <pii> candidate;
void solve() {
for (int i = n; i > 0; i--)
sufMa[i] = max(sufMa[i + 1], a[i]);
// nhung cap (a, b) : max(A[a+1, b-1]) < min(A[a], A[b])
stack <int> st;
for (int i = 1; i <= n; i++) {
while (!st.empty() && a[st.top()] <= a[i]) {
// neu truoc i ton tai k ma A[x] <= A[k] -> x da bi bo / neu ko thi A[x] lon nhat
// den khi A[x] <= A[i] -> x va i la 2 dau mut lon nhat
candidate.emplace_back(st.top(), i);
st.pop();
}
if (!st.empty()) {
// A[st.top()] > A[st.top() + 1 ... i]
// cap nay co the thoa/ko thoa khi A[i] <= max o giua
// nhung vi tri trong st con lai ko thoa vi co max_mid >= A[st.top()] > A[i]
candidate.emplace_back(st.top(), i);
}
st.emplace(i);
}
int ans = 0;
for (auto [x, y]: candidate) {
int miZ = 2 * y - x;
if (miZ <= n) maximize(ans, a[x] + a[y] + sufMa[miZ]);
}
cout << ans;
}
}
namespace sub4 {
void solve() {
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
if (fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
cin >> q;
for (int i = 1; i <= q; i++) cin >> l[i] >> r[i];
if (n <= 5e3) sub2::solve();
else if (q == 1 && l[1] == 1 && r[1] == n) sub3::solve();
else sub4::solve();
}
Compilation message (stderr)
jumps.cpp: In function 'int main()':
jumps.cpp:98:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen(Task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:99:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen(Task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |