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
#include<bits/stdc++.h>
using namespace std;
constexpr int N=5e5+5;

#define gc getchar_unlocked
#define pc putchar_unlocked
inline int read(){
    int x=0; char c=gc();
    while(!isdigit(c))c=gc();
    while(isdigit(c))x=x*10+c-'0',c=gc();
    return x;
}
void write(const int x){
    if(x>9)write(x/10);
    pc(x%10+'0');
}

int k,n[N],ans;
vector<int> a[N],cnt[N];

int main(){
    k=read(),n[0]=read(),a[0].resize(n[0]),cnt[0].resize(n[0]);
    for(int i=1;i<k;i++){
        n[i]=read(),a[i].resize(n[i]),cnt[i].resize(n[i]);
        for(int j=0;j<n[i];j++)a[i][j]=read()-1;
    }
    for(int i=k-1;~i;i--){
        int sum=0;
        for(int j=0;j<n[i];j++){
            sum+=cnt[i][j]=max(cnt[i][j],1);
            if(i&&~a[i][j])cnt[i-1][a[i][j]]+=cnt[i][j];
        }
        ans=max(ans,sum);
    }
    write(ans),pc('\n');
    return 0;
}