/*
        _.-- ,.--.
      .'   .'     /
       @       |'..--------._
     /      \._/              '.
    /  .-.-                     \
   (  /    \                     \
   \\      '.                  | #
    \\       \   -.           /
     :\       |    )._____.'   \
      "       |   /  \  |  \    )
              |   |./'  :__ \.-'
              '--'
*/
#include<bits/stdc++.h>
#define ll long long
#define llll pair<ll,ll>
#define ii pair<int,int>
#define fi first
#define se second
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define FOD(i,r,l) for(int i=r;i>=l;i--)
#define ull unsigned long long
#define iii pair<int,ii>
#define iv pair<pii,ii>
#define db double
#define ld long double
#define pb push_back
#define tdk "kfph"
using namespace std;
const int dx[]  = {1, -1, 0, 0};
const int dy[]  = {0, 0, -1, 1};
const int dxx[] = {1, 1, -1, -1, 2, 2, -2, -2};
const int dyy[] = {2, -2, 2, -2, 1, -1, 1, -1};
const ll INF=1e18;
const int N=5e5+5;
int a[N],n,q,st2[N*4],st1[N*4],lazy[4*N],ans[N];
vector<ii> query[N];
stack<int> st;
void build(int id,int l,int r)
{
    if(l==r)
    {
        st1[id]=a[l];
    }
    else
    {
        int mid=l+r>>1;
        build(id*2,l,mid);
        build(id*2+1,mid+1,r);
        st1[id]=max(st1[id*2],st1[id*2+1]);
    }
}
void push(int id)
{
    int k=lazy[id];
    if(k)
    {
        st2[id*2]=max(st2[id*2],k+st1[id]);
        st2[id*2+1]=max(st2[id*2+1],k+st1[id]);
        lazy[id*2]=max(lazy[id*2],k);
        lazy[id*2+1]=max(lazy[id*2+1],k);
        lazy[id]=0;
    }
}
void update(int id,int l,int r,int u,int v,int val)
{
    if(l>v or r<u) return;
    if(l>=u and r<=v)
    {
        st2[id]=max(st2[id],val+st1[id]);
        lazy[id]=max(lazy[id],val);
        return;
    }
    if(l!=r)push(id);
    int mid=l+r>>1;
    update(id*2,l,mid,u,v,val);
    update(id*2+1,mid+1,r,u,v,val);
    st2[id]=max(st2[id*2],st2[id*2+1]);
}
int get(int id,int l,int r,int u,int v)
{
    if(l>v or r<u) return 0;
    if(l>=u and r<=v) return st2[id];
    int mid=l+r>>1;
    return max(get(id*2,l,mid,u,v),get(id*2+1,mid+1,r,u,v));
}
void kfph()
{
    cin >> n;
    for(int i=1;i<=n;i++) cin >> a[i];
    build(1,1,n);
    cin >> q;
    for(int i=1;i<=q;i++)
    {
        int l,r;
        cin >> l >> r;
        query[l].pb({r,i});
    }
    for(int i=n;i>=1;i--)
    {
        while(st.size() and a[i]>a[st.top()])
        {
            int k=2*st.top()-i;
            update(1,1,n,k,n,a[i]+a[st.top()]);
            st.pop();
        }
        if(st.size())
        {
            int k=2*st.top()-i;
            update(1,1,n,k,n,a[i]+a[st.top()]);
        }
        st.push(i);
        for(int j=0;j<query[i].size();j++)
        {
            int r=query[i][j].fi,id=query[i][j].se;
            ans[id]=get(1,1,n,i,r);
        }
    }
    for(int i=1;i<=q;i++) cout << ans[i] << '\n';
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    if(fopen(tdk".inp","r"))
    {
        freopen(tdk".inp","r",stdin);
        freopen(tdk".out","w",stdout);
    }
    int t=1;
    //cin >> t;
    while(t--)
    {
        kfph();
    }
    return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
jumps.cpp: In function 'int main()':
jumps.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |         freopen(tdk".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
jumps.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |         freopen(tdk".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... |