#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Line {
    mutable ll k, m, p;
    bool operator<(const Line& o) const { return k < o.k; }
    bool operator<(ll x) const { return p < x; }
};
struct cht : multiset<Line, less<>> {
    // (for doubles, use inf = 1/.0, div(a,b) = a/b)
    const ll inf = 1ll<<62;
    ll div(ll a, ll b) { // floored division
        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) {
        if(empty()) return -inf;
        auto l = *lower_bound(x);
        return l.k * x + l.m;
    }
};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n; cin >> n;
    vector<ll> t(n + 2), dp_l(n + 2), dp_r(n + 2), use(n + 2, -(1ll<<60));
    for(int i = 1; i <= n; i++) cin >> t[i];
    for(int i = 1; i <= n; i++) t[i] <<= 1;
    {
        ll sum = 0;
        // (i - j) * (i - j + 1) - sum
        // i ^ 2 - i * j + i - i * j + j ^ 2 - j
        // i ^ 2 - 2 * i * j + j ^ 2 - sum
        cht cl;
        cl.add(-2*0, 0);
        for(int i = 1; i <= n; i++){
            dp_l[i] = dp_l[i - 1];
            sum += t[i];
            dp_l[i] = max(dp_l[i], cl.query(i) - sum + 1ll*i*i + i);
            cl.add(-2*i, sum + 1ll*i*i - i + dp_l[i]);
        }
    }
    {
        ll sum = 0;
        // (j - i) * (j - i + 1) - sum
        cht cr;
        cr.add(-2*(n + 1), 1ll*(n+1)*(n+1) + (n + 1));
        for(int i = n; i >= 1; i--){
            dp_r[i] = dp_r[i + 1];
            sum += t[i];
            dp_r[i] = max(dp_r[i], cr.query(i) - sum + 1ll*i*i - i);
            cr.add(-2*i, sum + 1ll*i*i + i + dp_r[i]);
        }
    }
    function<void(int,int)> solve = [&](int l, int r){
        if(l > r) return;
        if(l == r){
            use[l] = max(use[l], dp_l[l - 1] + dp_r[l + 1] - t[l] + 2);
            return;
        }
        int m = (l + r)>>1;
        {
            cht cr;
            ll sum = 0;
            cr.add(0, dp_r[m + 1]);
            for(int i = m + 1; i <= r; i++){
                ll l = i - m;
                sum += t[i];
                cr.add(2 * l, -sum + l*l + l + dp_r[i + 1]);
            }
            sum = 0;
            for(int i = l; i <= m; i++) sum += t[i];
            ll best = -(1ll<<60);
            for(int i = l; i <= m; i++){
                ll l = m - i + 1, vh = cr.query(l) + l*l - sum + l + dp_l[i - 1];
                best = max(best, vh);
                use[i] = max(use[i], best);
                sum -= t[i];
            }
        }
        {
            cht cl;
            ll sum = 0;
            cl.add(0, dp_l[m]);
            for(int i = m; i >= l; i--){
                ll l = m - i + 1;
                sum += t[i];
                cl.add(2 * l, -sum + l*l + l + dp_l[i - 1]);
            }
            sum = 0;
            for(int i = m + 1; i <= r; i++) sum += t[i];
            ll best = -(1ll<<60);
            for(int i = r; i >= m + 1; i--){
                ll l = i - m, vh = cl.query(l) + l*l - sum + l + dp_r[i + 1];
                best = max(best, vh);
                use[i] = max(use[i], best);
                sum -= t[i];
            }
        }
        solve(l, m);
        solve(m + 1, r);
    };
    solve(1, n);
    int m; cin >> m;
    for(int i = 0; i < m; i++){
        int p, x; cin >> p >> x; x <<= 1;
        cout << max(dp_l[p - 1] + dp_r[p + 1], use[p] - x + t[p]) / 2 << '\n';
    }
    return 0;
}

Measured memory_bytes: 12800