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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
///makra zaczerpniete z "Algorytmiki Praktycznej" Piotra Stanczyka
#include <cstdio>
//#include <iostream>
#include <algorithm>
//#include <string>
#include <vector>
//#include <complex>
//#include <iterator>
//#include <set>
//#include <bitset>
//#include <map>
//#include <stack>
//#include <list>
//#include <queue>
//#include <deque>
using namespace std;
typedef vector<int> VI;
typedef long long LL;
#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 VAR(v, n) typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define FOREACH(i, c) for(VAR(i, (c).begin()); i !=(c).end(); ++i)
#define FORE(i, c) for(VAR(i, (c).begin()), KS=(c).end(); i !=KS; ++i)
#define PB push_back
#define ST first
#define ND second
const int INF = (1<<30)-1;
const double EPS = 10e-9;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<double> VD;
//typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
#define PF push_front
#define MP make_pair
struct bigint
{
  LL a,b; ///a - mala czesc, b - duza czesc
  inline bigint(const LL aa=0,const LL bb=0)
  {
    a=aa;
    b=bb;
  }
  inline void operator = (const bigint n)
  {
    a=n.a;
    b=n.b;
  }
  template <class T> inline void operator = (const T n)
  {
    a=n&INF;
    b=(n-a)>>30;
//    if(a<0 || b<0 || b>xx)
//      printf("=");
  }
  inline void operator += (const bigint n)
  {
    a+=n.a;
    b+=n.b;
    b+=a>>30;
    a&=INF;
//    if(a<0 || b<0 || b>xx)
//      printf("+=");
  }
  inline bigint operator - (const bigint y)
  {
    bigint n;
    n.a=a-y.a;
    n.b=b-y.b;
    if(n.a<0)
    {
      n.a+=(1<<30);
      --n.b;
    }
//    if(n.a<0 || n.b<0 || n.b>xx)
//      printf("-");
    return n;
  }
  inline void operator *= (const bigint x)
  {
    b=a*x.b+b*x.a+((b*x.b)<<30);
    a*=x.a;
    b+=a>>30;
    a&=INF;
//    if(a<0 || b<0 || b>xx)
//      printf("*=");
//    else
//      printf("(*=)");
  }
};
//const bigint B0=bigint(0,0);
inline void pr(const bigint n)
{
//  if(n.b>(1<<30) || n.a<0)
//    printf("pr");
  printf("%lld\n",(n.b<<30)+n.a);
}
struct czt
{
  int a;
  LL d,b;
  bigint i;
  czt(int k=0,LL q=0,LL l=0,bigint m=0)
  {
    a=k; ///a - ktory z tablicy a jest pierwszy do sciecia
    d=q; ///d - dzien sciecia
    b=l; ///j - jak bardzo sciety
    i=m; ///i - ile w sumie scielo
  }
  inline bool czyusu(const LL dd,const LL bb,VI& aa)
  {
    return (dd-d)*aa[a]+b>bb;
  }
  inline LL znaja(const LL dd,const LL bb)
  {
    return (bb-b)/(dd-d);
  }
  inline bigint licz(const LL sa,const LL dd,const LL bb,const int r)
  {
    bigint b1,b2,b3,b4;
    b1=sa;
    b2=dd-d;
    b3=r;
    b4=abs(b-bb);
/*    pr(b1);
    pr(b2);
    pr(b3);
    pr(b4);*/
    b1*=b2;
    b3*=b4;
    if(b>=bb)
      b1+=b3;
    else
      b1=b1-b3;
    return b1;
  }
};
int main()
{
  int n,m,i,aa; ///10^6
  LL d,b; ///10^12
  scanf("%d%d",&n,&m);
  VI a(n+1); ///10^6
  VLL s(n+1); ///10^12
  bigint il,ii; ///10^24
  for(i=0;i<n;++i)
    scanf("%d",&a[i]);
  ++n;
  sort(ALL(a));
  s[n-1]=a[n-1];
  for(i=n-2;i>=0;--i)
  {
    s[i]=s[i+1]+a[i];
  }
  vector<czt> kos(1);
  //VAR(itr,kos.rbegin());
  while(m--)
  {
    scanf("%lld%lld",&d,&b);
    il=0;
    //printf("a");
    while(kos.back().czyusu(d,b,a))
    {
      il+=kos.back().i;
      kos.pop_back();
    }
    //printf("b");
    aa=upper_bound(ALL(a),kos.back().znaja(d,b))-a.begin();
    //printf("c");
    if(aa!=n)
    {
      //printf("%I64d %I64d %I64d %d ",s[aa],d,b,n-aa);
      ii=kos.back().licz(s[aa],d,b,n-aa);
      //printf("d");
      kos.PB(czt(aa,d,b,ii));
      //printf("e");
      pr(ii-il);
      //printf("f");
    }
    else
      printf("0\n");
  }
  return 0;
}