fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Main
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. Scanner in = new Scanner(System.in);
  10. int n = in.nextInt(), count = 0, t, m;
  11. int a[][] = new int[n + 1][2];
  12. a[count][1] = 0;
  13. a[count][0] = -1;
  14. count++;
  15. long sumr = 0;
  16. for(int i = 0; i < n; i++){
  17. t = in.nextInt();
  18. m = in.nextInt();
  19. if(m == 0){
  20. a[count][1] = a[a[t][0]][1];
  21. a[count][0] = a[a[t][0]][0];
  22. }else{
  23. a[count][1] = a[t][1] + m;
  24. a[count][0] = t;
  25. }
  26. sumr += a[count][1];
  27. count++;
  28. }
  29. System.out.println(sumr);
  30. }
  31. }
Success #stdin #stdout 0.06s 2184192KB
stdin
5
1 2
3 4
5 5
1 7
5 6
stdout
26