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
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    unsigned int n, m;
    unsigned int *a;
    unsigned long long int d, b, d_last, d_last_all, b_last;

    unsigned int i, j;
    unsigned long long int przyrost, przyrost_min, reszta, suma, *trawa;

    scanf("%u %u", &n, &m);

    unsigned int SIZE = n;

    a = calloc(SIZE, sizeof *a);
    trawa = calloc(SIZE, sizeof *trawa);

    przyrost = 0;
    przyrost_min = 1000000;
    for(i=0; i<n; i++)
    {
        scanf("%u", &a[i]);
        przyrost += a[i];
        if( przyrost_min > a[i] )
            przyrost_min = a[i];
    }

    reszta = 0;
    d_last = 0;
    d_last_all = 0;
    b_last = 0;

    for(i = 0; i < m; i++)
    {
        scanf("%llu %llu", &d, &b);

        if( (d - d_last) * przyrost_min >= b )
        {
            suma = (d - d_last) * przyrost + reszta - b * n;
            reszta = b * n;
            //printf(".");
        }
        else
        {
            suma = 0;
            reszta = 0;
            for(j = 0; j < n; j++)
            {
                trawa[j] += a[j] * (d_last - d_last_all);
                if(trawa[j] > b_last)
                {
                   trawa[j] = b_last;
                }
                trawa[j] += a[j] * (d - d_last);
                if(trawa[j] > b)
                {
                    suma += (trawa[j] - b);
                    trawa[j] = b;
                    reszta += b;
                }
                else
                {
                    reszta += trawa[j];
                }
            }
            d_last_all = d;
            //printf("_");
        }

        d_last = d;
        b_last = b;

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

    free(a);
    free(trawa);

    return 0;
}