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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;



struct S{
  int left;
  long long d;
  long long b;
};

S stos[1<<20];
int sp = 0;
  
long long sumSuf[1<<20];

inline int policzIdx(int elem)
{
  int left = 0, right = sp + 1;
  
  while (left < right)
  {
    int s = (left + right) / 2;
    int w = stos[sp - s].left;
    if (w >= elem)
      right = s;
    else
      left = s + 1;
  }
  
  return left;
}


int main()
{
  int n, m;
  scanf("%d%d", &n, &m);
  
  vector<int> ary;
  memset(stos, 0, sizeof(stos));
  memset(sumSuf, 0, sizeof(sumSuf));
  
  stos[0].left = n;
  stos[0].d = 0;
  stos[0].b = 0;
  
  for (int i = 0; i < n; i++)
  {
    int a;
    scanf("%d", &a);
    ary.push_back(a);
  }
  
  sort(ary.begin(), ary.end());
  
  for (int i = 0; i < ary.size(); i++)
  {
    sumSuf[i + 1] = sumSuf[i] + ary[n - i - 1];
  }
  
  
    int left = 0, right = n;
    
  /*while (left < right)
  {
    int s = (left + right) / 2;
    long long w = ary[n - s - 1];
    if (w >= 1)
      right = s;
    else
      left = s + 1;
  }
  printf("Idx %d\n", left);
      
    
  return 0;*/
  
  
  //return 0;
  long long pref = 0;
  for (int i = 0; i < m; i++)
  {
    long long d, b;
    scanf("%lld%lld", &d, &b);
    
    int left = 0, right = n;
    
    while (left < right)
    {
      int s = (left + right) / 2;
      
      int idx = policzIdx(n - s);
      
      S el = stos[sp - idx];
      
      long long w = el.b + ary[s] * (d - el.d);
      if (w >= b)
        right = s;
      else
        left = s + 1;
    }
    //printf("i = %d\n", i);
    
    long long ile = 0;
    int zPrawej = n - left;
    
    //printf("zPrawej = %d\n", zPrawej);
    
    //przed pierwszym el. tj. cala trawa ucieta

    if (left == 0)
    {
      long long popd = stos[sp].d;
      int popleft = 0;
      while (sp >= 0)
      {
        ile +=  (stos[sp].left - popleft) * stos[sp].b + (d - stos[sp].d) * (sumSuf[stos[sp].left] - sumSuf[popleft]);
        popleft = stos[sp].left;
        sp--;
      }
      sp = 0;
      stos[sp].left = zPrawej;
      stos[sp].d = d;
      stos[sp].b = b;
      ile -= b * zPrawej;
    } else
    //poza ostatnim, tj. nic nie przyciete
    if (left >= n)
    {
      //nic
    }
    else
    //gdzies pomiedzy
    {
      long long popd = stos[sp].d;
      int popleft = 0;
      while (stos[sp].left < zPrawej)
      {
        ile +=  (stos[sp].left - popleft) * stos[sp].b + (d - stos[sp].d) * (sumSuf[stos[sp].left] - sumSuf[popleft]);
        popleft = stos[sp].left;
        sp--;
      }
      
      {
        
        ile +=  (zPrawej - popleft) * stos[sp].b + (d - stos[sp].d) * (sumSuf[zPrawej] - sumSuf[popleft]);
        if (zPrawej > popleft)
          sp++;
        //sp++;
      }
      //else sp++;
      
      stos[sp].left = zPrawej;
      stos[sp].d = d;
      stos[sp].b = b;
      ile -= b * zPrawej;
    }
    
    printf("%lld\n", ile);
    
    //printf("Idx %d\n", left);
    
    //while (stos[sp].left )
    
  }
  
  
  return 0;
}