#include <bits/stdc++.h>
using namespace std;
 
struct go_log{
    int tool;
    int price;
    int time;
    go_log* next;
};
 
int main(){
    int n = 0;
    cin >> n;
     
    go_log* gl_head = NULL;
    go_log* gl_tail = NULL;
    
    int price_min = 0;
     
    for(int i = 0; i < n; i++){
        go_log gl;
        cin >> gl.tool >> gl.price >> gl.time;
         
        //remove > 45 mintue
        while(gl_head != NULL){
           if((gl.time - gl_head->time) <= 45){
                break;  
           } 
           gl_head = gl_head->next;
        }
        if(gl_head == NULL){
            gl_tail = NULL;
        }
 
        //subway
        if(gl.tool == 0){
            go_log* node = new go_log;
            node->tool  = gl.tool;
            node->price = gl.price;
            node->time  = gl.time;
            node->next  = NULL;
            
            if(gl_head == NULL){
                gl_head = node;
                gl_tail = node;
            }else{
                gl_tail->next = node;
                gl_tail = node;
            }
        }
         
        //bus
        if(gl.tool == 1){
            //has ticket in queue ?            
            for(go_log* p = gl_head; p != NULL; p = p->next){
                if(p->price >= gl.price){
                    gl.price = 0;
                    p->price = -1; //it is not use
                    break;
                }  
            }
        }
         
        price_min = price_min + gl.price;      
        //printf("[%d][%d][%d]\n", i + 1, gl.tool, gl.price);
    }
     
    cout << price_min << endl;
    return 0;
}