#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 51234; struct Info{ int lmax,rmax,sum,value; Info(){ value=lmax=rmax=-INF; sum=0; } void maintain(int x){ lmax=rmax=value=sum=x; } }; Info operator + (const Info & a,const Info & b){ Info ret; ret.sum=a.sum+b.sum; ret.lmax=max(a.lmax,b.lmax+a.sum); ret.rmax=max(b.rmax,a.rmax+b.sum); ret.value=max(max(a.value,b.value),a.rmax+b.lmax); return ret; } #define lson o<<1,l,m #define rson o<<1|1,m+1,r #define root 1,1,n #define Now int o,int l,int r #define Mid int m=l + (r-l)/2 Info val[maxn*4]; void update(Now,int pos,int v){ if(l==r){ val[o].maintain(v); return; } Mid; if(pos<=m) update(lson,pos,v); else update(rson,pos,v); val[o] = val[o<<1]+val[o<<1|1]; } Info query(Now,int ql,int qr){ if(ql<=l && r<=qr){ return val[o]; } Mid; Info ret; if(ql<=m) ret = query(lson,ql,qr) + ret; if(m+1<=qr) ret = ret+query(rson,ql,qr); return ret; } int main(){ int n; while(scanf("%d",&n)==1){ int x; for(int i=1;i<=n;i++){ scanf("%d",&x); update(root,i,x); } int m; scanf("%d",&m); int l,r; while(m--){ scanf("%d %d",&l,&r); printf("%d\n",query(root,l,r).value); } } return 0; }