#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 50050;
template<class T> inline void sf(T& x) {
char c; int mul = 1;
while ((c = getchar()) != EOF) {
if (c == '-') mul = -1;
if(c >= '0' && c <= '9') {
x = c-'0'; break;
}
}
if (c == EOF) {
x = EOF; return;
}
while ((c = getchar())) {
if(c >= '0' && c <= '9') {
x = (x<<1)+(x<<3)+(c-'0');
}
else break;
}
x *= mul;
}
int val[maxn];
struct SegNode {
int left, right, sum, maxLeft, maxRight, maxAll;
int mid() {
return (left + right) >> 1;
}
};
struct SegmentTree {
SegNode tree[maxn*5];
void pushUp(int idx) {
tree[idx].sum = tree[idx<<1].sum + tree[idx<<1|1].sum;
tree[idx].maxLeft = max(tree[idx<<1].maxLeft, tree[idx<<1].sum + tree[idx<<1|1].maxLeft);
tree[idx].maxRight = max(tree[idx<<1|1].maxRight, tree[idx<<1|1].sum + tree[idx<<1].maxRight);
tree[idx].maxAll = max(max(tree[idx<<1].maxAll, tree[idx<<1|1].maxAll), tree[idx<<1].maxRight + tree[idx<<1|1].maxLeft);
}
void build(int left, int right, int idx) {
tree[idx].left = left; tree[idx].right = right;
if (left == right) {
tree[idx].maxAll = tree[idx].maxRight = tree[idx].maxLeft = tree[idx].sum = val[left];
return ;
}
int mid = tree[idx].mid();
build(left, mid, idx<<1);
build(mid+1, right, idx<<1|1);
pushUp(idx);
}
SegNode query(int left, int right, int idx) {
if (tree[idx].left >= left && tree[idx].right <= right)
return tree[idx];
int mid = tree[idx].mid();
if (right <= mid) return query(left, right, idx<<1);
else if (left > mid) return query(left, right, idx<<1|1);
else {
SegNode Left = query(left, mid, idx<<1);
SegNode Right = query(mid+1, right, idx<<1|1);
SegNode result;
result.sum = Left.sum + Right.sum;
result.maxLeft = max(Left.maxLeft, Left.sum + Right.maxLeft);
result.maxRight = max(Right.maxRight, Right.sum + Left.maxRight);
result.maxAll = max(max(Left.maxAll, Right.maxAll), Left.maxRight + Right.maxLeft);
return result;
}
}
};
SegmentTree g;
int n, q;
int main() {
sf(n);
for (int i = 1; i <= n; i++)
sf(val[i]);
scanf("%d", &q); g.build(1, n, 1);
for (int i = 0; i < q; i++) {
int left, right;
sf(left); sf(right);
printf("%d\n", g.query(left, right, 1).maxAll);
}
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxxdWV1ZT4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHZlY3Rvcj4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGludCBtYXhuID0gNTAwNTA7CnRlbXBsYXRlPGNsYXNzIFQ+IGlubGluZSB2b2lkIHNmKFQmIHgpIHsKCWNoYXIgYzsgaW50IG11bCA9IDE7Cgl3aGlsZSAoKGMgPSBnZXRjaGFyKCkpICE9IEVPRikgewoJCWlmIChjID09ICctJykgbXVsID0gLTE7CgkJaWYoYyA+PSAnMCcgJiYgYyA8PSAnOScpIHsKCQkJeCA9IGMtJzAnOyBicmVhazsKCQl9Cgl9CglpZiAoYyA9PSBFT0YpIHsKICAgIAl4ID0gRU9GOyByZXR1cm47CiAgICB9CiAgICB3aGlsZSAoKGMgPSBnZXRjaGFyKCkpKSB7CiAgICAJaWYoYyA+PSAnMCcgJiYgYyA8PSAnOScpIHsKICAgICAgICAgICAgeCA9ICh4PDwxKSsoeDw8MykrKGMtJzAnKTsKICAgICAgICB9CiAgICAgICAgZWxzZSBicmVhazsKICAgIH0KICAgIHggKj0gbXVsOwp9CgppbnQgdmFsW21heG5dOwoKc3RydWN0IFNlZ05vZGUgewoJaW50IGxlZnQsIHJpZ2h0LCBzdW0sIG1heExlZnQsIG1heFJpZ2h0LCBtYXhBbGw7CgoJaW50IG1pZCgpIHsKCQlyZXR1cm4gKGxlZnQgKyByaWdodCkgPj4gMTsKCX0KfTsKCnN0cnVjdCBTZWdtZW50VHJlZSB7CgoJU2VnTm9kZSB0cmVlW21heG4qNV07Cg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