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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#include <bits/stdc++.h>

using namespace std;

bool check(int tab[], int n) {
    for (int i = 0; i < n; i++)
        if (tab[i] != 0)
            return true;
    return false;
}

int main() {
    int n;
    cin >> n;
    vector<pair<int, int>> tab1;
    vector<pair<int, int>> tab2;
    for (int i = 0; i < n; i++) {
        int r, w, t;
        cin >> r >> w >> t;
        if (r == 1)
            tab1.push_back(make_pair(w, t));
        else
            tab2.push_back(make_pair(w, t));
    }
    int wypadki1[tab1.size()];
    int wypadki2[tab2.size()];
    int wynik = 0;

    for (int i = 0; i < tab1.size(); i++) {
        wypadki1[i] = 0;
    }
    for (int i = 0; i < tab2.size(); i++)
        wypadki2[i] = 0;

    do {
        vector<int> lista1[tab1.size()];
        vector<int> lista2[tab2.size()];
        for (int i = 0; i < tab1.size(); i++) {
            int a = tab1[i].first, b = tab1[i].second;
            for (int j = 0; j < tab2.size(); j++) {
                if (a + tab2[j].second == b + tab2[j].first) {
                    wypadki1[i]++;
                    wypadki2[j]++;
                    lista1[i].push_back(j);
                    lista2[j].push_back(i);
                }
            }
        }
        for (int i = 0; i < tab2.size(); i++) {
            int a = tab2[i].first, b = tab2[i].second;
            for (int j = 0; j < tab1.size(); j++) {
                if (a + tab1[j].second == b + tab1[j].first) {
                    wypadki2[i]++;
                    wypadki1[j]++;
                    lista2[i].push_back(j);
                    lista1[j].push_back(i);
                }
            }
        }
        int maks = 0;
        int r = 0;
        int x = 0;
        for(int i = 0; i < tab1.size(); i++) {
            if(wypadki1[i] > maks)
            {
                maks = wypadki1[i];
                x = i;
                r = 1;
            }
            if(wypadki2[i] > maks)
            {
                maks = wypadki2[i];
                x = i;
                r = 2;
            }
        }
        //cout << maks << " " << r << " " << x << endl;
        if(r == 1)
        {
            int a;
            while(!lista1[x].empty())
            {
                a = lista1[x].back();
                lista1[x].pop_back();
                wypadki2[a]--;
            }
        }
        if(r == 2)
        {
            int a;
            while(!lista2[x].empty())
            {
                a = lista2[x].back();
                lista2[x].pop_back();
                wypadki1[a]--;
            }
        }
        wynik++;
    } while(check(wypadki1, tab1.size()) && check(wypadki2, tab2.size()));
    cout << wynik << endl;
    return 0;
}