#include <bits/stdc++.h>
#define ll long long
#define oi ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
ll n, way, c, num, to=0;
struct Coupon{int pr, ti;} a[1000005];
int main()
{
//freopen("xx.in", "r", stdin);
//freopen("xx.out", "w",stdout);
oi;
cin>>n;
queue <Coupon> coupons;
int pr, ti;
for(int i=1;i<=n;i++){
cin>>way>>pr>>ti;
if(way==0){
to+=pr;
coupons.push({pr, ti});
}else{
while(!coupons.empty()&&ti-coupons.front().ti>45)
coupons.pop();
}
bool us=0;
queue<Coupon>temp;
while(!coupons.empty()){
Coupon c=coupons.front();
coupons.pop();
if(!us&&c.pr>=pr)
us=1;
else temp.push(c);
}
while(!temp.empty()){
coupons.push(temp.front());
temp.pop();
}
if(!us) to+=pr;
}
cout<<to<<endl;
return 0;
}
全WA的 TOT