#include<iostream>
#include<cstdio>
using namespace std;
struct ticket{
int z_prize,last_time,used;
}a[100010];
int top,end,cost,n;
int prize,op,time;
bool can = false;
int main(){
scanf("%d",&n);
for(int i = 0;i < n;i++){
cin>>op>>prize>>time;
if(op == 0){
cost += prize;
a[end].last_time = time + 45;
a[end++].z_prize = prize;
}
else{
while(top < end && a[top].last_time < time){
top++;
}
for(int j = top;j < end;++j){
if(a[j].z_prize >= prize && a[j].used == 0){
can = true;
a[j].used = 1;
break;
}
}
if(!can) cost += prize;
}
}
printf("%d",cost);
return 0;
}