#include <vector>
#include <string>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <bitset>
#include <stack>
#include <list>
#include <numeric>
#include <algorithm>
#include <random>
#include <chrono>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <climits>
#include <cctype>
#include <cmath>
#include <ctime>
#include <cassert>
using namespace std;
#define ULL unsigned long long
#define LL long long
#define PII pair <int, int>
#define VB vector <bool>
#define VI vector <int>
#define VLL vector <LL>
#define VD vector <double>
#define VS vector <string>
#define VPII vector <pair <int, int> >
#define VVI vector < VI >
#define VVB vector < VB >
#define SI set < int >
#define USI unordered_set <int>
#define MII map <int, int>
#define UMII unordered_map <int, int>
#define MS multiset
#define US unordered_set
#define UM unordered_map
#define UMS unordered_multiset
#define UMM unordered_multimap
#define FORN(i, n) for(int i = 0; i < (n); ++i)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define SZ size()
#define BG begin()
#define EN end()
#define CL clear()
#define X first
#define Y second
#define RS resize
#define PB push_back
#define MP make_pair
#define ALL(x) x.begin(), x.end()
#define INS insert
#define ER erase
#define CNT count
template <class T> ostream& operator <<(ostream & os, const vector<T> &vec)
{
for (int i = 0; i < vec.size() - 1; ++i) os << vec[i] << ' ';
return os << vec[vec.size() - 1];
}
template <class T1, class T2> ostream& operator <<(ostream & os, const pair<T1, T2> &p)
{
return os << p.X << " " << p.Y;
}
template <typename T>
void pr(T var1)
{
cout << var1 << '\n';
}
template <typename T, typename... Types>
void pr(T var1, Types... var2)
{
cout << var1;
pr(var2...);
}
#define MAX 500
int n, q;
VI a, buy, sell;
VVI sol, pos;
int main()
{
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> q;
a.RS(n + 1);
pos.RS(MAX + 1);
FOR(i, 1, n)
{
cin >> a[i];
pos[a[i]].PB(i);
}
sol.RS(MAX + 1, VI(MAX + 1));
sell.RS(n + 1);
FOR(i, 1, MAX - 1)
{
VI res(buy.SZ + pos[i].SZ);
merge(ALL(buy), ALL(pos[i]), res.BG);
buy = res;
if (buy.empty()) continue;
int profit = -a[buy[0]];
SI unpaired = {buy[0]}, paired;
fill(ALL(sell), -1);
FORD(j, MAX, i + 1)
{
for(int sellPos : pos[j])
{
auto buyPos = unpaired.lower_bound(sellPos);
if (buyPos != unpaired.BG)
{
--buyPos;
int aux = *buyPos;
profit += a[sellPos];
sell[*buyPos] = sellPos;
unpaired.ER(buyPos);
paired.INS(aux);
auto it = upper_bound(ALL(buy), sellPos);
if (it == buy.EN) continue;
profit -= a[*it];
unpaired.INS(*it);
}
else
{
auto buyPos = paired.lower_bound(sellPos);
if (buyPos == paired.BG) continue;
--buyPos;
int aux = *buyPos;
int oldSellPos = sell[aux];
if (oldSellPos > sellPos)
{
profit += a[sellPos] - a[oldSellPos];
auto it = upper_bound(ALL(buy), sellPos);
if (it != buy.EN && !paired.CNT(*it) && !unpaired.CNT(*it))
{
unpaired.INS(*it);
profit -= a[*it];
}
sell[aux] = sellPos;
buyPos = unpaired.lower_bound(oldSellPos);
if (buyPos == unpaired.BG) continue;
--buyPos;
aux = *buyPos;
if (sell[aux] == -1)
{
profit += a[oldSellPos];
sell[*buyPos] = oldSellPos;
unpaired.ER(buyPos);
paired.INS(aux);
auto it = upper_bound(ALL(buy), oldSellPos);
if (it != buy.EN && !paired.CNT(*it) && !unpaired.CNT(*it))
{
profit -= a[*it];
unpaired.INS(*it);
}
}
}
}
}
sol[i][j] = profit;
}
}
FOR(i, 1, q)
{
int x, y;
cin >> x >> y;
cout << sol[x][y] << "\n";
}
return 0;
}