#include <iostream>
using namespace std;
int n,f,h,t,ans,price[100005],tm[100005];
struct node
{
int pr,time;
bool flag;
}q[100005];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>f>>price[i]>>tm[i];
if(f==0)
{
ans+=price[i];
t++;
q[t].pr=price[i];
q[t].time=tm[i];
q[t].flag=false;
}
else
{
int j;
if(tm[i]>45)
{
j=t;
while(q[j].time+45>tm[i])j--;
h=j;
}
else h=1;
while(h<=t)
{
if(price[i]<=q[h].pr && tm[i]<=q[h].time+45 && q[h].flag==false)
{
q[h].flag=true;
break;
}
else h++;
}
if(h>t)ans+=price[i];
}
}
cout<<ans<<endl;
return 0;
}