#include<bits/stdc++.h>
#define ll long long
#define int long long
#define re read()
using namespace std;
const int N=3e5+5;
const int NN=3e3+5;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
struct yhjjgt{
int jg,zs;
}yhj[1000005];
signed main(){
ios::sync_with_stdio(false);
int n=re,jq=0,j=0,s=1;
for(int i=1;i<=n;i++) {
int aa=re,jqq=re,sj=re;
if(aa==0){
j++;
yhj[j].jg=jqq;
yhj[j].zs=sj+45;
jq+=jqq;
}
else{
if(sj>yhj[s].zs)
while(sj>yhj[s].zs&&s<j) s++;
int k=0;
bool bo=0;
if(jqq<=yhj[s].jg&&sj<=yhj[s].zs){
s++;
}
else if(s==j){
jq+=jqq;
}
else{
while(jqq>yhj[s+k].jg&&s<j){
k++;
if(jqq<=yhj[s+k].jg){
s++;
bo=1;
break;
}
}
if(bo==0){
jq+=jqq;
}
}}
}
cout<<jq;
return 0;
}