#include <bits/stdc++.h>
using namespace std;
struct clc{
int price,ti;
};
queue <clc> coupons;
int main()
{
int n,tot=0;
cin>>n;
for(int i=0;i<n;i++)
{
int op,Price,time;
cin>>op>>Price>>time;
if(op==0)
{
clc coupon;
coupon.price=Price;
coupon.ti=time;
coupons.push(coupon);
tot+=Price;
}
if(op==1)
{
bool used=false;
for(int j=0;j<coupons.size();j++)
{
clc tmp=coupons.front();
if(Price<=tmp.price&&tmp.ti-time<=45)
{
coupons.pop();
used=true;
break;
}
if(tmp.ti-time>45) coupons.pop();
}
if(used) continue;
else tot+=Price;
}
}
cout<<tot;
}