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
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int k; LL n1;
    cin >> k >> n1;

    LL cur_tot = n1, avail_f = 0;
    int prev_n = n1;

    for (int i = 2; i <= k; ++i) {
        int ni, st_new = 0; cin >> ni;
        vector<int> ch_count(prev_n + 1, 0);

        for (int j = 0; j < ni; ++j) {
            int aij; cin >> aij;
            if (aij == 0) st_new++;
            else ch_count[aij]++;
        }

        LL spl_ned = 0, fin_hr = 0;
        for (int j = 1; j <= prev_n; ++j) {
            if (ch_count[j] > 0) spl_ned += (ch_count[j] - 1);
            else fin_hr++;
        }

        avail_f += fin_hr;
        cur_tot += spl_ned;

        if (st_new > avail_f) {
            cur_tot += (st_new - avail_f);
            avail_f = 0;
        } else {
            avail_f -= st_new;
        }

        prev_n = ni;
    }
    cout << cur_tot << endl;
}