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

using namespace std;

#define endl "\n"

#define SIZE 500500

void solve() {
  int count = 0;
  int prev_count = 0;
  vector<int> parent_day(SIZE, 0);
  vector<int> is_end(SIZE, true);
  vector<int> levels(1);  //dummy

  int k;
  cin >> k;

  for (int day = 1; day <= k; day++) {
    int n;
    cin >> n;

    if(day == 1) {
      for(int j = 1; j <= n; j++) {
        parent_day[j] = 1;
      }
    } else {
      for(int j = 1; j<= n; j++) {
        int prev;
        cin >> prev;

        if(prev == 0)
          parent_day[count + j] = day;
        else {
          parent_day[count + j] = parent_day[prev_count + prev];
          is_end[prev_count + prev] = false;
        }
      }
    }

    prev_count = count;
    count += n;
    levels.push_back(count);
  }

  // in every day - how many needed , how many can be reused
  vector<pair<int, int>> balance(k+1);
  balance[0] = {0, 0};

  int day = 1;

  for(int i = 1; i <= count; i++) {
    if(is_end[i]) {
      balance[day + 1].second ++;
      balance[parent_day[i]].first ++;
    }
    if(i == levels[day]) day++;
  }

  int ans = 0;

  for(day = 1; day <= k; day++) {
    balance[day].second += balance[day-1].second;
    int taken = min(balance[day].first , balance[day].second);
    ans += balance[day].first - taken;
    balance[day].second -= taken;
  }

  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1;
  //cin >> t;
  while (t--) {
    solve();
  }
}