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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
using namespace std;
bool ktoradost[500000];
int w[500000];
int t[500000];
int polozenieA[500000];
int polozenieU[500000];
int iloszzmian[500000];
int ilosczderz[500000];
int tret[500000];
int wret[500000];
int main()
{
    int n;
    int najwyzw = 0;
    int najnizt = 0;
    cin >> n;
    int nRet = n;
    while (n != 0) 
    {
        n--;
        short Nabool;
        cin >> Nabool >> w[n] >> t[n];
        if (Nabool == 1) 
        {
            ktoradost[n] = true;
            polozenieA[n] = w[n];
            polozenieU[n] = 0;
        }
        else 
        {
            ktoradost[n] = false;
            polozenieU[n] = w[n];
            polozenieA[n] = 0;
        }
        if (w[n] >= najwyzw)
        {
            najwyzw = w[n];
        }
        if (w[n] >= najnizt)
        {
            najnizt = w[n];
        }
    }
    n = nRet;
    int n2 = n;
    int tcz = 0;
    while(najwyzw!=tcz-najnizt)
    {
        while (n2 != 0) 
        {
            n2--;
            w[n2] = wret[n2];
            t[n2] = tret[n2];
        }
        while (n!=0)
        {
            n--;
            n2 = nRet;
            iloszzmian[n]++;
            if (t[n] == 0)
            {
                if (ktoradost) 
                {
                    polozenieU[n]++;
                }
                else
                {
                    polozenieA[n]++;
                }
                while (n2 != 0)
                {
                    n2--;
                    if ((polozenieU[n2] == polozenieU[n]) && (polozenieA[n2] == polozenieA[n]) && (n != n2))
                    {
                        ilosczderz[n]++;
                    }
                }
            }
            else
            {
                t[n]--;
            }
        }
        tcz++;
    }
    n = nRet;
    tcz = 0;
    int wyrzucajaca = 0;
    int beakowana = 0;
    while (najwyzw != tcz - najnizt)
    {
        tcz = 0;
        int beakowana = 0;
        n = nRet;
        while (najwyzw != tcz - najnizt) 
        {
            while (n != 0)
            {
                n--;
                n2 = nRet;
                iloszzmian[n]++;
                if ((polozenieA[n] == 0) && (polozenieU[n] == 0)) { }
                else
                {
                    /*cout << "oo";*/
                    if (t[n] == 0)
                    {
                        if (ktoradost)
                        {
                            polozenieU[n]++;
                        }
                        else
                        {
                            polozenieA[n]++;
                        }
                        while (n2 != 0)
                        {
                            n2--;
                            if ((polozenieU[n2] == polozenieU[n]) && (polozenieA[n2] == polozenieA[n]) && (n != n2))
                            {
                                beakowana = 1;
                                wyrzucajaca++;
                                if (ilosczderz[n] > ilosczderz[n2])
                                {
                                    polozenieU[n] = 0;
                                    polozenieA[n] = 0;
                                }
                                else
                                {
                                    polozenieU[n2] = 0;
                                    polozenieA[n2] = 0;
                                }
                                break;
                            }
                        }
                    }
                    else
                    {
                        t[n]--;
                    }
                }
            }
            if (beakowana == 1)
            {
                break;
            }
            tcz++;
        }
    }
    cout << wyrzucajaca;
}