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: class NumArray(object):
  
      def __init__(self, nums):
          self.l = len(nums)
          self.tree = [0] * self.l + nums
          for i in range(self.l - 1, 0, -1):
              self.tree[i] = max(self.tree[i << 1], self.tree[i << 1 | 1])
  
      def update(self, i, val):
          n = self.l + i
          self.tree[n] = val
          while n > 1:
              self.tree[n >> 1] = max(self.tree[n], self.tree[n ^ 1])
              n >>= 1
  
      def maxRange(self, i, j):
          m = self.l + i
          n = self.l + j
          res = 0
          while m <= n:
              if m & 1:
                  # res += self.tree[m]
                  res = max(res, self.tree[m])
                  m += 1
              m >>= 1
              if n & 1 == 0:
                  # res += self.tree[n]
                  res = max(res, self.tree[n])
                  n -= 1
              n >>= 1
          return res
  
  
  N = int(input())
  H = list(map(int, input().strip().split(" ")))
  V = list(map(int, input().strip().split(" ")))
  Ans = NumArray([0] * (N + 1))
  for i in range(len(H)):
      h = H[i]
      ans1 = Ans.maxRange(0, h - 1) + V[i]
      Ans.update(h, ans1)
  print(max(Ans.tree))
  
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."}]