#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#define fi first
#define se second
#define pii pair<int,int>
//#define ivorysi
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define MAXN 300005
using namespace std;
typedef long long int64;
typedef double db;
typedef unsigned int u32;
template<class T>
void read(T &res) {
	res = 0;T f = 1;char c = getchar();
	while(c < '0' || c > '9') {
		if(c == '-') f = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9' ) {
		res = res * 10 + c - '0';
		c = getchar();
	}
	res *= f;
}
template<class T>
void out(T x) {
	if(x < 0) {x = -x;putchar('-');}
	if(x >= 10) {
		out(x / 10);
	}
	putchar('0' + x % 10);
}
int N,M;
int64 T[MAXN],pre[MAXN],suf[MAXN],sum[MAXN],ans[MAXN],cal[MAXN];
struct Point {
	int64 x,y;
	Point(){}
	Point(int64 _x,int64 _y) {
		x = _x;y = _y;
	}
}que[MAXN],pre_pos[MAXN],suf_pos[MAXN];
struct qry_node {
	int id,pos;
	int64 v;
}qry[MAXN],tmp1[MAXN],tmp2[MAXN];
bool slope(Point a,Point b,Point c) {
	return (c.y - b.y) * (b.x - a.x) > (b.y - a.y) *  (c.x - b.x);
}
void DC(int l,int r,int ql,int qr) {
	if(qr < ql) return;
	if(l == r) return;
	int mid = (l + r) >> 1;
	int tot = 0;
	que[++tot] = pre_pos[l - 1];
	for(int i = l ; i <= mid ; ++i) {
		while(tot > 1) {
			if(slope(que[tot - 1],que[tot],pre_pos[i])) --tot;
			else break;
		}
		que[++tot] = pre_pos[i];
	}
	for(int i = r + 1 ; i >= mid + 2; --i) {
		int L = 1,R = tot;
		while(L < R) {
			int mid = (L + R) >> 1;
			if((que[mid + 1].y - que[mid].y) >= 1LL * i * (que[mid + 1].x - que[mid].x)) 
				L = mid + 1;
			else R = mid;
		}
		L = que[L].x;
		cal[i] = suf[i] + pre[L] - sum[i - 1] + sum[L] + 1LL * (i - L - 1) * (i - L) / 2;
	}
	tot = 0;
	que[++tot] = suf_pos[r + 1];
	for(int i = r ; i > mid ; --i) {
		while(tot > 1) {
			if(slope(suf_pos[i],que[tot],que[tot - 1])) --tot;
			else break;
		}
		que[++tot] = suf_pos[i];
	}
	for(int i = l - 1 ; i < mid ; ++i) {
		int L = 1,R = tot;
		while(L < R) {
			int mid = (L + R) >> 1;
			if((que[mid].y - que[mid + 1].y) >= 1LL * i * (que[mid].x - que[mid + 1].x))
				R = mid;
			else L = mid + 1;
		}
		L = que[L].x;
		cal[i] = pre[i] + suf[L] - sum[L - 1] + sum[i] + 1LL * (L - i - 1) * (L - i) / 2;
	}
	for(int i = l ; i < mid ; ++i) cal[i] = max(cal[i - 1],cal[i]);
	for(int i = r ; i > mid + 1 ; --i) cal[i] = max(cal[i + 1],cal[i]);
	int t1 = 0,t2 = 0;
	for(int i = ql ; i <= qr ; ++i) {
		if(qry[i].pos <= mid) {
			ans[qry[i].id] = max(ans[qry[i].id],cal[qry[i].pos - 1] + T[qry[i].pos] - qry[i].v);
			tmp1[++t1] = qry[i];
		}
		else {
			ans[qry[i].id] = max(ans[qry[i].id],cal[qry[i].pos + 1] + T[qry[i].pos] - qry[i].v);
			tmp2[++t2] = qry[i];
		}
	}
	int p = ql - 1;
	for(int i = 1 ; i <= t1 ; ++i) qry[++p] = tmp1[i];
	for(int i = 1 ; i <= t2 ; ++i) qry[++p] = tmp2[i];
	DC(l,mid,ql,ql + t1 - 1);
	DC(mid + 1,r,ql + t1,qr);
}	

void Solve() {
	read(N);
	for(int i = 1 ; i <= N ; ++i) read(T[i]);
	for(int i = 1 ; i <= N ; ++i) sum[i] = sum[i - 1] + T[i];
	int tot = 0;
	que[++tot] = Point(0,0);
	for(int i = 1 ; i <= N ; ++i) {
		int l = 1,r = tot;
		while(l < r) {
			int mid = (l + r) >> 1;
			if((que[mid + 1].y - que[mid].y) >= 1LL * i * (que[mid + 1].x - que[mid].x)) 
				l = mid + 1;
			else r = mid;
		}
		l = que[l].x;
		pre[i] = pre[l] + 1LL * (i - l) * (i - l + 1) / 2 - sum[i] + sum[l];
		pre[i] = max(pre[i],pre[i - 1]);
		Point p = Point(i,pre[i] + sum[i] + (1LL * i * (i - 1)) / 2);
		while(tot > 1) {
			if(slope(que[tot - 1],que[tot],p)) --tot;
			else break;
		}
		que[++tot] = p;
	}
	tot = 0;
	que[++tot] = Point(N + 1,(1LL * (N + 1) * (N + 2) / 2) - sum[N]);
	for(int i = N ; i >= 1 ; --i) {
		int l = 1,r = tot;
		while(l < r) {
			int mid = (l + r) >> 1;
			if((que[mid].y - que[mid + 1].y) >= 1LL * i * (que[mid].x - que[mid + 1].x))
				r = mid;
			else l = mid + 1;
		}
		l = que[l].x;
		suf[i] = suf[l] + (1LL * (l - i + 1) * (l - i) / 2) - sum[l - 1] + sum[i - 1];
		suf[i] = max(suf[i],suf[i + 1]);
		Point p = Point(i,suf[i] - sum[i - 1] + (1LL * i * (i + 1) / 2));
		while(tot > 1) {
			if(slope(p,que[tot],que[tot - 1])) --tot;
			else break;
		}
		que[++tot] = p;
	}
	for(int i = 0 ; i <= N ; ++i) {
		pre_pos[i] = Point(i,pre[i] + sum[i] + (1LL * i * (i + 1) / 2));
	}
	for(int i = 1 ; i <= N + 1 ; ++i) {
		suf_pos[i] = Point(i,suf[i] - sum[i - 1] + (1LL * i * (i - 1) / 2));
	}
	read(M);
	int p;int64 v;
	for(int i = 1 ; i <= M ; ++i) {
		read(p);read(v);
		qry[i] = (qry_node){i,p,v};
		ans[i] = pre[p - 1] + suf[p + 1];
	}
	DC(1,N,1,M);
	for(int i = 1 ; i <= M ; ++i) {
		out(ans[i]);enter;
	}
}
int main() {
#ifdef ivorysi
	freopen("f1.in","r",stdin);
#endif
	Solve();
	return 0;
}

Measured memory_bytes: 42752