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
#include <cstdio>
#include <cstdlib>
#include <cstring>

static inline unsigned *new_day(unsigned n)
{
	unsigned *day = (unsigned *) calloc(n + 1, sizeof(*day));
	day[0] = n;
	return day;
}

static unsigned long long buf1[500001];
static unsigned long long buf2[500001];

int main(void)
{
	unsigned k;
	scanf("%u", &k);
	unsigned **d = (unsigned **) calloc(k, sizeof(unsigned *));
	for (unsigned i = 0; i < k; i++) {
		unsigned n;
		scanf("%u", &n);
		d[i] = new_day(n);
		if (i)
			for (unsigned j = 1; j <= n; j++)
				scanf("%u", d[i] + j);
	}
	unsigned long long *p = buf1, *q = buf2, result = 0;
	for (unsigned i = k; i > 0; ) {
		i--;
		const unsigned *dp = d[i];
		const unsigned n = *dp++;
		if (i == k - 1) {
			memset(q + 1, 0, sizeof(*q) * n);
		} else {
			unsigned long long *t = p;
			p = q;
			q = t;
		}
		if (i > 0)
			memset(p + 1, 0, sizeof(*p) * d[i - 1][0]);
#if DEBUG
		fprintf(stderr, "[%u]", i + 1);
		for (unsigned j = 1; j <= n; j++)
			fprintf(stderr, " %llu", q[j]);
#endif
		unsigned long long r = 0;
		for (unsigned j = 1; j <= n; j++, dp++) {
			unsigned long long x = q[j] ?: 1;
			r += x;
			if (i > 0 && *dp)
				p[*dp] += x;
		}
#if DEBUG
		fprintf(stderr, ": %llu > %llu\n", r, result);
#endif
		if (result < r)
			result = r;
	}
	printf("%llu\n", result);
	return 0;
}