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
83
84
85
86
87
88
#include <iostream>
#include <cstdlib>

using namespace std;

long long sum_growth_rate = 0;
long long grass_mass = 0;
long long mowing_grass_mass = 0;
long long growth_rate = 0;
long long current_mowing_day = 0;
long long last_mowing_day = 0;
long long mowing_length = 0;
int nr_of_areas;
int nr_of_mowing;
long long should_remain_mowing_grass_mass;
long long current_grass_growth_mass;
long long rest = 0;

int min_growth_rates = -1;
long long last_mowing_length = 0;
long long period = 0;
bool update_to_last_mowing_langth = false;

int main(int argc, char *argv[])
{
    cin >> nr_of_areas;
    cin >> nr_of_mowing;

    unsigned int growth_rates[nr_of_areas];
    unsigned int current_grass_lengts[nr_of_areas];

    for (unsigned int i=0; i<nr_of_areas; i++) {
         cin >> growth_rates[i];
         current_grass_lengts[i] = 0;
         sum_growth_rate += growth_rates[i];

        if (growth_rates[i] < min_growth_rates || min_growth_rates == -1) {
                min_growth_rates = growth_rates[i];
        }
    }

    for (unsigned int i=0; i<nr_of_mowing; i++) {
         cin >> current_mowing_day;
         cin >> mowing_length;

         period = current_mowing_day - last_mowing_day;
         should_remain_mowing_grass_mass = nr_of_areas * mowing_length;

         if (mowing_length <= (period * min_growth_rates)) {
                update_to_last_mowing_langth = true;

                current_grass_growth_mass = period * sum_growth_rate;
                grass_mass += current_grass_growth_mass;
                mowing_grass_mass = grass_mass - should_remain_mowing_grass_mass;
                grass_mass -= mowing_grass_mass;
         } else {
                 mowing_grass_mass = 0;
                 grass_mass = 0;
                 for(unsigned int j=0; j < nr_of_areas; j++) {

                    if (update_to_last_mowing_langth) {
                        current_grass_lengts[j] = last_mowing_length + growth_rates[j] * (period);
                    } else {
                        current_grass_lengts[j] += growth_rates[j] * (period);
                    }

                    rest = current_grass_lengts[j] - mowing_length;

                    if (rest >= 0) {
                        current_grass_lengts[j] -= rest;
                        mowing_grass_mass += rest;
                    }

                    grass_mass += current_grass_lengts[j];
                 }

                 update_to_last_mowing_langth = false;
        }

         last_mowing_day = current_mowing_day;
         last_mowing_length = mowing_length;

         cout << mowing_grass_mass << "\n";
    }

    return EXIT_SUCCESS;
}