#include<bits/stdc++.h>
using namespace std;
struct sub{
long long time, price, used = 0;
}bus[100005];
long long n, h, t, cost;
int main() {
scanf("%lld", &n);
for(int i = 0;i < n;i++) {
long long any, ti, pri;
scanf("%lld%lld%lld", &any, &pri, &ti);
if(any == 0) {
cost += pri;
bus[t].time = ti + 45;
bus[t].price = pri;
bus[t++].used = 0;
} else if(any == 1){
while(h < t && bus[h].time < ti)h++;
bool found = false;
for(int j = h;j < t;j++) {
if(bus[j].price >= pri && bus[j].used == 0 && bus[h].time >= ti) {
bus[j].used == 1;
found = true;
break;
}
}
if(!found)cost += pri;
}
}
printf("%lld", cost);
return 0;
}