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
#include <iostream>
#include <vector> 
#include <algorithm> 
using namespace std;

int n;
vector <int> a, b;
vector <pair<int,int>> A,B;

int main() {
    ios_base::sync_with_stdio(0); cin.tie();
        cin >> n;
        for(int i = 0; i < n; i++){
            int r, w, t;
            cin >> r >> w >> t;
            int x = w - t;
            if(r == 1) a.push_back(x);
            else b.push_back(x);
        }

        if(a.empty() || b.empty()) {
            cout << 0;
            return 0;    
        }


        sort(a.begin(), a.end());
        sort(b.begin(), b.end());

        int cur = a[0], il = 0;
        for(int i = 0; i < a.size(); i++){
            if(cur == a[i]) il++;
            else{
                A.push_back({cur, il});
                cur = a[i];
                il = 1;
            }
        } 
        A.push_back({cur, il});
        
        cur = b[0]; il = 0;
        for(int i = 0; i < b.size(); i++){
            if(cur == b[i]) il++;
            else{
                B.push_back({cur, il});
                cur = b[i];
                il = 1;
            }
        }
        B.push_back({cur, il});

        int i = 0, j = 0;
        int ans = 0;
        while(i < A.size() && j < B.size()){
            if(A[i].first > B[j].first) j++;
            else if(B[j].first > A[i].first) i++; 
            else {
                ans += min(A[i].second, B[j].second);
                i++;
                j++;
            }
        }

        cout << ans;
   }