#include <fstream>
#include <vector>
using namespace std;
using int64 = long long;
struct Node
{
int64 left, right;
int64 best, total;
};
using Aint = vector<Node>;
const int64 kInf = (1LL << 50);
const Node kMinNode = (Node){-kInf, -kInf, -kInf, -kInf};
inline int LeftSon(int n) { return (n << 1); }
inline int RightSon(int n) { return (n << 1) + 1; }
Node Combine(const Node &lson, const Node &rson)
{
Node father = kMinNode;
father.left = max(lson.left, lson.total + rson.left);
father.right = max(rson.right, rson.total + lson.right);
father.total = lson.total + rson.total;
father.best = max(lson.best, rson.best);
father.best = max(father.best, lson.right + rson.left);
return father;
}
void Update(Aint &aint, int ind, int l, int r, int pos, int val)
{
if (l == r) {
aint[ind] = (Node){val, val, val, val};
return;
}
int mid = l + (r - l) / 2;
if (pos <= mid) {
Update(aint, LeftSon(ind), l, mid, pos, val);
} else {
Update(aint, RightSon(ind), mid + 1, r, pos, val);
}
aint[ind] = Combine(aint[LeftSon(ind)], aint[RightSon(ind)]);
}
Node Query(const Aint &aint, int ind, int l, int r, int x, int y)
{
if (x <= l && r <= y) {
return aint[ind];
}
int mid = l + (r - l) / 2;
Node node_left = kMinNode;
Node node_right = kMinNode;
if (x <= mid) {
node_left = Query(aint, LeftSon(ind), l, mid, x, y);
}
if (y > mid) {
node_right = Query(aint, RightSon(ind), mid + 1, r, x, y);
}
return Combine(node_left, node_right);
}
int main()
{
ifstream fin("sequencequery.in");
ofstream fout("sequencequery.out");
int n, m;
fin >> n >> m;
Aint aint(4 * n + 4, kMinNode);
for (int i = 1; i <= n; ++i) {
int x;
fin >> x;
Update(aint, 1, 1, n, i, x);
}
while (m--) {
int x, y;
fin >> x >> y;
fout << Query(aint, 1, 1, n, x, y).best << "\n";
}
return 0;
}