#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int tlen=0;
struct aa { int tpye,price,time;} ticket[N];
bool cmp(aa a,aa b) { return a.time<b.time;}
int main() {
int n;
aa a[N];
scanf("%d",&n);
for(int i=0; i<n; i++) scanf("%d%d%d",&a[i].tpye,&a[i].price,&a[i].time);
sort(a,a+n,cmp);
int mr=0,head=0,pd;
for(int i=0; i<n; i++) {
if(!a[i].tpye) {
mr+=a[i].price;
ticket[++tlen]=a[i];
} else {
pd=1;
while(a[i].time-a[head].time>45||a[head].time==-46) head++;
for(int j=head; j<tlen; j++) {
if(a[i].time-a[j].time<=45&&a[i].price<=a[j].price) {
a[j].time=-46;
pd=0;
break;
}
}
if(pd) mr+=a[i].price;
}
}
cout<<mr;
return 0;
}