#include <iostream>
#include <queue>
using namespace std;
struct yh { long long pc, tm; };
int main ( )
{
queue <yh> tkt;
yh tkto;
long long n, pc, tm, ans = 0;
bool bs, f;
cin >> n;
while(n--) {
f = true;
cin >> bs >> pc >> tm;
tkto.pc = pc;
tkto.tm = tm;
if (!bs) {
ans += pc;
tkt.push (tkto);
continue;
}
else {
while (!tkt.empty ( )) {
if ((tm - tkt.front ( ).tm <= 45) && (pc <= tkt.front ( ).pc)) {
tkt.pop ( );
f = false;
break;
}
else if ((tm - tkt.front ( ).tm > 45) || (pc > tkt.front ( ).pc)) {
if (!(tm - tkt.front ( ).tm <= 45)) {
tkt.pop ( );
}
}
}
if (f)ans += pc;
}
}
cout << ans;
}