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
#include <bits/stdc++.h>

int main(){
	using namespace std;
	ios::sync_with_stdio(false), cin.tie(nullptr);
	
	int K;
	cin >> K;
	
	vector<int> ile(K);
	vector<vector<int>> A(K);
	vector<vector<int64_t>> f(K);
	// ile nas potrzebuje.
	
	cin >> ile[0];
	f[0].resize(ile[0]);
	for(int i = 1;i < K;i++){
		cin >> ile[i];
		A[i].resize(ile[i]);
		for(int &x : A[i]){
			cin >> x;
		}
		f[i].assign(ile[i], 0);
	}
	
	int64_t ans = 0;
	for(int i = K-1;i >= 0;i--){
		for(int j = 0;j < ile[i];j++){
			f[i][j] = max(f[i][j], int64_t(1));
		}
		if(i > 0) {
			for(int j = 0;j < ile[i];j++){
				int x = A[i][j];
				if(x > 0) {
					f[i - 1][x - 1] += f[i][j];
				}
			}
		}
		int64_t s = 0;
		for(int j = 0;j < ile[i];j++){
			s += f[i][j];
		}
		ans = max(ans, s);
	}
	
	cout << ans << '\n';
	
	return 0;
}


//~ 4 3
//~ 3 1 1 1
//~ 4 0 0 2 0
//~ 2 3 3

//~ czemu 5 nie działa ? 
         
//~ day 1 {0, 0, 0} + od next {1,1,1}
//~ day 2 {1, 1, 1} + od next {2} (ale żeby pójść na 2 trzeba też pójść na jeden}
//~ day 3 {0, 0, 2, 0} + od next {3, 3} (ale żeby pójść na 3 trzeba pójść na 2 itd. itd)

//~ consider as graph is better, sum of f[i] in graph