#include <bits/stdc++.h>
using namespace std;
int n,num=0;
struct node{
int z,p,t;
}a[1000005];
struct pack{
int p,t;
}b[1000005];
int main(){
cin >> n;
for(int i=1;i<=n;i++){
cin >> a[i].z >> a[i].p >> a[i].t;
}
int m=0,h=0;
for(int i=1;i<=n;i++){
for(int j=m-1;j>=h;j--){
if(a[i].t-45>b[j].t){
h=j+1;
break;
}
}
if(a[i].z==0){
b[m].p=a[i].p;
b[m].t=a[i].t;
m++;
num+=a[i].p;
}else{
int u=0;
for(int j=h;j<m;j++){
if(b[j].p>=a[i].p && b[j].t>=0 && u==0){
b[j].t=-1;
u++;
break;
}
}
if(u==0){
num+=a[i].p;
}
}
}
cout << num;
return 0;
}