template <typename T> voidread(T& x) { char c = getchar(); int neg = 0; x = 0; for (; !isdigit(c); c = getchar()) neg |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c - '0'); if (neg) x = -x; }
template <typename T> T max(T x, T y) { if (x > y) return x; else return y; }
int n, m, ql, qr, ans, rans; int a[50010];
classSegmentTree { public: int M[200010], L[200010], R[200010], S[200010];
voidbuild(int rt, int l, int r) { if (l == r) { M[rt] = L[rt] = R[rt] = S[rt] = a[l]; } else { int m = l + r >> 1; build(rt << 1, l, m); build(rt << 1 | 1, m + 1, r); pushUp(rt); } }
voidquery(int rt, int l, int r) { int m = l + r >> 1; if (l >= ql && r <= qr) { ans = max(ans, max(M[rt], rans + L[rt])); rans = max(R[rt], rans + S[rt]); } else { if (ql <= m) query(rt << 1, l, m); if (m < qr) query(rt << 1 | 1, m + 1, r); } } };
SegmentTree sgt;
intmain() {
read(n); for (int i = 1; i <= n; ++i) { read(a[i]); } sgt.build(1, 1, n); for (read(m); m; --m) { read(ql); read(qr); ans = rans = -0x7f7f7f7f; sgt.query(1, 1, n); std::cout << ans << std::endl; } return0; }