#include<cstdio>
const int MAXN=100000+5;
int n,op,price,t,ans,h,tail;
struct y{
int price,t,used;
};
y a[MAXN];
int main(){
scanf("%d",&n);
while(n--){
scanf("%d %d %d",&op,&price,&t);
if(op==0){
ans+=price;
a[tail].price=price;
a[tail].t=t+45;
tail++;
}else{
while(h<tail&&a[h].t<t)
h++;
bool flag=1;
for(int i=h;i<tail;i++){
if(a[i].price>price&&a[i].used==0){
flag=0;
a[i].used=1;
break;
}
}
if(flag){
ans+=price;
continue;
}
}
}
printf("%d",ans);
}