#include <iostream>
using namespace std;
struct Ticket{
long long time1,price1,used1;
}t[1000000];
int head=0,tail=1000000,cnt,n;
int main(){
cin>>n;
int op,price2,time2;
for(int i = 0;i<n;i++){
cin>>op>>price2>>time2;
if(op==0){
cnt+=price2;
t[tail].time1 = time2 + 45;
t[tail++].price1 = price2;
}
bool found=false;
if(op==1){
while (head < tail && t[head].time1 < time2) {
head++;
}
for(int j=head;j<tail;j++){
if(t[j].price1>=price2 && t[j].used1==0){
found=true;
t[j].used1=1;
break;
}
}
if(found==false){
cnt+=price2;
}
}
cout<<cnt;
}
return 0;
}