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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <cstdlib>
#include <chrono>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cassert>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>

struct TestData;
void solve(TestData& t);
void io_solve(TestData& t, FILE* input, FILE* output);
void test();

#if _WIN32
#	define DO_TESTS 1
#	if DO_TESTS
#		define DO_LOGS 1
#	else
#		define DO_LOGS 0
#	endif
#else
#	define DO_LOGS 0
#	define DO_TESTS 0
#endif

#if DO_LOGS
auto time_start = std::chrono::high_resolution_clock::now();

#	define DEB(...) do { \
	auto duration_from_start = std::chrono::high_resolution_clock::now() - time_start;	\
	auto micros = std::chrono::duration_cast<std::chrono::microseconds>(duration_from_start).count(); \
	fprintf(stderr, "# %lld.%06lld ", micros/1000000, micros%1000000); \
	fprintf(stderr, __VA_ARGS__); \
	fputc('\n', stderr); \
} while (0)
#else
#	define DEB(...) do {} while (0);
#endif

struct Meet {
	int prev;
	int agents;
};

struct TestData {
	std::vector<std::vector<Meet>> by_days;

	int64_t result;

	TestData() {}
};

int main() {
#if DO_TESTS
	test();
#else
	FILE* input = stdin;
	FILE* output = stdout;

#if _WIN32
	const char *file = nullptr;
	//file = "in/r3d-x_1_tak.txt";
	if (file != nullptr) input = fopen(file, "r");
	if (input == NULL) {
		input = stdin;
	} else {
# 	  if 0
		std::string out_file = file;
		out_file += "_out.log";
		output = fopen(out_file.c_str(), "w");
#	  endif
	}
#endif
	TestData t;

	io_solve(t, input, output);
#endif
	return 0;
}

void solve(TestData& t);

#if DO_TESTS

void test_files() {
	std::vector<std::string> files = {
		"in/pdf-0.in",
		"in/my-1.in",
		"in/my-2.in"
	};
	for (int i = 0; i < files.size(); ++i) {
		DEB("test %d %s", i, files[i].c_str());
		FILE* file = fopen(files[i].c_str(), "r");
		if (file == nullptr) {
			DEB("can not open %s", files[i].c_str());
			return;
		}
		//FILE* outfile = fopen("out.out", "w");
		FILE* outfile = stdout;

		TestData t;
		io_solve(t, file, outfile);
		fclose(file);
		if (outfile != stdout) fclose(outfile);

		std::string check_out = files[i];
		check_out[check_out.size()-2] = 'o';
		check_out[check_out.size()-1] = 'u';
		check_out += 't';

		FILE* expect_file = fopen(check_out.c_str(), "r");
		if (expect_file == nullptr) {
			DEB("test %d. can not open %s\n", i, check_out.c_str());
		} else {
			DEB("test %d. Expects", i);
			char buf[8*1024];
			for (int lines = 0; lines < 3; ++lines) {
				fgets(buf, sizeof(buf) -1, expect_file);
				buf[sizeof(buf) -1] = 0;
				char* end = strchr(buf, '\n');
				if (end != nullptr) *end = '\0';
				DEB("%s", buf);
			}
			fclose(expect_file);
		}
	}
	DEB("test_files done %d", (int) files.size());
}

void test() {
#	if 0
	{
		FILE* test_file = fopen("in/my-2.in", "w");
		int days = 500000;
		fprintf(test_file, "%d 0\n", days);
		int i = 2;
		for (; i < days/2; ++i) {
			fprintf(test_file, "0\n");
		}
		fprintf(test_file, "1 0\n");
		for (; i < days; ++i) {
			fprintf(test_file, "2 1 0\n");
		}
		fclose(test_file);
	}
#	endif

	test_files();

	/*
	std::vector<TestData> tests;
	for(int i = 0; i < tests.size(); ++i)
	{
		auto& t = tests[i];
		DEB("test %d %d", i, t.m);

		solve(t);

		if (t.expected != t.result) {
			DEB("test %d: t.expected %d != %d t.result", i, t.expected, t.result);
			continue;
		}

		DEB("test %d done %d", i, t.result);
	}*/
}
#endif

void io_solve(TestData& t, FILE *input, FILE *output) {
	//DEB("io_solve");
	int days;
	int meets;

	fscanf(input, "%d %d\n", &days, &meets);
	t.by_days.resize(days);
	t.by_days[0].resize(meets);
	for (int d = 1; d < days; ++d) {
		fscanf(input, "%d ", &meets);
		auto& day_meets = t.by_days[d];
		day_meets.resize(meets);
		
		for (int m = 0; m < meets; ++m) {
			fscanf(input, "%d", &day_meets[m]);
		}
	}

	solve(t);

	fprintf(output, "%lld\n", t.result);
}

void solve(TestData& t) {
	int free_agents = 0;

	for (int d = t.by_days.size() - 1; d >= 1; --d) {
		auto& day_meets = t.by_days[d];
		for (int m = day_meets.size() - 1; m >= 0; --m) {
			Meet& meet = day_meets[m]; 
			if (meet.agents == 0) {
				//no one was scheduled here - get free agent or reserve one
				meet.agents = 1;
				if (free_agents > 0)
					--free_agents;
			}

			if (meet.prev == 0) {
				free_agents += meet.agents;
			} else {
				t.by_days[d-1][meet.prev-1].agents += meet.agents;
			}
		}
	}

	auto& day_meets = t.by_days[0];
	for (int m = day_meets.size() - 1; m >= 0; --m) {
		if (day_meets[m].agents == 0)
			day_meets[m].agents += 1;
		free_agents += day_meets[m].agents;
	}

	t.result = free_agents;
}