Hay Weight
Time limit2sMemory limit512 MB
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 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 grass species will be sown in the field. Each species will occupy 1 are of land in total. For the -th species, a blade of grass grows by 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 . With this setting, every blade of grass taller than centimeters is cut to exactly 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 is negligibly small.
Input
The first line of input contains two integers and (), 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 integers () denoting the growth rates of the successive grass species.
The next lines contain descriptions of the mowings performed by Bajtazar. The -th mowing is described by the two integers and (, ) on the -th of these lines, meaning that on day Bajtazar cut the grass to a length of centimeters. You may assume that the mowing descriptions are given in chronological order, i.e., .
Furthermore, you may assume that Bajtazar never lets the grass in any part of the field reach a height exceeding centimeters.
Output
Output exactly lines. The -th line should contain the total weight of hay (in kilograms) obtained after the -th mowing.
Hint
The table below shows the blade heights of grass species 1, 2, 3, 4 before and after each mowing.