#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> piipi;
typedef pair<pii, pii> piipii;

#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define eb emplace_back

int t[300005];
int p[300005], x[300005];

bool Q;
struct Line {
    mutable ll k, m, p;
    bool operator<(const Line& o) const {
        return Q ? p < o.p : k < o.k;
    }
};
 
struct LineContainer : multiset<Line> {
    const ll inf = LLONG_MAX;
    void init(){
        clear();
    }
    ll div(ll a, ll b){
        return a / b - ((a ^ b) < 0 && a % b);
    }
    bool isect(iterator x, iterator y) {
        if (y == end()) { x->p = inf; return false; }
        if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
        else x->p = div(y->m - x->m, x->k - y->k);
        return x->p >= y->p;
    }
    void add(ll k, ll m) {
        auto z = insert({k, m, 0}), y = z++, x = y;
        while (isect(y, z)) z = erase(z);
        if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
        while ((y = x) != begin() && (--x)->p >= y->p)
            isect(x, erase(y));
    }
    ll query(ll x) {
        assert(!empty());
        Q = 1; auto l = *lower_bound({0,0,x}); Q = 0;
        return l.k * x + l.m;
    }
}C;
ll pref[300005], suff[300005];
ll sum[300005];
ll dp[300005];

void dfs(int s, int e){
    if(s == e){
        dp[s] = max(dp[s], 1ll-t[s] + pref[s-1] + suff[s+1]);
        return;
    }
    int mid = (s+e)/2;
    dfs(s, mid);
    dfs(mid+1, e);

    ll mx = -1e18;
    C.init();
    for(int i=mid+1;i<=e;i++){
        int r = i;
        ll k = -r;
        ll m = (r*1ll*r + r)/2 + suff[i+1] - sum[i];
        C.add(k, m);
    }
    for(int i=s;i<=mid;i++){
        int l = i-1;
        mx = max(mx, C.query(l) + (l*1ll*l - l)/2 + sum[i-1] + pref[i-1]);
        dp[i] = max(dp[i], mx);
    }

    mx = -1e18;
    C.init();
    for(int i=s;i<=mid;i++){
        int l = i-1;
        ll k = -l;
        ll m = (l*1ll*l - l)/2 + pref[i-1] + sum[i-1];
        C.add(k, m);
    }
    for(int i=e;i>=mid+1;i--){
        int r = i;
        mx = max(mx, C.query(r) + (r*1ll*r + r)/2 - sum[i] + suff[i+1]);
        dp[i] = max(dp[i], mx);
    }
}

int main(){
    int n;
    scanf("%d", &n);
    for(int i=1;i<=n;i++) scanf("%d", &t[i]), sum[i] = sum[i-1] + t[i];
    for(int i=1;i<=n;i++) dp[i] = -1e18;
    int m;
    scanf("%d", &m);
    for(int i=1;i<=m;i++) scanf("%d%d", &p[i], &x[i]);

    C.init();
    for(int i=1;i<=n;i++){
        int l = i-1, r = i;
        ll k = -l;
        ll m = (l*1ll*l - l)/2 + pref[i-1] + sum[i-1];
        C.add(k, m);
        pref[i] = C.query(r) + (r*1ll*r + r)/2 - sum[i];
        pref[i] = max(pref[i], pref[i-1]);
    }

    C.init();
    for(int i=n;i>=1;i--){
        int l = i-1, r = i;
        ll k = -r;
        ll m = (r*1ll*r + r)/2 + suff[i+1] - sum[i];
        C.add(k, m);
        suff[i] = C.query(l) + (l*1ll*l - l)/2 + sum[i-1];
        suff[i] = max(suff[i], suff[i+1]);
    }
    dfs(1, n);
    for(int i=1;i<=m;i++) printf("%lld\n", max(pref[p[i]-1] + suff[p[i]+1], dp[p[i]] + t[p[i]] - x[i]));
}

Measured memory_bytes: 16676