#include <iostream>
using namespace std;
const int MAX=114514;
struct ticket{
int typ;
int price;
int time;
}ket[MAX];
struct youhuipiao{
int pri;
int tim;
bool used=false;
}yhp[MAX];
int main(){
int n;
cin>>n;
int nn=0;
for(int e=0;e<n;e++){
cin>>ket[e].typ>>ket[e].price>>ket[e].time;
if(ket[e].typ==0){
yhp[nn].pri=ket[e].price;
yhp[nn].tim=ket[e].time+45;
nn++;
}
}
int ans=0;
int cnt=0;
for(int i=0;i<n;i++){
if(ket[i].typ==0){
ans+=ket[i].price;
}
else{
while(cnt<=nn&&(yhp[cnt].tim<ket[i].time)){
yhp[cnt].used=true;
cnt++;
}
bool iffind=false;
for(int m=cnt;m<=nn;m++){
if(yhp[m].pri>=ket[i].price&&!(yhp[m].used)){
yhp[m].used=true;
iffind=true;
break;
}
}
if(!iffind){
ans+=ket[i].price;
}
}
}
cout<<ans;
}