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
#include <cstdio>
#include <algorithm>

struct Node {
    long long int last_h, last_t, sum, sum_t;
};

long long int n, m, a[1000001], d, b, r, size, tree_subadd[1048577], tree_subsize[1048577];
Node tree[1048577];

void update(long long int a, long long int t) {
    tree[a].sum =
        tree[2*a].sum + tree[2*a+1].sum +
        (t-tree[2*a].sum_t)*tree_subadd[2*a] +
        (t-tree[2*a+1].sum_t)*tree_subadd[2*a+1];

    tree[a].sum_t = t;
}

void set(long long int a, long long int b, long long int t, long long int h) {
    a += size, b += size;

    tree[a].last_h = h, tree[a].last_t = tree[a].sum_t = t, tree[a].sum = h;
    tree[b].last_h = h, tree[b].last_t = tree[b].sum_t = t, tree[b].sum = h;

    while (a/2 != b/2) {

        if (a%2 == 0) {
            tree[a+1].last_h = h, tree[a+1].last_t = t, tree[a+1].sum = tree_subsize[a+1] * h,
                tree[a+1].sum_t = t;

//            printf("ustawiam %d, czas do %d, h do %d\n", a+1, t, h);
        }

        if (b%2 == 1) {
            tree[b-1].last_h = h, tree[b-1].last_t = t, tree[b-1].sum = tree_subsize[b-1] * h,
                tree[b-1].sum_t = t;

//            printf("ustawiam %d, czas do %d, h do %d\n", b-1, t, h);
        }

        update(a/2, t);
        update(b/2, t);

        a /= 2, b /= 2;
    }

    while (a > 0) {
        update(a, t);
        a /= 2;
    }
}

long long int get_sum(long long int a, long long int t) {
    long long int last_h, last_t = tree[a].sum_t;

    long long int ta = a/2;
    while (ta > 0) {
        if (last_t < tree[ta].last_t)
            last_t = tree[ta].last_t, last_h = tree[ta].last_h;

        ta /= 2;
    }

//    printf("licze sume dla %d w czasie %lld, last_t %lld, last_h %lld, sum %lld, add %lld",
//            a, t, last_t, last_h, tree[a].sum, tree_subadd[a]);

    long long int r;
    if (last_t == tree[a].sum_t)
        r = tree[a].sum + tree_subadd[a]*(t-last_t);

    else 
        r = tree_subadd[a]*(t-last_t) + tree_subsize[a]*last_h;

//    printf("\tdone = %lld\n", r);
    return r;
}

long long int sum(long long int a, long long int b, long long int t) {
    long long int r = 0;

    a += size, b += size;

    r += get_sum(a, t);
    if (a != b)
        r += get_sum(b, t);

    while (a/2 != b/2) {

        if (a%2 == 0)
            r += get_sum(a+1, t);

        if (b%2 == 1)
            r += get_sum(b-1, t);

        a /= 2, b /= 2;
    }

    return r;
}

int main () {
    scanf("%lld %lld", &n, &m);
    size = 1;
    while (size < n)
        size *= 2;

    for (int i = 0; i < n; i ++) {
        scanf("%lld", &tree_subadd[size+i]);
        tree_subsize[size+i] = 1;
    }

    std::sort(tree_subadd + size, tree_subadd + size + n);

    for (int i = size-1; i >= 1; i --) {
        tree_subadd[i] = tree_subadd[2*i] + tree_subadd[2*i+1];
        tree_subsize[i] = tree_subsize[2*i] + tree_subsize[2*i+1];
    }

    while (m--) {
        scanf("%lld %lld", &d, &b);

        long long int A = 0, B = n-1, C;
        if (get_sum(size + n-1, d) <= b)
            A = n;

        while (A < B) {
            C = (A+B)/2;

            if (get_sum(C + size, d) <= b) {
                A = C+1;

            } else {
                B = C;
            }

        }

        if (get_sum(size + C, d) <= b)
            A = C + 1;
        else
            A = C;

        if (A >= n || get_sum(size+A, d) <= b) {
            printf("0\n");
            continue;
        }

        printf("%lld\n", sum(A, n-1, d) - (n-A)*b);
        set(A, n-1, d, b);
    }
}