#include<bits/stdc++.h>
using namespace std;
struct node {
int time,price,used;
};
node ticket[100005];
int cnt=1,ans=0,n;
int tran,cost,t,start=1;
int main() {
scanf("%d",&n);
for(int i=1; i<=n; ++i) {
scanf("%d%d%d",&tran,&cost,&t);
if(tran==0) {
ticket[cnt].price=cost;
ticket[cnt].time=t;
ticket[cnt].used=0;
++cnt;
ans+=cost;
} else {
for(int j=start; j<cnt; ++j) {
if(t-ticket[j].time>45)
start=j;
else {
if(ticket[j].price>=cost&&!ticket[j].used) {
cost=0,ticket[j].used=1;
break;
}
}
}
ans+=cost;
}
}
printf("%d",ans);
return 0;
}