#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n,flag,p[maxn],t[maxn];
int cnt,cost;
queue <int> yhq,jq;
int main(){
cin>>n;
for(int i = 1;i <= n;i++){
cin>>flag>>p[i]>>t[i];
if(flag == 0){
yhq.push(t[i]);
jq.push(p[i]);
cost += p[i];
}
if(flag == 1){
while(t[i] - yhq.front() > 45 and !yhq.empty()){
// cout<<" delete1 "<<yhq.front()<<endl;
yhq.pop();
jq.pop();
}
if(!yhq.empty()){
while(jq.front() < p[i] and !yhq.empty()){
// cout<<" delete2 "<<yhq.front()<<endl;
yhq.pop();
jq.pop();
}
if(!yhq.empty()){
// cout<<" delete3 "<<yhq.front()<<endl;
yhq.pop();
jq.pop();
continue;
}
}
cost += p[i];
}
// cout<<" zhuyi "<<cost<<endl;
}
// cout<<endl;
cout<<cost;
return 0;
}
/*
100
0 85 6
1 61 35
0 117 44
0 114 45
0 182 47
1 19 48
1 52 49
0 265 838
1 408 866
0 946 867
1 86 872
1 231 875
1 324 879
1 357 880
1 276 882
0 63 916
0 357 921
1 24 936
0 221 938
1 102 942
1 81 949
0 226 1555
0 399 1876
0 237 1907
0 157 1917
1 234 1919
0 183 2820
1 172 2841
0 618 2847
1 158 2848
1 106 2849
1 80 2851
1 122 2852
1 7 2853
1 328 2854
1 5 2855
1 152 2856
1 273 2857
1 129 2858
1 249 2859
1 333 2860
0 828 2861
1 64 2862
1 217 2863
1 269 2864
0 391 2888
0 380 3319
1 386 3332
1 620 3334
0 333 3341
0 300 3365
1 339 3373
0 624 3374
1 546 3375
1 420 3376
1 168 3377
1 319 3378
0 819 3379
1 509 3380
1 453 3381
1 54 3382
0 89 3383
1 336 3384
1 83 3385
0 85 4260
1 156 4293
1 2 4294
1 22 4298
1 126 4299
1 67 4300
1 63 4301
1 72 4302
1 87 4303
0 633 4304
0 150 4336
1 132 4367
0 248 4368
0 624 4369
0 666 4370
1 212 4371
1 49 4372
1 245 4373
1 155 4374
1 239 4375
1 194 4376
1 255 4377
1 120 4378
1 63 4379
1 158 4380
0 62 4716
1 73 4734
0 834 4744
1 19 4751
0 570 4754
1 6 4756
0 652 4757
1 49 4758
1 44 4759
1 79 4760
0 345 4798
*/
// ans = 21240