#include <iostream>
#include <deque>
class railway{public:int price,t;};
int main(){
std::ios::sync_with_stdio(0);
std::cin.tie(0),std::cout.tie(0);
bool flag;
int n,w,price,t,ans = 0;
std::deque <railway> Q;
std::cin >> n;
while (n--){
flag = 0;
std::cin >> w >> price >> t;
while (!Q.empty() && t - 45 > Q.front().t) Q.pop_front();
if (w == 0) Q.push_back((railway){price,t}),ans += price;
else{
for (auto it = Q.begin();it != Q.end();it++)
if ((*it).price >= price){
flag = 1;
break;
}
if (!flag) ans += price;
}
}
std::cout << ans << '\n';
return 0;
}