import java.io.* ;
import java.util.* ;
class utkarsh {
class node{
int SUM, P, M, S, B;
node( int h) {
SUM = P = M = S = B = h;
}
}
int a[ ] , n;
node tree[ ] ;
int max( int x, int y) {
return x > y ? x : y;
}
void build( int N, int s, int e) {
if ( s == e) {
tree[ N] = new node( a[ s] ) ;
return ;
}
int mid, lc, rc;
mid = s + ( e - s) / 2 ;
lc = 2 * N + 1 ;
rc = 2 * N + 2 ;
build( lc, s, mid) ;
build( rc, mid + 1 , e) ;
tree[ N] = new node( 0 ) ;
tree[ N] .SUM = tree[ lc] .SUM + tree[ rc] .SUM ;
tree[ N] .P = max( tree[ lc] .P , tree[ lc] .SUM + tree[ rc] .P ) ;
tree[ N] .M = tree[ lc] .S + tree[ rc] .P ;
tree[ N] .S = max( tree[ rc] .S , tree[ rc] .SUM + tree[ lc] .S ) ;
tree[ N] .B = max( tree[ N] .SUM , max( tree[ N] .P , max( tree[ N] .M , max( tree[ N] .S , max( tree[ lc] .B , tree[ rc] .B ) ) ) ) ) ;
}
node query( int N, int s, int e, int l, int r) {
if ( s > r || l > e) {
return new node( - 200100 ) ;
}
if ( s >= l && e <= r) {
return tree[ N] ;
}
int mid = s + ( e - s) / 2 ;
if ( r <= mid) {
return query( 2 * N + 1 , s, mid, l, r) ;
} else if ( l > mid) {
return query( 2 * N + 2 , mid + 1 , e, l, r) ;
} else {
node LC, RC, disha;
LC = query( 2 * N + 1 , s, mid, l, r) ;
RC = query( 2 * N + 2 , mid + 1 , e, l, r) ;
disha = new node( 0 ) ;
disha.SUM = LC.SUM + RC.SUM ;
disha.P = max( LC.P , LC.SUM + RC.P ) ;
disha.M = LC.S + RC.P ;
disha.S = max( RC.S , RC.SUM + LC.S ) ;
disha.B = max( disha.SUM , max( disha.P , max( disha.M , max( disha.S , max( LC.B , RC.B ) ) ) ) ) ;
return disha;
}
}
void solve( ) {
//Enter code here utkarsh
int i, q;
n = ni( ) ;
a = new int [ n] ;
tree = new node[ 4 * n] ;
for ( i = 0 ; i < n; i++ ) {
a[ i] = ni( ) ;
}
build( 0 , 0 , n- 1 ) ;
q = ni( ) ;
while ( q-- > 0 ) {
out.println ( query( 0 , 0 , n- 1 , ni( ) - 1 , ni( ) - 1 ) .B ) ;
}
}
public static void main
( String [ ] args
) { new utkarsh
( ) .
run ( ) ; }
}
byte input[ ] = new byte [ 1024 ] ;
int len = 0 , ptr = 0 ;
int readByte
( ) { if ( ptr
>= len
) { ptr
= 0 ; try { len
= is.
read ( input
) ; } catch ( IOException e
) { throw new InputMismatchException
( ) ; } if ( len
<= 0 ) { return - 1 ; } } return input
[ ptr
++ ] ; }
boolean isSpaceChar( int c) { return ! ( c >= 33 && c <= 126 ) ;
}
int skip( ) { int b = readByte( ) ; while ( b != - 1 && isSpaceChar( b) ) { b = readByte( ) ; } return b;
}
int ni( ) { int n = 0 ,b = readByte( ) ; boolean minus = false ; while ( b != - 1 && ! ( ( b >= '0' && b <= '9' ) || b == '-' ) ) { b = readByte( ) ; } if ( b == '-' ) { minus = true ; b = readByte( ) ; } if ( b == - 1 ) { return - 1 ; } while ( b >= '0' && b <= '9' ) { n = n * 10 + ( b - '0' ) ; b = readByte( ) ; } return minus ? - n : n;
}
long nl( ) { long n = 0L; int b = readByte( ) ; boolean minus = false ; while ( b != - 1 && ! ( ( b >= '0' && b <= '9' ) || b == '-' ) ) { b = readByte( ) ; } if ( b == '-' ) { minus = true ; b = readByte( ) ; } while ( b >= '0' && b <= '9' ) { n = n * 10 + ( b - '0' ) ; b = readByte( ) ; } return minus ? - n : n;
}
}
CmltcG9ydCBqYXZhLmlvLio7CmltcG9ydCBqYXZhLnV0aWwuKjsKCmNsYXNzIHV0a2Fyc2ggewoKICAgIElucHV0U3RyZWFtIGlzOwogICAgUHJpbnRXcml0ZXIgb3V0OwogICAgCiAgICBjbGFzcyBub2RlewogICAgICAgIGludCBTVU0sIFAsIE0sIFMsIEI7CiAgICAgICAgbm9kZShpbnQgaCl7CiAgICAgICAgICAgIFNVTSA9IFAgPSBNID0gUyA9IEIgPSBoOwogICAgICAgIH0KICAgIH0KICAgIAogICAgaW50IGFbXSwgbjsKICAgIG5vZGUgdHJlZVtdOwogICAgCiAgICBpbnQgbWF4KGludCB4LCBpbnQgeSl7CiAgICAgICAgcmV0dXJuIHggPiB5ID8geCA6IHk7CiAgICB9CiAgICAKICAgIHZvaWQgYnVpbGQoaW50IE4sIGludCBzLCBpbnQgZSl7CiAgICAgICAgaWYocyA9PSBlKXsKICAgICAgICAgICAgdHJlZVtOXSA9IG5ldyBub2RlKGFbc10pOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGludCBtaWQsIGxjLCByYzsKICAgICAgICBtaWQgPSBzICsgKGUgLSBzKSAvIDI7CiAgICAgICAgbGMgPSAyICogTiArIDE7CiAgICAgICAgcmMgPSAyICogTiArIDI7CiAgICAgICAgYnVpbGQobGMsIHMsIG1pZCk7CiAgICAgICAgYnVpbGQocmMsIG1pZCArIDEsIGUpOwogICAgICAgIHRyZWVbTl0gPSBuZXcgbm9kZSgwKTsKICAgICAgICB0cmVlW05dLlNVTSA9IHRyZWVbbGNdLlNVTSArIHRyZWVbcmNdLlNVTTsKICAgICAgICB0cmVlW05dLlAgPSBtYXgodHJlZVtsY10uUCwgdHJlZVtsY10uU1VNICsgdHJlZVtyY10uUCk7CiAgICAgICAgdHJlZVtOXS5NID0gdHJlZVtsY10uUyArIHRyZWVbcmNdLlA7CiAgICAgICAgdHJlZVtOXS5TID0gbWF4KHRyZWVbcmNdLlMsIHRyZWVbcmNdLlNVTSArIHRyZWVbbGNdLlMpOwogICAgICAgIHRyZWVbTl0uQiA9IG1heCh0cmVlW05dLlNVTSwgbWF4KHRyZWVbTl0uUCwgbWF4KHRyZWVbTl0uTSwgbWF4KHRyZWVbTl0uUywgbWF4KHRyZWVbbGNdLkIsIHRyZWVbcmNdLkIpKSkpKTsKICAgIH0KICAgIAogICAgbm9kZSBxdWVyeShpbnQgTiwgaW50IHMsIGludCBlLCBpbnQgbCwgaW50IHIpewogICAgICAgIGlmKHMgPiByIHx8IGwgPiBlKXsKICAgICAgICAgICAgcmV0dXJuIG5ldyBub2RlKC0yMDAxMDApOwogICAgICAgIH0KICAgICAgICBpZihzID49IGwgJiYgZSA8PSByKXsKICAgICAgICAgICAgcmV0dXJuIHRyZWVbTl07CiAgICAgICAgfQogICAgICAgIGludCBtaWQgPSBzICsgKGUgLSBzKSAvIDI7CiAgICAgICAgaWYociA8PSBtaWQpewogICAgICAgICAgICByZXR1cm4gcXVlcnkoMipOICsgMSwgcywgbWlkLCBsLCByKTsKICAgICAgICB9ZWxzZSBpZihsID4gbWlkKXsKICAgICAgICAgICAgcmV0dXJuIHF1ZXJ5KDIqTiArIDIsIG1pZCArIDEsIGUsIGwsIHIpOwogICAgICAgIH1lbHNlewogICAgICAgICAgICBub2RlIExDLCBSQywgZGlzaGE7CiAgICAgICAgICAgIExDID0gcXVlcnkoMipOICsgMSwgcywgbWlkLCBsLCByKTsKICAgICAgICAgICAgUkMgPSBxdWVyeSgyKk4gKyAyLCBtaWQgKyAxLCBlLCBsLCByKTsKICAgICAgICAgICAgZGlzaGEgPSBuZXcgbm9kZSgwKTsKICAgICAgICAgICAgZGlzaGEuU1VNID0gTEMuU1VNICsgUkMuU1VNOwogICAgICAgICAgICBkaXNoYS5QID0gbWF4KExDLlAsIExDLlNVTSArIFJDLlApOwogICAgICAgICAgICBkaXNoYS5NID0gTEMuUyArIFJDLlA7CiAgICAgICAgICAgIGRpc2hhLlMgPSBtYXgoUkMuUywgUkMuU1VNICsgTEMuUyk7CiAgICAgICAgICAgIGRpc2hhLkIgPSBtYXgoZGlzaGEuU1VNLCBtYXgoZGlzaGEuUCwgbWF4KGRpc2hhLk0sIG1heChkaXNoYS5TLCBtYXgoTEMuQiwgUkMuQikpKSkpOwogICAgICAgICAgICByZXR1cm4gZGlzaGE7CiAgICAgICAgfQogICAgfQogICAgCiAgICB2b2lkIHNvbHZlKCl7CiAgICAgICAgLy9FbnRlciBjb2RlIGhlcm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