#include<iostream>
using namespace std;
int n,cnt,front = 1,money;
bool how_go,flag;
long long t,sumny,last;
struct Edeg{
long long ti;
long long ms;
}cheaps[100005];
int main(){
cin >> n;
for(int i=1;i<=n;i++){
cin >> how_go >> money >> t;
if(!how_go){
sumny += money;
cheaps[++cnt].ti = t;
cheaps[cnt].ms = money;
}else{
flag = false;
for(int j=front;j<=cnt;j++){
if(cheaps[j].ti == -1){
if(front == j-1){
front++;
}
continue;
}
if((t-cheaps[j].ti) <= 45 && cheaps[j].ms >= money){
cheaps[j].ti = -1;
flag = true;
break;
}else{
if((t-cheaps[j].ti) > 45){
front++;
}
}
}
if(!flag){
sumny+=money;
}
}
}
cout << sumny;
return 0;
}