Hay Weight

Time limit2sMemory limit512 MB

Summary
After each mowing that cuts every blade above height b, report the total hay cut, given growth rates and chronological mowing days.
Level

Medium7 of 10

Topics
Sorting, Binary search, Prefix sum, Math
Solved
No attempts yet

Problem

Farmer Bajtazar has bought a field of area nn ares, where he plans to sow grass. The cut and dried grass will serve as feed for the animals raised on Bajtazar's farm.

A mixture of nn grass species will be sown in the field. Each species will occupy 1 are of land in total. For the ii-th species, a blade of grass grows by aia_i centimeters per day, regardless of weather conditions and soil fertility. It is also known that cutting one centimeter of grass over an area of one are yields exactly 1 kilogram of hay.

Bajtazar has a mower that can be set to cut grass to any height bb. With this setting, every blade of grass taller than bb centimeters is cut to exactly bb centimeters.

Bajtockie law requires that after each mowing, the amount of hay obtained from the cut grass be documented. Bajtazar faces a choice: he must either buy a scale or write a program that, based on the mowing dates and mower settings, estimates the weight of hay obtained after each mowing. The second option seemed more convenient and cheaper to him.

We assume that the grass is sown on day 0 at midnight and is always mowed at midnight. We also assume that the time needed to mow the field to any height bb is negligibly small.

Input

The first line of input contains two integers nn and mm (1≤n,m≤500 0001 \le n, m \le 500\,000), denoting Bajtazar's field area in ares (and also the number of sown grass species) and the number of mowings. The second line contains a sequence of nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1061 \le a_i \le 10^6) denoting the growth rates of the successive grass species.

The next mm lines contain descriptions of the mowings performed by Bajtazar. The ii-th mowing is described by the two integers did_i and bib_i (1≤di≤10121 \le d_i \le 10^{12}, 0≤bi≤10120 \le b_i \le 10^{12}) on the ii-th of these lines, meaning that on day did_i Bajtazar cut the grass to a length of bib_i centimeters. You may assume that the mowing descriptions are given in chronological order, i.e., d1<d2<…<dmd_1 < d_2 < \ldots < d_m.

Furthermore, you may assume that Bajtazar never lets the grass in any part of the field reach a height exceeding 101210^{12} centimeters.

Output

Output exactly mm lines. The ii-th line should contain the total weight of hay (in kilograms) obtained after the ii-th mowing.

Hint

The table below shows the blade heights of grass species 1, 2, 3, 4 before and after each mowing.

DayBefore mowingAfter mowing
11, 2, 4, 31, 1, 1, 1
22, 3, 5, 42, 2, 2, 2
33, 4, 6, 50, 0, 0, 0
41, 2, 4, 31, 2, 4, 3

Examples1

  1. Example 1

    Input
    4 4
    1 2 4 3
    1 1
    2 2
    3 0
    4 4
    
    Expected output
    6
    6
    18
    0