# include <bits/stdc++.h>
# define maxn 100001
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
# define pb push_back
# define mp make_pair
# define int ll
using namespace std;
const int inf = INT_MAX;
int a[maxn];
int n,m,l,r;
struct data
{
int sum;
int best;
int lbest;
int rbest;
}t[4 * maxn];
data combo(data a,data b)
{
data tmp;
tmp.sum = a.sum + b.sum;
tmp.lbest = max({tmp.sum,a.lbest,a.sum + b.lbest});
tmp.rbest = max({tmp.sum,b.rbest,b.sum + a.rbest});
tmp.best = max({tmp.lbest,tmp.rbest,a.rbest+b.lbest,tmp.sum,a.best,b.best});
return tmp;
};
void build(int nod,int l,int r)
{
if(l == r)
{
t[nod] = {a[l],a[l],a[l],a[l]};
return ;
}
int mid = l + r >> 1;
build(nod << 1,l,mid);
build(nod << 1 | 1,mid + 1,r);
t[nod] = combo(t[nod << 1],t[nod << 1 | 1]);
}
data qry(int nod,int tl,int tr,int l,int r)
{
if(l > r) return {-inf,-inf,-inf,-inf};
if(l == tl && r == tr) return t[nod];
int mid = tl + tr >> 1;
return combo(qry(nod << 1,tl,mid,l,min(mid,r)),qry(nod << 1 | 1,mid + 1,tr,max(mid + 1,l),r));
}
int32_t main(){_
freopen("sequencequery.in","r",stdin);
freopen("sequencequery.out","w",stdout);
cin >> n >> m;
for(int i = 1;i<=n;i++)
cin >> a[i];
build(1,1,n);
while(m --)
{
cin >> l >> r;
cout << qry(1,1,n,l,r).best << '\n';
}
return 0;
}