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
#include <stdio.h>

int n;
int m;

long long ai[500001];
long long ac[500001];
long long di[500001];
long long bi[500001];

long long result;


int cutsCount;
long long cutsd[500001];
long long cutsb[500001];
int cutss[500001];
int cutse[500001];


void heapify (long long *tab, int heap_size, int i)
{
    long long temp;
    int largest;
    int l = 2 * i;
    int r = (2 * i) + 1;

    if (l <= heap_size && tab[l] > tab[i]) {
        largest=l;
    } else {
        largest=i;
    }

    if (r <= heap_size && tab[r] > tab[largest]) {
        largest=r;
    }
        
    if (largest!=i)
    {
        temp=tab[largest];
        tab[largest]=tab[i];
        tab[i]=temp;
        heapify(tab,heap_size,largest);
    }
}


void makeHeap(long long *tab, int heap_size)
{
    int i;

    for (i = heap_size/2; i > 0; --i) {
        heapify(tab, n, i);
    }
}
 

void sort(long long *tab, int heap_size)
{
    long long temp;
    int i;

    makeHeap(tab, heap_size);
    
    for (i = heap_size; i > 1; --i)
    {
        temp = tab[i];
        tab[i] = tab[1];
        tab[1] = temp;
        --heap_size;
        heapify(tab, heap_size, 1);
    }
}


int aDoable(long long b, long long d, int a, int i) {
    int res = 0;

    if (b + (di[i] - d) * ai[a] > bi[i]) {
        res = 1;
    }

    return res;
}


int boxDoable(int box, int i) {
    int res = 0;

    if (aDoable(cutsb[box], cutsd[box], cutse[box], i)) {
        res = 1;
    }

    return res;
}


int findCutBoxWorker(int s, int e, int i) {
    int res = -1;
    int count = e - s;
    if (count == 1) {
        if (boxDoable(s, i)) {
            return s;
        } else {
            return -1;
        }
    } else {
        int ne = (s + e) / 2;
        if (ne == s) ne += 1;
        if (boxDoable(ne, i)) {
            res = findCutBoxWorker(s, ne, i);
            if (res < 0) {
                res = ne;
            }
        } else {
            res = findCutBoxWorker(ne, e, i);
        }
    }

    return res;
}


int findCutBox(int i) {
    int res = -1;
    if (! boxDoable(cutsCount - 1, i)) {
        return res;
    }

    if (cutsCount < 3) {
        res = 1;
        if (boxDoable(0, i)) { 
            res = 0;
        }
    } else {
        res = findCutBoxWorker(0, cutsCount, i);
    }

    return res;
}


int findCutWorker(int s, int e, long long b, long long d, int i) {
    int res = -1;
    int count = e - s;
    if (count < 3) {
        if (aDoable(b, d, s, i)) {
            res = s;
        }
        if (count == 1) {
            return res;
        } else {
            if (res < 0 && aDoable(b, d, s+1, i)) {
                res = s+1;
            }
            return res;
        }
    } else {
        int ne = (s + e) / 2;
        if (ne == s) ne += 1;
        if (aDoable(b, d, ne, i)) {
            res = findCutWorker(s, ne, b, d, i);
            if (res < 0) {
                res = ne;
            }
        } else {
            res = findCutWorker(ne, e, b, d, i);
        }
    }

    return res;
}


int findCut(int box, int i) {
    int res = cutse[box];
    int size = cutse[box] - cutss[box] + 1;
    if (size < 3) {
        if (aDoable(cutsb[box], cutsd[box], cutss[box], i)) {
            res = cutss[box];
        }
    } else {
        res = findCutWorker(cutss[box], cutse[box] + 1, cutsb[box], cutsd[box], i);
    }

    return res;
}


long long calculate(int box, int a, int i) {
    long long res = 0;
    long long acc;
    int j;

    // calculate for given box
    acc = ac[a];
    if (box < cutsCount - 1) {
        acc -= ac[cutss[box+1]];
    }
    res = acc * (di[i] - cutsd[box]);
    res -= (bi[i] * (cutse[box] + 1 - a));
    res += (cutsb[box] * (cutse[box] + 1 - a));

    // calculate remaining boxes
    for (j = box + 1; j < cutsCount; ++j) {
        acc = ac[cutss[j]];
        if (j < cutsCount - 1) {
            acc -= ac[cutss[j+1]];
        }
        res += acc * (di[i] - cutsd[j]);
        res -= (bi[i] * (cutse[j] + 1 - cutss[j]));
        res += (cutsb[j] * (cutse[j] + 1 - cutss[j]));
    }

    return res;
}


void splitMergeBox(int box, int a, int i) {
    if (a == cutss[box]) {
        cutsCount = box + 1;
        cutsb[box] = bi[i];
        cutsd[box] = di[i];
        cutse[box] = n;
    } else {
        cutsCount = box + 1;
        cutse[box] = a - 1;
        cutsd[cutsCount] = di[i];
        cutsb[cutsCount] = bi[i];
        cutss[cutsCount] = a;
        cutse[cutsCount] = n;
        cutsCount += 1;
    }
}

int main() {
    int i;
    int cutboxIdx = 0;
    int cutIdx = 0;

    scanf("%d %d", &n, &m);

    for (i = 1; i <= n; ++i) {
        scanf("%lld", &ai[i]);
    }
    for (i = 0; i < m; ++i) {
        scanf("%lld %lld", &di[i], &bi[i]);
    }

    sort(ai,n);
    ac[n] = ai[n];
    for (i = n - 1; i > 0; --i) {
        ac[i] = ac[i+1] + ai[i];
    }

    cutsCount = 1;
    cutsd[0]  = 0;
    cutsb[0]  = 0;
    cutss[0]  = 1;
    cutse[0]  = n;
    for (i = 0; i < m; ++i) {
        result = 0;

        // find cut place container
        cutboxIdx = findCutBox(i);

        if (cutboxIdx >= 0) {
            // find cut place
            cutIdx = findCut(cutboxIdx, i);

            // calculate accross containers
            result = calculate(cutboxIdx, cutIdx, i);

            // split-merge containers
            splitMergeBox(cutboxIdx, cutIdx, i);
        }

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

    return 0;
}