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

using namespace std;

typedef unsigned long long ull;
static ull a[500005];
static ull c[500005];

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);

  ull last = 0;

  for (int i = 0; i < m; ++i) {
    ull d, b;
    scanf("%llu %llu", &d, &b);
    ull delta = d - last;
    ull res = 0;
    for (int j = 0; j < n; ++j) {
      c[j] += a[j]*delta;
      if (b < c[j]) {
        res += c[j]-b;
        c[j] = b;
      }
    }
    last = d;

    printf("%llu\n", res);
  }

}