1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//Tadrion
#include <cstdio>
#include <vector>
#include <iostream>
#include <deque>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <utility>
using namespace std;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define CLEAR(x) (memset(x,0,sizeof(x)))
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define VAR(v, n) __typeof(n) v = (n)
#define FOR(x, b, e) for(int x = b; x <= (e); ++x)
#define FORD(x, b, e) for(int x = b; x >= (e); --x)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define FOREACH(i, c) for(VAR(i,(c).begin()); i != (c).end(); ++i)
#define DBG(v) cout<<#v<<" = "<<v<<endl;
#define IN(x,y) ((y).find(x)!=(y).end())
#define ST first
#define ND second
#define PB push_back
#define PF push_front
#define MP make_pair
typedef long long int LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

const int M = (1<<19LL);
pair<LL,LL> t[(1<<20LL)+10LL];

void ins(pair<LL,LL> x, int a, int b) {
    a+=M; b+=M;

    t[a] = MAX(t[a],x);
    t[b] = MAX(t[b],x);

    while(a/2 != b/2) {
        if(a%2==0) t[a+1] = MAX(t[a+1],x);
        if(b%2==1) t[b-1] = MAX(t[b-1],x);
        a/=2; b/=2;
    }
}

pair<LL,LL> query(int a) {
    a += M;
    pair<LL, LL> res = t[a];
    a/=2;

    while(a != 0) {
        res = MAX(t[a],res);
        a/=2;
    }
    return res;
}

LL suf[500010];
vector<LL> a;
LL n,m,d,b,oo;

stack< pair<LL,LL> > s; //indeks, ile straty

int main() {
  scanf("%lld %lld",&n,&m);
  FOR(i,1,n) {
    scanf("%lld",&oo);
    a.PB(oo);
  }
  a.PB(0);
  //trawy od 1 do n mamy, w 0 trawa straznik
  sort(a.begin(),a.end());
  FORD(i,n,0) {
    suf[i] = suf[i+1] + a[i];
  }

  FOR(i,0,(1<<20LL)) {
    t[i] = MP(0,0);
  }

  //query - nr dnia, wysokosc
  FOR(i,1,m) {
    scanf("%lld %lld",&d,&b);
    //czy ostatnia by juz zdazyla urosnac
    pair<LL,LL> ost = query(n);
    LL ans = 0;
    if((d-ost.ST)*a[n] + ost.ND <= b) {
            //printf("NIC NIE TNE!!!\n");
            printf("0\n");
            continue;
    }
    else {
        //szukamy ostatniej scietej
        int st = 0;
        int en = n;
        int scieta = n;
        while(en-st > 1) {
            int m = (en+st) / 2;
            pair<LL,LL> p = query(m);
            if((d-p.ST)*a[m] + p.ND <= b) st = m;
            else {
                scieta = m;
                en = m;
            }
        }
        //printf("scinamy od %d do konca, ostatnio scieta dnia %lld do wysokosci %lld\n",scieta,query(scieta).ST,query(scieta).ND);
        //scinamy do b trawy scieta, scieta+1 ... n
        LL lost = 0;
        while(!s.empty()) {
            pair<LL,LL> curT = s.top();
            if(curT.ST <= scieta) break;
            else {
                if(curT.ST > scieta) {
                    s.pop();
                    lost += curT.ND;
                }
            }
        }
            pair<LL,LL> sc = query(scieta);
            ans = (d-sc.ST)*suf[scieta] + (sc.ND-b)*(n-scieta+1) - lost;
            s.push(MP(scieta,ans+lost));

            ins(MP(d,b),scieta,n);

            printf("%lld\n",ans);
        }

    }

  return 0;
}