#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector< ii > vii;
#define INF 0x3F3F3F3F
#define LINF 0x3F3F3F3F3F3F3F3FLL
#define pb push_back
#define mp make_pair
#define pq priority_queue
#define LSONE(s) ((s)&(-s)) //LASTBIT
#define DEG_to_RAD(X) (X * PI / 180)
#define F first
#define S second
#define PI 2*acos(0)
#ifdef ONLINE_JUDGE
#define debug(args...)
#else
#define debug(args...) fprintf(stderr,args)
#endif
//////////////////////
int dx[] = {1,-1,0,0};
int dy[] = {0,0,-1,1};
//////////////////////
const int N = 100050;
const int SQ = 330;
int v[N];
int n,m;
inline int scan(){
char c = getchar_unlocked();
int x = 0;
bool b=0;
while(c<'0'||c>'9'){
if(c=='-'){
b=1;
}
c=getchar_unlocked();
}
while(c>='0'&&c<='9'){
x=(x<<1)+(x<<3)+c-'0';
c=getchar_unlocked();
}
if(b){
return -x;
}
return x;
}
struct est
{
int l,r,id,sq;
est(){};
est( int a, int b, int c )
{
l = a;
r = b;
id = c;
sq = a/SQ;
}
bool operator < ( est foo ) const
{
if( sq != foo.sq ) return sq < foo.sq;
return r > foo.r;
}
};
int tr[4*N];
void update( int no, int l, int r, int i, int val )
{
if( l == r )
{
tr[no] = val;
return;
}
int nxt = (no<<1), mid = (l+r)>>1;
if( i <= mid ) update(nxt,l,mid,i,val);
else update(nxt+1,mid+1,r,i,val);
tr[no] = max( tr[nxt], tr[nxt+1] );
}
est que[N];
int ans[N];
deque<int>s[N];
void init(){
for(int i=0;i<N;++i){
update(1,0,N-1,i,0);
s[i].clear();
}
}
int main()
{
//ios::sync_with_stdio(0);
register int i,l,r;
n = scan();
m = scan();
for(i=1;i<=n;++i) v[i] = scan(), v[i] += v[i-1];
for(i=0;i<=n;++i) v[i] += 50000;
for(i=0;i<m;++i){
int a,b; a = scan(); b = scan(); a--;
que[i] = est(a,b,i);
}
sort(que,que+m);
int ult =- 1;
for(i=0;i<m;++i){
if( ult != que[i].sq ){
init();
l = r = que[i].l;
s[v[r]].push_front(r);
while( r < que[i].r ){
r++;
s[v[r]].push_front(r);
int a = s[v[r]].front();
int b = s[v[r]].back();
update(1,0,N-1,v[r],abs(b-a));
}
}
while( l < que[i].l ){
s[v[l]].pop_back();
if( s[v[l]].size() == 0 ) update(1,0,N-1,v[l],0);
else{
int a = s[v[l]].front();
int b = s[v[l]].back();
update(1,0,N-1,v[l],abs(b-a));
}
l++;
}
while( l > que[i].l ){
l--;
s[v[l]].push_back(l);
int a = s[v[l]].front();
int b = s[v[l]].back();
update(1,0,N-1,v[l],abs(b-a));
}
while( r > que[i].r ){
s[v[r]].pop_front();
if( s[v[r]].size() == 0 ) update(1,0,N-1,v[r],0);
else{
int a = s[v[r]].front();
int b = s[v[r]].back();
update(1,0,N-1,v[r],abs(b-a));
}
r--;
}
ans[que[i].id] = tr[1];
ult = que[i].sq;
}
for(i=0;i<m;++i) printf("%d\n",ans[i]);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdHlwZWRlZiBsb25nIGxvbmcgbGw7CnR5cGVkZWYgbG9uZyBkb3VibGUgbGQ7CnR5cGVkZWYgdW5zaWduZWQgbG9uZyBsb25nIHVsbDsKdHlwZWRlZiBwYWlyPGxsLGxsPiBpaTsKdHlwZWRlZiB2ZWN0b3I8bGw+IHZpOwp0eXBlZGVmIHZlY3RvcjwgaWkgPiB2aWk7CgojZGVmaW5lIElORiAweDNGM0YzRjNGCiNkZWZpbmUgTElORiAweDNGM0YzRjNGM0YzRjNGM0ZMTAojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIHBxIHByaW9yaXR5X3F1ZXVlCiNkZWZpbmUgTFNPTkUocykgKChzKSYoLXMpKSAvL0xBU1RCSVQKI2RlZmluZSBERUdfdG9fUkFEKFgpICAgKFggK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