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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <stdlib.h>

typedef struct field
{
    struct field *first;		/* jeżeli NULL, to lista jest błędna lub pusta */
    struct field *last;			/* jeżeli NULL, to lista jest błędna lub pusta i first == NULL */
    struct field *next;			/* jeżeli NULL, to jest to ostatni element listy */
    unsigned int speed;			/* wysokość, o którą wzrasta trawa w ciągu doby */
    unsigned long long int height;	/* aktualna wysokość trawy */
}
Field;

Field *field_add(Field *field, unsigned int speed)
{
    /**
     * zwraca wskaźnik na pierwszy element listy pól
     * dodaje na koniec listy field nowe pole o danych parametrach
     */

    Field *new = (Field *) malloc(sizeof(Field));

    if(new == NULL)
	return NULL;

    if(field->first == NULL)	/* jeżeli lista pusta */
    {
	field->first	= field;	/* to ustawienie nowej pozycji na początku */
	field->last	= field;
	field->next	= NULL;
	field->speed	= speed;
	//field->height	= (unsigned long long int) speed;
	field->height	= 0;

	return field;
    }

    /* ustawienie nowej pozycji na końcu listy */
    field->last->next = new;
    field->last = new;

    new->first	= field->first;
    new->last	= new;
    new->next	= NULL;
    new->speed	= speed;
    //new->height	= (unsigned long long int) speed;
    new->height	= 0;

    //printf("Dodawanie pola s=%d\n", new->speed);

    return field->first;
}

Field *fields_cut(Field *field, unsigned long long int days, unsigned long long int level)
{
    /**
     * zwraca wskaźnik na początek listy
     * przycina wszystkie pola do level
     * wyświetla sumę długości skoszonych traw
     */
    unsigned long long int sum = 0;

    while(1)
    {
	//printf("Badanie pola h=%lld, v=%d, d=%lld, t=%lld\n", field->height, field->speed, level, days);
	field->height += (unsigned long long int) field->speed * days;

	if(field->height > level)
	{
	    sum += field->height - level;
	    field->height = level;
	}
	else
	{
	    if(field->height > 1e12)
	    {
		sum += field->height - 1e12;
		field->height = 1e12;
	    }
	}

	//field->height += (unsigned long long int) field->speed;

	if(field->next == NULL)
	    break;

	field = field->next;
    }

    printf("%lld", sum);

    return field->first;
}

int main(void)
{
    char vbuf[524288];
    setvbuf(stdout, vbuf, _IOFBF, sizeof(vbuf));

    unsigned int fields			= 0;	/* liczba pól */
    unsigned int cut_amount		= 0;	/* liczba skoszeń */
    unsigned int c			= 0;
    unsigned int buffer			= 0;
    unsigned long long int llbuffer	= 0;

    Field *list	= (Field *) malloc(sizeof(Field));
    if(list == NULL)
	return 1;

    list->first	= NULL;
    list->next	= NULL;

    scanf("%d %d", &fields, &cut_amount);

    while(c < fields)
    {
	scanf("%d", &buffer);
	
	list = field_add(list, buffer);
	if(list == NULL)
	    return 1;

	++ c;
    }

    unsigned long long int date		= 0;
    unsigned long long int date_last	= 0;
    c					= 0;

    while(c < cut_amount)
    {
	scanf("%lld %lld", &date, &llbuffer);

	list = fields_cut(list, date-date_last, llbuffer);

	date_last = date;
	++ c;

	if(c < cut_amount)
	    printf("\n");
    }

    return 0;
}