#include <iostream>

using namespace std;

struct Node{
	int t;
	int p;
	bool f;
}arr[999999];
int cnt;

int n, ans;


int main(){
//	freopen("transfer.in", "r", stdin);
//	freopen("transfer.out", "w", stdout);
	cin >> n;
	while(n--){
		int trans, price, t;
		cin >> trans >> price >> t;
		if(trans == 0){		//乘坐地铁 
			cnt++;
			arr[cnt].t = t;
			arr[cnt].p = price;
			arr[cnt].f = true;
			ans += price;
		}else if(trans == 1){	//乘坐公交 
			bool flag = false;
			for(int i=1; i<=cnt; i++){
				//时间小于45分钟
				//没有用过
				//价格大小 
				if(t - arr[i].t <= 45 && arr[i].f == true && price <= arr[i].p){
					arr[i].f = false;
					flag = true;
					break;
				}
			}
			if(flag == false){
				ans += price;
			}
		}
	}
	cout << ans;
//	fclose(stdin);
//	fclose(stdout);
	return 0;
}