#include <bits/stdc++.h>
using namespace std;
long long n;
bool op;
long long m;
long long t;
long long ans;
struct Node {
long long money, times;
bool operator < (const Node & b) const {
if (b.money == money) {
return times < b.times;
}
return money < b.money;
}
};
set <Node> q;
int main() {
cin >> n;
while (n--) {
cin >> op >> m >> t;
if (op == 0) {
ans = ans + m;
q.insert({m, t});
} else {
set<Node>::iterator it = q.lower_bound({m, t - 45});
if (it == q.end()) {
ans = ans + m;
}else {
if (t - (it -> times) <= 45) {
q.erase(it);
} else {
ans = ans + m;
}
}
}
}
cout << ans << "\n";
return 0;
}