#include<bits/stdc++.h>
using namespace std;
queue<int> a,b;//优惠票价格,优惠票有效期
int ans=0,cnt=0,qwe=0;//有多少优惠票,支付的钱 ,计数有无使用优惠
int n;
int che[100005],p[100005],t[100005];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>che[i]>>p[i]>>t[i];
if(che[i]==0)
{
a.push(p[i]);
b.push(t[i]);
ans++;
cnt+=p[i];
}
while(!b.empty()&&t[i]-b.front()>45)
{
a.pop();
b.pop();
ans--;
}
if(che[i]>0)
{
if(ans!=0)
{
qwe=0;
for(int j=1;j<=a.size();j++)
{
if(qwe==0&&a.front()>=p[i])
{
a.pop();
b.pop();
ans--;
qwe=1;
}
if(a.front()<p[i])
{
a.push(a.front());
a.pop();
b.push(b.front());
b.pop();
}
}
}
else
{
cnt+=p[i];
continue;
}
if(qwe==0)
{
cnt+=p[i];
}
}
}
cout<<cnt;
return 0;
}