#include<bits/stdc++.h>
using namespace std;
struct piao{
int jiage,shijian;
};
deque<piao>q;
int lx,jg,sj,money,n;
int main()
{
cin>>n;
while(n--)
{
cin>>lx>>jg>>sj;
money+=jg;
if(lx==0)
{
piao x;
x.jiage=jg;
x.shijian=sj;
q.push_back(x);
}
else
{
while(q.size()>0 && sj-q.front().shijian>45)
{
q.pop_front();
}
for(int i=1;i<=q.size();i++)
{
if(q[i].jiage>=jg )
{
money-=jg;
q[i].shijian=-1;
break;
}
}
}
}
cout<<money;
return 0;
}