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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int x, y;
    cin >> x >> y;

    vector<vector<pair<int,int>>> spotkania;
    spotkania.resize(x);

    for(int i = 0; i < y; i++){
        spotkania[0].push_back({0, 0});
    }

    for(int i = 1; i < x; i++){
        int z;
        cin >> z;
        for(int r = 0; r < z; r++){
            int w;
            cin >> w;
            spotkania[i].push_back({w, 0});
        }
    }

    for(int i = x-1; i > 0; i--){  
        for(int z = 0; z < (int)spotkania[i].size(); z++){
            if(spotkania[i][z].first != 0){
                int poprzednik = spotkania[i][z].first - 1;
                spotkania[i-1][poprzednik].second += max(spotkania[i][z].second, 1);
            }
        }
    }

    long long wynik = 0;
    long long pula = 0;  

    for(int d = 0; d < x; d++){
        long long nowe_potrzeby = 0;   
        long long zwrot = 0;           
        
        for(int z = 0; z < (int)spotkania[d].size(); z++){
            if(spotkania[d][z].first == 0){
                nowe_potrzeby += max(spotkania[d][z].second, 1);
            }
            if(spotkania[d][z].second == 0){
                zwrot++;
            }
        }

        long long brakuje = max(0LL, nowe_potrzeby - pula);
        wynik += brakuje;
        pula = max(0LL, pula - nowe_potrzeby) + zwrot;
    }

    cout << wynik << endl;
    return 0;
}