#include <bits/stdc++.h>
using namespace std;
struct Trans {
int t;
int price;
int time;
};
Trans a[100000];
int n,ans, select = -1;
int main() {
scanf("%d",&n);
for(int i=0; i<n; i++) {
cin>>a[i].t>>a[i].price>>a[i].time;
if(a[i].t==0) {
ans+=a[i].price;
} else {
select = -1;
for(int j=i-1; j>=0; j--) {
if(a[i].time-a[j].time<=45) {
if(a[j].t==0 && a[j].price>=a[i].price) {
select = j;
}
} else {
if(select != -1) {
a[select].price = 0;
} else {
ans += a[i].price;
}
break;
}
}
}
}
cout<<ans;
return 0;
}
?