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
import java.util.*;

public class sia
{
    public static void main(String[] argv)
    {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[]v = new int[n];
        for(int i = 0; i < n; ++i)
        {
            v[i] = scanner.nextInt();
        }
        long[] height = new long[v.length];
        long lastDay = 0;
        long daysDifference;
        long gathered;
        long day;
        long cut;


        for( int j = 0; j < m; ++j )
        {
            day = scanner.nextLong();
            cut = scanner.nextLong();
            gathered = 0;
            daysDifference = day - lastDay;
            lastDay = day;

            for(int i=0; i<height.length; ++i)
            {
                long newHeight = height[i] + daysDifference * v[i];
                if( newHeight > cut)
                {
                    gathered += newHeight - cut;
                    newHeight = cut;
                }
                height[i] = newHeight;
            }
            System.out.println(gathered);
        }
    }
}