#include<iostream>
using namespace std;
struct info
{
int tools; // 0-Subway 1-Bus
int time; //0 to time
int prices;
bool used; // 0-Ain't used yet 1-have been used
};
int main()
{
info points[1000];
int n; cin>>n;
int cost=0;
int subways[1000],latest_ticket=0;
for(int i=0;i<n;i++)
{
cin>>points[i].tools>> points[i].prices>> points[i].time;
points[i].used=false; //初始化为没用过
if(points[i].tools==0) //Taking the subway
{
cost+=points[i].prices;
subways[latest_ticket]=i;
latest_ticket++;
}
else //Taking the bus
{
bool flag=false;
for(int j=0;j<latest_ticket;j++)
{
if(subways[j]!=-1)
{
if (points[i].time - points[subways[j]].time <= 45)
{
if (!points[subways[j]].used)
{
if (points[i].prices <= points[subways[j]].prices)
{
cost += 0;
//latest_ticket--;
subways[j] = -1;
flag = true;
points[subways[j]].used = true;
break;
}
}
}
}
}
if(!flag) cost+=points[i].prices; //没找到优惠券 加钱
}
}
cout<<cost<<endl;
}