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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <bits/stdc++.h>
#define LL long long int
#define LLU unsigned long long
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define FOR(i, f, n) for(int i = (f); i <= (n); ++i)
using namespace std;


const int N = 500015;

int n, m;
LLU d, b;
int currZap;

int a[N];
LLU f[N];

LLU** daysTree, **heightsTree;
int **zapTree;


class uint128_t{
    private:
        uint64_t UPPER, LOWER;

    public:
        uint128_t();
        uint128_t(const uint128_t & rhs);

        template <typename T> uint128_t(const T & rhs){
            UPPER = 0;
            LOWER = rhs;
        }

        template <typename S, typename T> uint128_t(const S & upper_rhs, const T & lower_rhs){
            UPPER = upper_rhs;
            LOWER = lower_rhs;
        }

        uint64_t upper() const;
        uint64_t lower() const;

        uint128_t operator=(const uint128_t & rhs);

        template <typename T> uint128_t operator=(const T & rhs){
            UPPER = 0;
            LOWER = rhs;
            return *this;
        }

        operator uint64_t() const;

        uint128_t operator+(const uint128_t & rhs) const;
        uint128_t operator+=(const uint128_t & rhs);
        uint128_t operator-(const uint128_t & rhs) const;
        uint128_t operator-=(const uint128_t & rhs);
        uint128_t operator*(const uint128_t & rhs) const;
        uint128_t operator*=(const uint128_t & rhs);

    public:

        template <typename T> uint128_t operator+(const T & rhs) const{
            return uint128_t(UPPER + ((LOWER + (uint64_t) rhs) < LOWER), LOWER + (uint64_t) rhs);
        }

        template <typename T> uint128_t operator+=(const T & rhs){
            UPPER = UPPER + ((LOWER + rhs) < LOWER);
            LOWER = LOWER + rhs;
            return *this;
        }

        template <typename T> uint128_t operator-(const T & rhs) const{
            return uint128_t((uint64_t) (UPPER - ((LOWER - rhs) > LOWER)), (uint64_t) (LOWER - rhs));
        }

        template <typename T> uint128_t operator-=(const T & rhs){
            *this = *this - rhs;
            return *this;
        }

        template <typename T> uint128_t operator*(const T & rhs) const{
            return (*this) * (uint128_t(rhs));
        }

        template <typename T> uint128_t operator*=(const T & rhs){
            *this = *this * uint128_t(rhs);
            return *this;
        }
};

uint64_t uint128_t::upper() const{
    return UPPER;
}

uint64_t uint128_t::lower() const{
    return LOWER;
}

template <typename T> T operator+(const T & lhs, const uint128_t & rhs){
    return (T) (rhs + lhs);
}

template <typename T> T & operator+=(T & lhs, const uint128_t & rhs){
    lhs = (T) (rhs + lhs);
    return lhs;
}

template <typename T> T operator-(const T & lhs, const uint128_t & rhs){
    return (T) (uint128_t(lhs) - rhs);
}

template <typename T> T & operator-=(T & lhs, const uint128_t & rhs){
    lhs = (T) (uint128_t(lhs) - rhs);
    return lhs;
}

template <typename T> T operator*(const T & lhs, const uint128_t & rhs){
    return lhs * (T) rhs.lower();
}

template <typename T> T & operator*=(T & lhs, const uint128_t & rhs){
    lhs *= (T) rhs.lower();
    return lhs;
}


uint128_t::uint128_t(){
    UPPER = 0;
    LOWER = 0;
}

uint128_t::uint128_t(const uint128_t & rhs){
    UPPER = rhs.UPPER;
    LOWER = rhs.LOWER;
}

uint128_t uint128_t::operator=(const uint128_t & rhs){
    UPPER = rhs.UPPER;
    LOWER = rhs.LOWER;
    return *this;
}

uint128_t::operator uint64_t() const{
    return (uint64_t) LOWER;
}

uint128_t uint128_t::operator+(const uint128_t & rhs) const{
    return uint128_t(UPPER + rhs.UPPER + ((LOWER + rhs.LOWER) < LOWER), LOWER + rhs.LOWER);
}

uint128_t uint128_t::operator+=(const uint128_t & rhs){
    UPPER = rhs.UPPER + UPPER + ((LOWER + rhs.LOWER) < LOWER);
    LOWER += rhs.LOWER;
    return *this;
}

uint128_t uint128_t::operator-(const uint128_t & rhs) const{
    return uint128_t(UPPER - rhs.UPPER - ((LOWER - rhs.LOWER) > LOWER), LOWER - rhs.LOWER);
}

uint128_t uint128_t::operator-=(const uint128_t & rhs){
    *this = *this - rhs;
    return *this;
}

uint128_t uint128_t::operator*(const uint128_t & rhs) const{
    // split values into 4 32-bit parts
    uint64_t top[4] ={UPPER >> 32, UPPER & 0xffffffff, LOWER >> 32, LOWER & 0xffffffff};
    uint64_t bottom[4] ={rhs.UPPER >> 32, rhs.UPPER & 0xffffffff, rhs.LOWER >> 32, rhs.LOWER & 0xffffffff};
    uint64_t products[4][4];

    for(int y = 3; y > -1; y--){
        for(int x = 3; x > -1; x--){
            products[3 - x][y] = top[x] * bottom[y];
        }
    }

    // initial row
    uint64_t fourth32 = products[0][3] & 0xffffffff;
    uint64_t third32 = (products[0][2] & 0xffffffff) + (products[0][3] >> 32);
    uint64_t second32 = (products[0][1] & 0xffffffff) + (products[0][2] >> 32);
    uint64_t first32 = (products[0][0] & 0xffffffff) + (products[0][1] >> 32);

    // second row
    third32 += products[1][3] & 0xffffffff;
    second32 += (products[1][2] & 0xffffffff) + (products[1][3] >> 32);
    first32 += (products[1][1] & 0xffffffff) + (products[1][2] >> 32);

    // third row
    second32 += products[2][3] & 0xffffffff;
    first32 += (products[2][2] & 0xffffffff) + (products[2][3] >> 32);

    // fourth row
    first32 += products[3][3] & 0xffffffff;

    // combines the values, taking care of carry over
    return uint128_t(first32 << 32, 0) + uint128_t(third32 >> 32, third32 << 32) + uint128_t(second32, 0) + uint128_t(fourth32);
}

uint128_t uint128_t::operator*=(const uint128_t & rhs){
    *this = *this * rhs;
    return *this;
}


uint128_t dsum[N];


struct C
{
    C() : day(0), height(0), zap(0) {}
    C(LLU day, LLU height, int zap) : day(day), height(height), zap(zap) {}
    LLU day, height;
    int zap;
};

void insert(int a)
{
    ++a;
    int i = 0;
    while(a)
    {
        if(a&1)
        {
            daysTree[i][a-1] = d;
            heightsTree[i][a-1] = b;
            zapTree[i][a-1] = currZap;
        }
        a >>= 1;
        ++i;
    }
}

C query(int a)
{
    int i = 0;
    C ret;
    while(i<19)
    {
        if(daysTree[i][a] > ret.day)
        {
            ret.day = daysTree[i][a];
            ret.height = heightsTree[i][a];
            ret.zap = zapTree[i][a];
        }
        a >>= 1;
        ++i;
    }
    return ret;
}

LLU getHeightAt(int id)
{
    C c = query(id);
    return c.height+(d-c.day)*a[id];
}


unsigned long long wynik()
{
    if(getHeightAt(0) <= b)
    {
        dsum[currZap] = dsum[currZap-1];
        return 0;
    }

    //szukamy binsearchem dokąd ścinamy
    int mini = 0, maxi = n-1, best = 0;

    while(mini <= maxi)
    {
        int mid = (mini+maxi)/2;
        LLU h = getHeightAt(mid);
        if(h >= b)
        {
            mini = mid+1;
            best = mid;
        }
        else
        {
            maxi = mid-1;
        }
    }

    //tniemy trawę od 0 do best włącznie
    C c = query(best);
    //tyle trawa mogła urosnąć w dniach od ostatniego ścięcia
    uint128_t cut = uint128_t(f[best])*(d-c.day);
    //dodajemy to co nie zostało wtedy ścięte
    cut += c.height*(best+1);
    //odejmujemy to, co zostało ścięte przed best
    cut -= dsum[currZap-1]-dsum[c.zap];
    //odejmujemy to, czego jeszcze nie ścięliśmy
    cut -= (best+1)*b;

    dsum[currZap] = cut+dsum[currZap-1];
    insert(best);
    return (LLU)cut;
}


void init()
{
    f[0] = a[0];
    for(int i = 1; i < n; ++i)
        f[i] = f[i-1]+a[i];

    daysTree = new LLU*[19];
    heightsTree = new LLU*[19];
    zapTree = new int*[19];
    REP(i, 19)
    {
        daysTree[i] = new LLU[1<<(19-i)];
        heightsTree[i] = new LLU[1<<(19-i)];
        zapTree[i] = new int[1<<(19-i)];
    }
}


int main()
{
    scanf("%d%d", &n, &m);

    REP(i, n)
        scanf("%d", &a[i]);
    sort(a, a+n, greater<int>());

    init();

    for(currZap = 1; currZap <= m; ++currZap)
    {
        scanf("%llu%llu", &d, &b);
        printf("%llu\n", wynik());
    }

    return 0;
}