#include<bits/stdc++.h>
using namespace std;
int n,ans;
int jtgj,price,timee,t[100005],p[100005],a;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>jtgj>>price>>timee;
if(jtgj==0){
t[i]=timee;
p[i]=price;
ans+=price;
}else{
sort(t+1,t+n+1);
sort(p+1,p+n+1);
for(int j=1;j<=n;j++){
if(t[j]!=0&&p[j]!=0){
if(timee-t[j]<=45){
if(price<=p[j]){
t[j]=0;
a++;
break;
}else{
t[j]=0;
p[j]=0;
break;
}
}else{
t[j]=0;
p[j]=0;
break;
}
}
}
if(a==0){
ans+=price;
}
a=0;
}
}
cout<<ans;
return 0;
}