#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];
ll 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 {
bool cmp(int x, int y) {
return a[x] > a[y];
}
void solve() {
vector <int> order(n); iota(all(order), 1);
sort (all(order), cmp);
priority_queue <tuple <int, int, int, int>> pq;
pq.emplace(a[order[0]] + a[order[1]] + a[order[2]], 0, 1, 2);
while (!pq.empty()) {
auto [sum, x, y, z] = pq.top(); pq.pop();
int A = order[x], B = order[y], C = order[z];
if (A > B) swap(A, B);
if (A > C) swap(A, C);
if (B > C) swap(B, C);
if (B - A <= C - B) {
cout << sum << '\n';
return;
}
if (x + 1 < y) pq.emplace(sum - a[order[x]] + a[order[x + 1]], x + 1, y, z);
if (y + 1 < z) pq.emplace(sum - a[order[y]] + a[order[y + 1]], x, y + 1, z);
if (z + 1 < n) pq.emplace(sum - a[order[z]] + a[order[z + 1]], x, y, z + 1);
}
}
}
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:91:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen(Task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:92:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | 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... |