#include<iostream>
#include<stdio.h>
using namespace std;
int n,cost;
struct sub
{
sub *back;
sub *next;
int t;
int price;
};
sub *head,*p,*r;
int main()
{
freopen("P5661_1.in","r",stdin);
freopen("P5661_1.out","w",stdout);
cin>>n;
head=new sub;
head->back=NULL;
r=head;
for(int i=0;i<n;i++)
{
int tpe;
cin>>tpe;
if(tpe==0)
{
p=new sub;
cin>>p->price>>p->t;
r->next=p;
p->back=r;
p->next=NULL;
r=p;
cost+=p->price;
if(p->t-(head->next->t)>45)
{
head=head->next;
head->back=NULL;
}
}
else
{
int bus_price,bus_t;
bool hpay=true;
cin>>bus_price>>bus_t;
if(head==r)
{
cost+=bus_price;
}
else
{
p=head->next;
while(p->next!=NULL)
{
if(bus_t-(p->t)<=45)
{
if(p->price>=bus_price)
{
sub *nx=p->next,*bc=p->back;
p->back->next=nx;
p->next->back=bc;
hpay=false;
break;
}
}
else
{
head=p->back;
head->back=NULL;
}
p=p->next;
}
}
if(hpay)
{
if(bus_t-(p->t)<=45)
{
if(p->price>=bus_price)
{
r=p->back;
r->next=NULL;
hpay=false;
}
}
else
{
head=p->back;
head->back=NULL;
}
if(hpay)
{
cost+=bus_price;
}
}
}
}
cout<<cost;
fclose(stdin);
fclose(stdout);
return 0;
}