output_description: Print the maximum possible sum of the beauties of the remaining flowers.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      h_1 h_2 \ldots h_N
      a_1 a_2 \ldots a_N
attempt: import math
  
  
  def makeTree(x, y):
      if y > x:
          T[(x, (x + y) // 2)] = 0
          makeTree(x, (x + y) // 2)
          T[((x + y) // 2 + 1, y)] = 0
          makeTree((x + y) // 2 + 1, y)
  
  
  def rmq(r, l, x, y):
      if l < x or r > y:
          return 0
      if r <= x and y <= l:
          return T[(x, y)]
      return max(rmq(r, l, x, (x + y) // 2), rmq(r, l, (x + y) // 2 + 1, y))
  
  
  def udTree(i):
      T[(i, i)] = dp[i]
      x = i
      y = i
      d = 1
      while d < 2**n:
          if x % (2 * d) == 0:
              T[(x, x + 2 * d - 1)] = max(T[(x, x + 2 * d - 1)], T[(x, y)])
              y = x + 2 * d - 1
          else:
              T[(y - 2 * d + 1, y)] = max(T[(y - 2 * d + 1, y)], T[(x, y)])
              x = y - 2 * d + 1
          d = y - x + 1
  
  
  N = int(input())
  H = list(map(int, input().split()))
  A = list(map(int, input().split()))
  Ho = [(i, H[i]) for i in range(N)]
  Ho = sorted(Ho, key=lambda x: x[1])
  dp = [0 for i in range(N)]
  n = math.ceil(math.log2(N))
  T = {}
  T[(0, 2**n - 1)] = 0
  makeTree(0, 2**n - 1)
  for i in range(N):
      ind = Ho[i][0]
      if ind == 0:
          dp[ind] = A[ind]
          udTree(ind)
      else:
          dp[ind] = rmq(0, ind - 1, 0, 2**n - 1) + A[ind]
          udTree(ind)
  print(T[(0, 2**n - 1)])
  
problem_description: Statement
  
  There are N flowers arranged in a row. For each i (1 \leq i \leq N), the
  height and the beauty of the i-th flower from the left is h_i and a_i,
  respectively. Here, h_1, h_2, \ldots, h_N are all distinct.
  
  Taro is pulling out some flowers so that the following condition is met:
  
    * The heights of the remaining flowers are monotonically increasing from left to right.
  
  Find the maximum possible sum of the beauties of the remaining flowers.
samples: [{"input": "4\n    3 1 4 2\n    10 20 30 40", "output": "60\n    \n\nWe should keep the second and fourth flowers from the left. Then, the heights\nwould be 1, 2 from left to right, which is monotonically increasing, and the\nsum of the beauties would be 20 + 40 = 60.\n\n* * *"}, {"input": "1\n    1\n    10", "output": "10\n    \n\nThe condition is met already at the beginning.\n\n* * *"}, {"input": "5\n    1 2 3 4 5\n    1000000000 1000000000 1000000000 1000000000 1000000000", "output": "5000000000\n    \n\nThe answer may not fit into a 32-bit integer type.\n\n* * *"}, {"input": "9\n    4 2 5 8 3 6 1 7 9\n    6 8 8 4 6 3 5 7 5", "output": "31\n    \n\nWe should keep the second, third, sixth, eighth and ninth flowers from the\nleft."}]