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 operator
  
  
  class SegmentTree:
      def __init__(self, size, fn=operator.add, default=None, initial_values=None):
          """
          :param int size:
          :param callable fn: 区間に適用する関数。引数を 2 つ取る。min, max, operator.xor など
          :param default:
          :param list initial_values:
          """
          default = default or 0
  
          # size 以上である最小の 2 冪を size とする
          n = 1
          while n < size:
              n *= 2
          self._size = n
          self._fn = fn
  
          self._tree = [default] * (self._size * 2 - 1)
          if initial_values:
              i = self._size - 1
              for v in initial_values:
                  self._tree[i] = v
                  i += 1
              i = self._size - 2
              while i >= 0:
                  self._tree[i] = self._fn(self._tree[i * 2 + 1], self._tree[i * 2 + 2])
                  i -= 1
  
      def set(self, i, value):
          """
          i 番目に value を設定
          :param int i:
          :param value:
          :return:
          """
          x = self._size - 1 + i
          self._tree[x] = value
  
          while x > 0:
              x = (x - 1) // 2
              self._tree[x] = self._fn(self._tree[x * 2 + 1], self._tree[x * 2 + 2])
  
      def update(self, i, value):
          """
          もとの i 番目と value に fn を適用したものを i 番目に設定
          :param int i:
          :param value:
          :return:
          """
          x = self._size - 1 + i
          self.set(i, self._fn(self._tree[x], value))
  
      def get(self, from_i, to_i=None, k=0, L=None, r=None):
          """
          [from_i, to_i) に fn を適用した結果を返す
          :param int from_i:
          :param int to_i:
          :param int k: self._tree[k] が、[L, r) に fn を適用した結果を持つ
          :param int L:
          :param int r:
          :return:
          """
          if to_i is None:
              return self._tree[self._size - 1 + from_i]
  
          L = 0 if L is None else L
          r = self._size if r is None else r
  
          if from_i <= L and r <= to_i:
              return self._tree[k]
  
          if to_i <= L or r <= from_i:
              return None
  
          ret_L = self.get(from_i, to_i, k * 2 + 1, L, (L + r) // 2)
          ret_r = self.get(from_i, to_i, k * 2 + 2, (L + r) // 2, r)
          if ret_L is None:
              return ret_r
          if ret_r is None:
              return ret_L
          return self._fn(ret_L, ret_r)
  
      def __len__(self):
          return self._size
  
  
  def resolve():
      N = int(input())
      H = list(map(int, input().split()))
      A = list(map(int, input().split()))
  
      Flowers = []
      for i in range(N):
          Flowers.append((H[i], (A[i], i)))
      # 高さを昇順にソート
      Flowers.sort()
  
      seg = SegmentTree(N, fn=max)
      for i in range(N):
          h, (a, idx) = Flowers[i]
          maxV = seg.get(0, idx)
          # l=0, r=0 の時Noneを返す仕様
          if maxV == None:
              maxV = 0
          seg.set(idx, maxV + a)
  
      ans = seg.get(0, N)
      print(ans)
  
  
  if __name__ == "__main__":
      resolve()
  
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."}]