Guest User

Untitled

a guest
Jun 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class H
  5. {
  6. public static void main(String args[])
  7. {
  8. FastScanner in = new FastScanner(System.in);
  9. PrintWriter out = new PrintWriter(System.out);
  10. while(new H().solve(in, out));
  11. out.close();
  12. }
  13.  
  14. int N, counter;
  15. ArrayList<Integer>[] adj;
  16.  
  17. int[] invp, jump;
  18. void dfs(int i)
  19. {
  20. int pre = counter++;
  21. invp[pre] = i;
  22.  
  23. for (int j : adj[i])
  24. dfs(j);
  25.  
  26. jump[pre] = counter;
  27. }
  28.  
  29. boolean solve(FastScanner in, PrintWriter out)
  30. {
  31. N = in.nextInt();
  32. int K = in.nextInt();
  33. if (N == 0 && K == 0)
  34. return false;
  35.  
  36. counter = 0;
  37. invp = new int[N];
  38. jump = new int[N];
  39.  
  40. adj = new ArrayList[N];
  41. for(int i=0; i<N; i++)
  42. adj[i] = new ArrayList<>();
  43.  
  44. int[] reward = new int[N];
  45. int root = -1;
  46. for(int i=0; i<N; i++)
  47. {
  48. int p = in.nextInt()-1;
  49. if (p == -1)
  50. {
  51. root = i;
  52. }
  53. else
  54. {
  55. adj[p].add(i);
  56. }
  57.  
  58. reward[i] = in.nextInt();
  59. }
  60.  
  61. dfs(root);
  62.  
  63. int[] cur = new int[N+1];
  64. int[] nxt = new int[N+1];
  65.  
  66. Arrays.fill(cur, Integer.MIN_VALUE);
  67. cur[0] = 0;
  68.  
  69. for (int k=0; k<K; k++)
  70. {
  71. Arrays.fill(nxt, Integer.MIN_VALUE);
  72. for (int i=0; i<N; i++)
  73. {
  74. // state - (i, k)
  75. // i - ith node (by preorder number)
  76. // k - taken k nodes so far
  77.  
  78. // (i, k) - > (i+1, k)
  79. cur[i+1] = Math.max(cur[i+1], cur[i]);
  80.  
  81. // (i, k) - > (jump[i], k+1)
  82. nxt[jump[i]] = Math.max(nxt[jump[i]], cur[i] + reward[invp[i]]);
  83. }
  84.  
  85. for(int i=0; i<=N; i++)
  86. cur[i] = nxt[i];
  87. }
  88.  
  89. int res = 0;
  90. for(int d : cur)
  91. res = Math.max(res, d);
  92. out.println(res);
  93.  
  94. return true;
  95. }
  96. }
  97.  
  98. class FastScanner{
  99. private InputStream stream;
  100. private byte[] buf = new byte[1024];
  101. private int curChar;
  102. private int numChars;
  103.  
  104. public FastScanner(InputStream stream)
  105. {
  106. this.stream = stream;
  107. }
  108.  
  109. int read()
  110. {
  111. if (numChars == -1)
  112. throw new InputMismatchException();
  113. if (curChar >= numChars){
  114. curChar = 0;
  115. try{
  116. numChars = stream.read(buf);
  117. } catch (IOException e) {
  118. throw new InputMismatchException();
  119. }
  120. if (numChars <= 0)
  121. return -1;
  122. }
  123. return buf[curChar++];
  124. }
  125.  
  126. boolean isSpaceChar(int c)
  127. {
  128. return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
  129. }
  130.  
  131. boolean isEndline(int c)
  132. {
  133. return c=='\n'||c=='\r'||c==-1;
  134. }
  135.  
  136. int nextInt()
  137. {
  138. return Integer.parseInt(next());
  139. }
  140.  
  141. long nextLong()
  142. {
  143. return Long.parseLong(next());
  144. }
  145.  
  146. double nextDouble()
  147. {
  148. return Double.parseDouble(next());
  149. }
  150.  
  151. String next(){
  152. int c = read();
  153. while (isSpaceChar(c))
  154. c = read();
  155. StringBuilder res = new StringBuilder();
  156. do{
  157. res.appendCodePoint(c);
  158. c = read();
  159. }while(!isSpaceChar(c));
  160. return res.toString();
  161. }
  162.  
  163. String nextLine(){
  164. int c = read();
  165. while (isEndline(c))
  166. c = read();
  167. StringBuilder res = new StringBuilder();
  168. do{
  169. res.appendCodePoint(c);
  170. c = read();
  171. }while(!isEndline(c));
  172. return res.toString();
  173. }
  174. }
Add Comment
Please, Sign In to add comment