#include<bits/stdc++.h>
using namespace std;
//优惠票信息(起始时间、价格、是否已被使用)
struct tickey{
int price,time;
bool used;
}a[100000];
// s总花费, n_ticket优惠票数量;
int n,s,n_ticket;
// 查找最早的、符合条件的优惠票,返回其下标
// 不存在返回 0
int find(int price,int time){
for(int i=max(1,n_ticket-45);i<=n_ticket;i++)
if(!a[i].used && a[i].price>=price && a[i].time+45>=time)
return i;
return 0;
}
int main(){
cin >> n;
for(int i=0;i<n;i++){
int x,y,z;
cin >> x >> y >> z;
//乘坐地铁,则记录地铁票信息,花钱
if(x==0){
s += y;
++n_ticket;
a[n_ticket].price = y;
a[n_ticket].time = z;
a[n_ticket].used = 0;
}
//若乘坐公交
else{
//查找最早、可使用的地铁票
int res = find(y,z);
//找到,更新地铁票状态
if(res) a[res].used = 1;
//未找到,花钱
else s += y;
}
}
cout << s;
return 0;
}