#include<bits/stdc++.h>
using namespace std;
int n,ans=0,price,t;
bool f;
queue<pair<int,int>>q;
int main(){
cin>>n;
while(n--){
cin>>f>>price>>t;
if(f){
while(t>q.front().first&&!q.empty())
q.pop();
pair<int,int>v=q.front();
bool flag=1;
do{
if(flag&&price<=q.front().second&&t<=q.front().first){
flag=0;
if(q.front()==v)
break;
}else
q.push(q.front());
q.pop();
}while(q.front()!=v&&!q.empty());
if(flag)
ans+=price;
}else{
ans+=price;
q.push(make_pair(t+45,price));
}
}
cout<<ans<<endl;
return 0;
}