#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN=10005;
int main()
{
int n,cost=0,price,t;
bool a;
queue<int> qp;
queue<int> tempp;
queue<int> tempt;
queue<int> qt;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a>>price>>t;
if(a)
{
if(i==1)
cost+=price;
else
{
while(!qt.empty())
{
if(t-qt.front()>45)
{
qt.pop();
qp.pop();
}
else break;
}
if(qt.empty()) cost+=price;
else
{
while(!qp.empty())
{
if(qp.front()<price)
{
int temp0,temp1;
temp0=qp.front();
temp1=qt.front();
qp.pop();
qt.pop();
tempp.push(temp0);
tempt.push(temp1);
}
else break;
}
if(qp.empty()) cost+=price;
else
{
qp.pop();
qt.pop();
while(!qp.empty())
{
int temp0,temp1;
temp0=qp.front();
temp1=qt.front();
qp.pop();
qt.pop();
tempp.push(temp0);
tempt.push(temp1);
}
qp=tempp;
qt=tempt;
}
}
}
}
else
{
cost+=price;
qp.push(price);
qt.push(t);
}
}
cout<<cost;
}