fork download
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.IO;
  6.  
  7. class Myon
  8. {
  9. public Myon() { }
  10. public static int Main()
  11. {
  12. new Myon().calc();
  13. return 0;
  14. }
  15.  
  16. bool[] used;
  17. int ret;
  18. int[] L, S;
  19. List<int>[] rev;
  20.  
  21. void calc()
  22. {
  23. Scanner cin = new Scanner();
  24. int n = cin.nextInt();
  25. L = new int[n];
  26. S = new int[n];
  27.  
  28. for (int i = 0; i < n; i++)
  29. {
  30. L[i] = cin.nextInt();
  31. S[i] = cin.nextInt() - 1;
  32. }
  33.  
  34. int[] innum = new int[n];
  35. rev = new List<int>[n];
  36.  
  37. for (int i = 0; i < n; i++)
  38. {
  39. rev[i] = new List<int>();
  40. }
  41.  
  42. for (int i = 0; i < n; i++)
  43. {
  44. innum[S[i]]++;
  45. rev[S[i]].Add(i);
  46. }
  47.  
  48. used = new bool[n];
  49. ret = 0;
  50.  
  51. while (true)
  52. {
  53. bool flag = false;
  54. for (int i = 0; i < n; i++)
  55. {
  56. if (innum[i] == 0 && !used[i])
  57. {
  58. ret += L[i];
  59. used[i] = true;
  60. innum[S[i]]--;
  61. flag = true;
  62. }
  63. }
  64. if (!flag) break;
  65. }
  66.  
  67.  
  68. for (int i = 0; i < n; i++)
  69. {
  70. if (!used[i])
  71. {
  72. int temp = dfs(i);
  73. ret += temp;
  74. }
  75. }
  76. Console.WriteLine("{0:0.0}", ret / 2.0);
  77. }
  78.  
  79. int dfs(int a)
  80. {
  81. if (used[a]) return 99999999;
  82. used[a] = true;
  83.  
  84. int r = L[a];
  85. ret += L[a];
  86.  
  87. foreach (var item in rev[a])
  88. {
  89. r = Math.Min(dfs(item), r);
  90. }
  91.  
  92. return r;
  93. }
  94.  
  95.  
  96. }
  97.  
  98.  
  99.  
  100.  
  101. class Scanner
  102. {
  103. string[] s;
  104. int i;
  105.  
  106. char[] cs = new char[] { ' ' };
  107.  
  108. public Scanner()
  109. {
  110. s = new string[0];
  111. i = 0;
  112. }
  113.  
  114. public string next()
  115. {
  116. if (i < s.Length) return s[i++];
  117. do
  118. {
  119. s = Console.ReadLine().Split(cs, StringSplitOptions.RemoveEmptyEntries);
  120. } while ((s.Length == 1 && s[0] == "") || s.Length == 0);
  121. i = 0;
  122. return s[i++];
  123. }
  124.  
  125. public int nextInt()
  126. {
  127. return int.Parse(next());
  128. }
  129.  
  130. public long nextLong()
  131. {
  132. return long.Parse(next());
  133. }
  134.  
  135. public double nextDouble()
  136. {
  137. return double.Parse(next());
  138. }
  139.  
  140. }
  141.  
Success #stdin #stdout 0.05s 33736KB
stdin
100
98 25
85 19
11 20
65 55
83 20
80 26
82 1
4 98
48 99
30 40
34 83
50 45
49 23
49 17
72 45
43 6
20 83
47 87
5 97
93 94
14 91
93 88
24 93
46 19
65 86
66 13
2 58
97 89
5 4
87 44
60 23
29 80
5 40
37 77
30 60
72 13
11 45
93 21
7 67
4 96
18 22
37 89
76 77
81 26
64 37
32 83
68 30
60 62
45 25
90 18
57 61
86 89
91 38
16 82
47 72
70 99
34 77
15 67
80 19
44 39
73 30
67 100
18 2
80 45
86 92
85 55
5 68
59 6
79 54
12 55
13 24
13 50
51 63
25 15
20 3
33 75
54 57
45 24
35 81
98 88
91 82
40 51
53 21
99 55
93 18
24 83
4 38
41 22
58 31
86 31
92 44
87 32
34 80
17 89
63 99
79 24
29 17
26 72
96 92
77 23
stdout
2616.0