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 sys
  
  input = sys.stdin.readline
  
  n = int(input())
  h = [int(i) for i in input().split()]
  a = [int(i) for i in input().split()]
  
  
  sellogn = []
  seln = 0
  selfunc = max
  selone = 0
  selb = []
  
  
  def __init__(a, func=max, one=-(10**18)):
      global sellogn, seln, selfunc, selone, selb
      sellogn = (len(a) - 1).bit_length()
      seln = 1 << sellogn
      selfunc = func
      selone = one
  
      selb = [selone] * (2 * seln - 1)
      for i, j in enumerate(a):
          selb[i + seln - 1] = j
      for i in reversed(range(seln - 1)):
          selb[i] = selfunc(selb[i * 2 + 1], selb[i * 2 + 2])
  
  
  def get_item(i):
      global sellogn, seln, selfunc, selone, selb
      return selb[i + seln - 1]
  
  
  def update(index, x):
      global sellogn, seln, selfunc, selone, selb
      i = index + seln - 1
      selb[i] = x
      while i != 0:
          i = (i - 1) // 2
          selb[i] = selfunc(selb[i * 2 + 1], selb[i * 2 + 2])
  
  
  def update_func(index, x):
      global sellogn, seln, selfunc, selone, selb
      i = index + seln - 1
      selb[i] = selfunc(selb[i], x)
      while i != 0:
          i = (i - 1) // 2
          selb[i] = selfunc(selb[i * 2 + 1], selb[i * 2 + 2])
  
  
  def get_segment(l, r):
      global sellogn, seln, selfunc, selone, selb
      l += seln
      r += seln
      s = selone
      t = selone
      while l < r:
          if l & 1:
              s = selfunc(s, selb[l - 1])
              l += 1
          if r & 1:
              r -= 1
              t = selfunc(selb[r - 1], t)
          l >>= 1
          r >>= 1
      return selfunc(s, t)
  
  
  __init__([0] * (n + 1))
  
  for i in range(n):
      j = get_segment(0, h[i] + 1)
      update_func(h[i], j + a[i])
  print(get_segment(0, 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."}]