import java.io.*;
import java.util.*;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static StringTokenizer st;
    static int N;
    static Stack<Integer> visit;
    static List<Integer>[] adjs;
    static int[] type;
    static int[] nums;
    static int answer = 0;

    public static void main(String[] args) throws IOException {
        //8:23
        // 늑대 1 -> 양 1
        st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        visit = new Stack<>();
        type = new int[N+1];
        nums = new int[N+1];

        adjs = new List[N+1];
        for(int i = 1; i <= N; i++) {
            adjs[i] = new ArrayList<>();
        }

        for(int i = 2; i <= N; i++) {
            st = new StringTokenizer(br.readLine());
            String t = st.nextToken();
            int num = Integer.parseInt(st.nextToken());
            int to = Integer.parseInt(st.nextToken());
            if(t.equals("S")) {
                nums[i] = num;
            } else {
                nums[i] = -num;
            }

            adjs[to].add(i);
        }

        bw.write(dfs(1)+"");
        bw.flush();

//        for(int i = 1; i <= N; i++) {
//            System.out.println("i = " + i + " nums[i] " + nums[i]);
//        }
    }

    public static long dfs(int idx) {
        long sum = 0;
        for(int i: adjs[idx]) {
            sum += dfs(i);
        }

        sum += nums[idx];
        if(sum < 0) return 0;
        else return sum;
    }
}
//8
//S 100 1
//S 100 1
//W 100 1
//S 1000 2
//W 1000 2
//S 900 6
//S 200 6
  • 트리 탐색 LRV 순으로 가야하는 dfs
  • 타입 조심! input를 보면 10^9으로 주어지기 때문에 long으로 선언해줘야함