#include <bits/stdc++.h>

#define For(i, j, k) for(int i = j; i <= k; i++)
#define Forr(i, j, k) for(int i = j; i >= k; i--)

using namespace std;

const int N = 3e5 + 10;

typedef long long LL;

int n, m;
int A[N];
LL S[N], F[N];
int stk[N], top;

double sect(int i, int j){
	return 1.0 * (F[i] - F[j]) / (i - j);
}

void pop(int i){
	while(top > 1 && sect(stk[top], stk[top - 1]) < i) --top;
}

void push(int i){
	while(top > 1 && sect(i, stk[top]) > sect(stk[top], stk[top - 1])) --top;
	stk[++top] = i;
}

void work(LL *dp){
	For(i, 1, n) S[i] = S[i - 1] + A[i];
	dp[0] = 0;
	stk[top = 1] = 0;

	For(i, 1, n){
		dp[i] = dp[i - 1];
		LL C = 1ll * i * (i + 1) / 2 - S[i];
		pop(i);
		
		int j = stk[top];
		dp[i] = max(dp[i], F[j] + C - 1ll * i * j);

		F[i] = 1ll * i * (i - 1) / 2 + dp[i] + S[i];
		push(i);
	}
}

LL val[N], tmp[N];

void solve(LL *dp, LL *rdp, int l, int r){
	if(l == 0 && r == n) For(i, 1, n) S[i] = S[i - 1] + A[i];
	if(l == r) return;
	
	int mid = (l + r) >> 1;
	top = 0;
	For(i, l, mid){
		F[i] = 1ll * i * (i - 1) / 2 + dp[i] + S[i];
		push(i);
	}
	For(i, mid + 1, r){
		pop(i);
		int j = stk[top];
		LL C = 1ll * i * (i + 1) / 2 - S[i];
		tmp[i] = F[j] + C - 1ll * i * j + rdp[i + 1];
	}
	Forr(i, r - 1, mid + 1) tmp[i] = max(tmp[i], tmp[i + 1]);
	For(i, mid + 1, r) val[i] = max(val[i], tmp[i]);
	solve(dp, rdp, l, mid), solve(dp, rdp, mid + 1, r);
}

LL fl[N], fr[N];

#define rev(x) reverse(x + 1, x + n + 1)

int main(){
	scanf("%d", &n);
	For(i, 1, n) scanf("%d", &A[i]), val[i] = -1e18;

	work(fl);
	rev(A);
	work(fr);
	rev(A), rev(fr);
	solve(fl, fr, 0, n);
	rev(fl), rev(fr), rev(A), rev(val);
	solve(fr, fl, 0, n);
	rev(fl), rev(fr), rev(A), rev(val);

	scanf("%d", &m);
	while(m--){
		int p, x;
		scanf("%d%d", &p, &x);
		printf("%lld\n", max(fl[p - 1] + fr[p + 1], val[p] + A[p] - x));
	}

	return 0;
}