#include<bits/stdc++.h>
using namespace std;
#define maxx 1 << 16
struct node
{
long pre;
long suff;
long sum;
long maxsum;
};
struct node tree[maxx<<1];
node merge(node &a,node &b)
{
node temp;
temp.sum=a.sum+b.sum;
temp.pre=max(a.pre,a.sum+b.pre);
temp.suff=max(b.suff,b.sum+a.suff);
temp.maxsum=max(max(temp.sum,temp.pre),temp.suff);
return temp;
}
void build(int a[],int pos,int low,int high)
{
if(low==high)
{
tree[pos].maxsum=a[low];
tree[pos].sum=a[low];
tree[pos].pre=a[low];
tree[pos].suff=a[low];
return;
}
else
{
int mid=(low+high)>>1;
build(a,2*pos,low,mid);
build(a,2*pos+1,mid+1,high);
tree[pos]=merge(tree[2*pos],tree[2*pos+1]);
}
}
node query(int pos,int low,int high,int qlow,int qhigh)
{
if(qlow>=low && qhigh<=high)tree[pos];
int mid=(low+high)>>1;
node l=query(2*pos,low,mid,qlow,qhigh);
node r=query(2*pos+1,mid+1,high,qlow,qhigh);
tree[pos]=merge(l,r);
return tree[pos];
}
int main()
{
int n;
cin>>n;
int a[maxx];
for(int i=0;i<n;i++)cin>>a[i];
build(a,0,0,n-1);
int Q;
cin>>Q;
while(Q--)
{
int x,y;
cin>>x>>y;
cout<<query(0,0,n-1,--x,--y).maxsum<<endl;
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBtYXh4IDEgPDwgMTYKc3RydWN0IG5vZGUKewogICAgbG9uZyBwcmU7CiAgICBsb25nIHN1ZmY7CiAgICBsb25nIHN1bTsKICAgIGxvbmcgbWF4c3VtOwp9OwpzdHJ1Y3Qgbm9kZSB0cmVlW21heHg8PDFdOwpub2RlIG1lcmdlKG5vZGUgJmEsbm9kZSAmYikKewogICAgbm9kZSB0ZW1wOwogICAgdGVtcC5zdW09YS5zdW0rYi5zdW07CiAgICB0ZW1wLnByZT1tYXgoYS5wcmUsYS5zdW0rYi5wcmUpOwogICAgdGVtcC5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