# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>
#include<fstream>
//#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;// 998244353;// ;
constexpr long long INF = numeric_limits<LL>::max() / 2;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourd; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Rrep(i,mf) for(LL i=mf-1;i>=0;i--)
void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}
struct Edge { LL to, cost; };
struct edge { LL from, to, cost; };
vector<vector<LL>>g, rev;
vector<edge>edges;
vector<LL>v;
map<LL, LL>ma;
set<LL>st;
LL h, w, n, m, k, t, s, p, q, last, cnt, sum[310000], ans, a[510000], b[510000], dp[310000];
string str, ss;
bool f;
char c;
template<typename V> struct ConvexHullTrick {
	deque<pair<V, V>> Q;
	int cmptype = 1; // 0-min 1-max
	ConvexHullTrick() {
		Q.push_back({0,0});
	}
	V calc(pair<V, V> p, V x) {
		return p.first*x + p.second;
	}
	int dodo(pair<V, V> A, pair<V, V> B, pair<V, V> C) { // max or min
		return cmptype ^ ((B.second - C.second)*(B.first - A.first) <= (A.second - B.second)*(C.first - B.first));
	}
	void add(V a, V b) { // add ax+b
		Q.push_front({ a,b });
		while (Q.size() >= 3 && !dodo(Q[0], Q[1], Q[2]))
			Q[1] = Q[0], Q.pop_front();
	}
	V get(V x) {
		int L = -1, R = Q.size() - 1;
		while (R - L > 1) {
			int M = (L + R) / 2;
			((cmptype ^ ((calc(Q[M], x) >= calc(Q[M + 1], x)))) ? L : R) = M;
		}
		return calc(Q[R], x);
	}
};
//[L,R)
LL inc[310000], ninc[310000], memo[310000];
LL LS[310000],LD[310000], RS[310000],RD[310000];
void dfs(LL L, LL R) {
	if (R - L <= 2)return;
	LL mid = (L + R) / 2;
	ConvexHullTrick<LL> LC, RC;
	for (LL i = L; i < R; i++) {
		if (i < mid)LC.add(-(i + 1), LS[i + 1] + LD[i + 1] + (i + 1) * i / 2);
		else memo[i] = LC.get(i + 1) - LS[i + 1] + (i + 1) * (i + 2) / 2 + RD[n - 1 - i];
	}
	for (LL j = R - 1;j>=L; j--) {
		LL i = n - j - 1;
		if(j>=mid)RC.add(-(i + 1), RS[i + 1] + RD[i + 1] + i  * (i + 1) / 2);
		else memo[j] = RC.get(i + 1) - RS[i + 1] + (i + 1) * (i + 2) / 2 + LD[j];
	}
	LL ma = -INF;
	for (LL i = L; i < mid; i++) {
		ma = max(memo[i], ma);
		inc[i] = max(inc[i], ma);
	}
	ma = -INF;
	for (LL i = R-1; i >= mid; i--) {
		ma = max(memo[i], ma);
		inc[i] = max(inc[i], ma);
	}
	dfs(L,mid);
	dfs(mid,R);
}

int main() {
	cin >> n;
	rep(i, n) {
		cin >> a[i];
	}
	ConvexHullTrick<LL> RC, LC;
	rep(i, n) {
		LS[i + 1] = LS[i] + a[i];
		LD[i + 1] = max(LC.get(i + 1) - LS[i + 1] + (i + 1) * (i + 2) / 2, LD[i]);
		LC.add(-(i + 1), LS[i + 1] + LD[i + 1] + (i + 1) * i / 2);

		RS[i + 1] = RS[i] + a[n - i - 1];
		RD[i + 1] = max(RC.get(i + 1) - RS[i + 1] + (i + 1) * (i + 2) / 2, RD[i]);
		RC.add(-(i + 1), RS[i + 1] + RD[i + 1] + (i + 1) * i / 2);
	}
	rep(i, n) {
		ninc[i] = LD[i] + RD[n - i - 1];
		inc[i] = 1 - a[i];
	}
	dfs(0, n);
	cin >> m;
	rep(i, m) {
		LL x, y;
		cin >> x >> y;
		x--;
		cout << max(ninc[x], inc[x] + a[x] - y)<<endl;
	}
	return 0;
}

Measured memory_bytes: 25088