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 SegTree:
      def __init__(self, N):
          # N:  処理する区間の長さ
          self.N0 = 2 ** (N - 1).bit_length()
          self.INF = 2**31 - 1
          self.seg_max = [-self.INF] * (2 * self.N0)
  
      def update(self, index, value):
          index += self.N0 - 1
          self.seg_max[index] = value
          while index > 0:
              index = (index - 1) // 2
              self.seg_max[index] = max(
                  self.seg_max[index * 2 + 1], self.seg_max[index * 2 + 2]
              )
  
      def query(self, first, last):
          first += self.N0 - 1
          last += self.N0 - 1
          ret = -self.INF
          while first <= last:
              if not first & 1:
                  ret = max(ret, self.seg_max[first])
              if last & 1:
                  ret = max(ret, self.seg_max[last])
              first = first // 2
              last = last // 2 - 1
          return ret
  
  
  N = int(input())
  h = list(map(int, input().split()))
  a = list(map(int, input().split()))
  
  rnk2idx = {}
  for i in range(N):
      rnk2idx[h[i]] = i + 1
  segtree = SegTree(N + 1)
  for i in range(1, N + 1):
      segtree.update(i, 0)
  for i in range(1, N + 1):
      idx = rnk2idx[i]
      segtree.update(idx, segtree.query(1, idx) + a[idx - 1])
  
  print(segtree.query(1, N))
  
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."}]