#include<cstdio>
#include<algorithm>
using namespace std;
struct Node
{
int sum_left,sum_right,sum_max,sum_total;
bool indicator;
};
const int MAX = 50010;
const int INF = -9999999;
Node tree[MAX<<2];
int arr[MAX],n;
void input()
{
scanf("%d", &n);
int i;
for(i=1;i<=n;i++)
scanf("%d", &arr[i]);
}
void Build_tree(int a, int b, int node)
{
if(a>b)
return;
if(a==b)
{
tree[node].sum_left=tree[node].sum_right=tree[node].sum_max=tree[node].sum_total=arr[a];
tree[node].indicator=1;
return;
}
Build_tree(a,(a+b)>>1,node<<1);
Build_tree(((a+b)>>1)+1,b,(node<<1)+1);
tree[node].sum_total=tree[node<<1].sum_total+tree[(node<<1)+1].sum_total;
tree[node].sum_left=max(tree[node<<1].sum_left,tree[node<<1].sum_total+tree[(node<<1)+1].sum_left);
tree[node].sum_right=max(tree[(node<<1)+1].sum_right,tree[(node<<1)+1].sum_total+tree[node<<1].sum_right);
tree[node].sum_max=max(tree[node<<1].sum_max,tree[(node<<1)+1].sum_max);
tree[node].sum_max=max(tree[node].sum_max,tree[node].sum_left);
tree[node].sum_max=max(tree[node].sum_max,tree[node].sum_right);
tree[node].sum_max=max(tree[node].sum_max,tree[node].sum_total);
tree[node].sum_max=max(tree[node].sum_max,tree[node<<1].sum_right+tree[(node<<1)+1].sum_left);
if(tree[node].sum_max==tree[node].sum_total)
tree[node].indicator=1;
else
tree[node].indicator=0;
}
Node Query_tree(int a, int b, int i, int j, int node)
{
if(a>b || a>j || b<i)
{
Node p;
p.sum_left=p.sum_right=p.sum_max=p.sum_total=INF;
p.indicator=1;
return p;
}
if(a>=i && b<=j)
return tree[node];
Node q1,q2;
q1=Query_tree(a,(a+b)>>1,i,j,node<<1);
q2=Query_tree(((a+b)>>1)+1,b,i,j,(node<<1)+1);
Node p;
p.sum_total=q1.sum_total+q2.sum_total;
p.sum_left=max(q1.sum_left,q1.sum_total+q2.sum_left);
p.sum_right=max(q2.sum_right,q2.sum_total+q1.sum_right);
p.sum_max=max(q1.sum_max,q2.sum_max);
p.sum_max=max(p.sum_max,p.sum_left);
p.sum_max=max(p.sum_max,p.sum_right);
p.sum_max=max(p.sum_max,p.sum_total);
p.sum_max=max(p.sum_max,q1.sum_right+q2.sum_left);
if(p.sum_max==p.sum_total)
p.indicator=1;
else
p.indicator=0;
return p;
}
void solve()
{
Build_tree(1,n,1);
int i,m,x,y;
scanf("%d", &m);
Node p;
for(i=1;i<=m;i++)
{
scanf("%d %d", &x, &y);
p=Query_tree(1,n,x,y,1);
printf("%d\n", p.sum_max);
}
}
int main()
{
input();
solve();
return 0;
}
I2luY2x1ZGU8Y3N0ZGlvPgojaW5jbHVkZTxhbGdvcml0aG0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnN0cnVjdCBOb2RlCnsKICAgIGludCBzdW1fbGVmdCxzdW1fcmlnaHQsc3VtX21heCxzdW1fdG90YWw7CiAgICBib29sIGluZGljYXRvcjsKfTsKY29uc3QgaW50IE1BWCA9IDUwMDEwOwpjb25zdCBpbnQgSU5GID0gLTk5OTk5OTk7Ck5vZGUgdHJlZVtNQVg8PDJdOwppbnQgYXJyW01BWF0sbjsKdm9pZCBpbnB1dCgpCnsKICAgIHNjYW5mKCIlZCIsICZuKTsKICAgIGludCBpOwogICAgZm9yKGk9MTtpPD1uO2krKykKICAgICAgICBzY2FuZigiJWQiLCAmYXJyW2ldKTsKfQp2b2lkIEJ1aWxkX3RyZWUoaW50IGEsIGludCBiLCBpbnQgbm9kZSkKewogICAgaWYoYT5iKQogICAgICAgIHJldHVybjsKICAgIGlmKGE9PWIpCiAgICB7CiAgICAgICAgdHJlZVtub2RlXS5zdW1fbGVmdD10cmVlW25vZGVdLnN1bV9yaWdodD10cmVlW25vZGVdLnN1bV9tYXg9dHJlZVtub2RlXS5zdW1fdG90YWw9YXJyW2FdOwogICAgICAgIHRyZWVbbm9kZV0uaW5kaWNhdG9yPTE7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgQnVpbGRfdHJlZShhLChhK2IpPj4xLG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