// Copyright (C) 2017 __debug.

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; version 3

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; If not, see <http://www.gnu.org/licenses/>.


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>

#define x first
#define y second
#define MP std::make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

using std::pair;
using std::vector;
using std::string;

typedef long long LL;
typedef pair<int, int> Pii;

const int oo = 0x3f3f3f3f;

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, true : false; }
string procStatus()
{
    std::ifstream t("/proc/self/status");
    return string(std::istreambuf_iterator<char>(t), std::istreambuf_iterator<char>());
}
template<typename T> T read(T &x)
{
    int f = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar())
        f = (ch == '-' ? -1 : 1);
    for (x = 0; isdigit(ch); ch = getchar())
        x = 10 * x + ch - '0';
    return x *= f;
}
template<typename T> void write(T x)
{
    if (x == 0) {
        putchar('0');
        return;
    }
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    static char s[20];
    int top = 0;
    for (; x; x /= 10)
        s[++top] = x % 10 + '0';
    while (top)
        putchar(s[top--]);
}
// EOT

const int MAXN = 3e5 + 5;

int N, M;
LL T[MAXN];

void input()
{
    read(N);
    for (int i = 1; i <= N; ++i) {
        read(T[i]);
        T[i] *= 2;
    }
    read(M);
}

typedef pair<LL, LL> Atom;

LL S[MAXN], rS[MAXN];
LL dpL[MAXN], dpR[MAXN], dpM[MAXN], dpN[MAXN];
Atom st[MAXN];
int top;

#define SLOPE(a, b) (double)(st[a].y - st[b].y) / (st[a].x - st[b].x)

inline void add(Atom x)
{
    st[0] = x;
    while (top > 1 && SLOPE(top - 1, top) <= SLOPE(top - 1, 0)) --top;
    st[++top] = x;
}

inline LL query(LL i)
{
    while (top > 1 && SLOPE(top - 1, top) < 2 * i) --top;
    return st[top].y + i * i - 2 * i * st[top].x + i - S[i];
}

void getDP(LL dp[])
{
    // printf("getDP()\n");
    top = 0;
    add(Atom(0, 0));
    for (int i = 1; i <= N; ++i) {
        dp[i] = std::max(dp[i - 1], query(i));
        add(MP(i, dp[i] + (LL)i * i - i + S[i]));
        // dp[i] = dp[i - 1];
        // for (int j = 0; j < i; ++j) {
        //     chkmax(dp[i], dp[j] + (LL)(i - j) * (i - j) + (i - j) - (S[i] - S[j]));
        // }
    }
}

void calc(int l, int r, LL dp[], LL dpl[], LL dpr[])
{
    if (l > r)
        return;
    if (l == r) {
        chkmax(dp[l], dpl[l - 1] + dpr[l + 1] + 1 - T[l]);
        return;
    }

    static LL val[MAXN];
    int mid = (l + r) >> 1;

    top = 0;
    for (int i = l - 1; i < mid; ++i) add(Atom(i, dpl[i] + (LL)i * i - i + S[i]));
    for (int i = mid; i <= r; ++i) {
        val[i] = query(i) + dpr[i + 1];
        // printf("%lld ", query(i));
    }
    // puts("");
    for (int i = r - 1; i >= mid; --i) chkmax(val[i], val[i + 1]);
    for (int i = mid; i <= r; ++i) chkmax(dp[i], val[i]);

    calc(l, mid - 1, dp, dpl, dpr);
    calc(mid + 1, r, dp, dpl, dpr);
}

void solve()
{
    for (int i = 1; i <= N; ++i) S[i] = S[i - 1] + T[i];
    getDP(dpL);
    std::reverse(T + 1, T + N + 1);
    for (int i = 1; i <= N; ++i) S[i] = S[i - 1] + T[i];
    getDP(dpR);
    std::reverse(T + 1, T + N + 1);
    std::reverse(dpR + 1, dpR + N + 1);
    for (int i = 1; i <= N; ++i) S[i] = S[i - 1] + T[i];

    // puts("dpL:");
    // for (int i = 1; i <= N; ++i) {
    //     printf("%lld ", dpL[i]);
    // }
    // puts("");
    // puts("dpR:");
    // for (int i = 1; i <= N; ++i) {
    //     printf("%lld ", dpR[i]);
    // }
    // puts("");

    memset(dpM, -oo, sizeof(dpM));
    calc(1, N, dpM, dpL, dpR);
    std::reverse(T + 1, T + N + 1);
    std::reverse(dpL + 1, dpL + N + 1);
    std::reverse(dpR + 1, dpR + N + 1);
    for (int i = 1; i <= N; ++i) S[i] = S[i - 1] + T[i];
    memset(dpN, -oo, sizeof(dpN));
    calc(1, N, dpN, dpR, dpL);
    std::reverse(T + 1, T + N + 1);
    std::reverse(dpL + 1, dpL + N + 1);
    std::reverse(dpR + 1, dpR + N + 1);
    std::reverse(dpN + 1, dpN + N + 1);
    for (int i = 1; i <= N; ++i) chkmax(dpM[i], dpN[i]);

    while (M--) {
        int p, x;
        read(p); read(x);
        x *= 2;
        LL ans = std::max(dpL[p - 1] + dpR[p + 1], dpM[p] + T[p] - x);
        assert(ans % 2 == 0);
        ans /= 2;
        write(ans); putchar('\n');
    }
}

int main()
{
#ifdef __DEBUG
    freopen("F.in", "r", stdin);
    freopen("F.out", "w", stdout);
#endif

    input();
    solve();

    return 0;
}

// 盛年不重来，一日难再晨。
//     -- 陶渊明《杂诗·人生无根蒂》


Measured memory_bytes: 19840