Bobjoehi

NutrientTree

Sep 26th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class NutrientTree {
  4.    
  5.     static Node createTree(String s) {
  6.         s = s.trim();
  7.         if(s.charAt(0) != '(') {
  8.             return new Node(Integer.parseInt(s));
  9.         }else {
  10.             s = s.substring(1, s.length() - 1).trim();
  11.  
  12.             int ind = -1;
  13.            
  14.             if(s.charAt(0) != '(') {
  15.                 ind = s.indexOf(' ');
  16.             }else {
  17.                 int count = 0;
  18.                 for(int i = 0; i < s.length(); i ++) {
  19.                     if(s.charAt(i) == '(') {
  20.                         count ++;
  21.                     }else if(s.charAt(i) == ')') {
  22.                         count --;
  23.                     }
  24.                     if(count == 0) {
  25.                         ind = i;
  26.                         break;
  27.                     }
  28.                 }
  29.             }
  30.             return new Node(createTree(s.substring(0, ind + 1)), createTree(s.substring(ind + 1)));
  31.         }
  32.     }
  33.    
  34.     static void solve(Node tree, int x) {
  35.         if(tree.lc == null) {
  36.             tree.dp = new int[x + 1];
  37.             for(int i = 0; i <= x; i ++) {
  38.                 tree.dp[i] = tree.v + i;
  39.             }
  40.         }else {
  41.             solve(tree.lc, x);
  42.             solve(tree.rc, x);
  43.            
  44.             int[] dpl = new int[x + 1];
  45.             for(int i = 0; i <= x; i ++) {
  46.                 int max = 0;
  47.                
  48.                 for(int j = 0; j <= i; j ++) {
  49.                     max = Math.max(Math.min((1 + j) * (1 + j), tree.lc.dp[i - j]), max);
  50.                 }
  51.                 dpl[i] = max;
  52.             }
  53.            
  54.             int[] dpr = new int[x + 1];
  55.             for(int i = 0; i <= x; i ++) {
  56.                 int max = 0;
  57.                
  58.                 for(int j = 0; j <= i; j ++) {
  59.                     max = Math.max(Math.min((1 + j) * (1 + j), tree.rc.dp[i - j]), max);
  60.                 }
  61.                 dpr[i] = max;
  62.             }
  63.            
  64.             tree.dp = new int[x + 1];
  65.            
  66.             for(int i = 0; i <= x; i ++) {
  67.                 int max = 0;
  68.                 for(int j = 0; j <= i; j ++) {
  69.                     max = Math.max(dpl[j] + dpr[i - j], max);
  70.                 }
  71.                 tree.dp[i] = max;
  72.             }
  73.         }
  74.     }
  75.    
  76.     public static void main(String[] args) throws IOException{
  77.         String s = in.readLine();
  78.         readLine();
  79.         int x = nextInt();
  80.        
  81.         Node root = createTree(s);
  82.        
  83.         solve(root, x);
  84.        
  85.         System.out.println(root.dp[x]);
  86.        
  87.     }
  88.     static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  89.     static StringTokenizer st;
  90.     static void readLine() throws IOException{
  91.         st = new StringTokenizer(in.readLine());
  92.     }
  93.     static int nextInt() {
  94.         return Integer.parseInt(st.nextToken());
  95.     }
  96.    
  97.    
  98.    
  99.     static class Node{
  100.         int[] dp;
  101.         int v;
  102.         Node lc, rc;
  103.         Node(int v){
  104.             this.v = v;
  105.             lc = null;
  106.             rc = null;
  107.         }
  108.         Node(Node lc, Node rc){
  109.             v = 0;
  110.             this.lc = lc;
  111.             this.rc = rc;
  112.         }
  113.         void print() {
  114.             if(lc == null) {
  115.                 System.out.println(v);
  116.             }else {
  117.                 System.out.println("this is node");
  118.                 lc.print();
  119.                 rc.print();
  120.             }
  121.         }
  122.     }
  123.    
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment