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: from collections import defaultdict, deque, Counter
  from heapq import heappush, heappop, heapify
  import math
  import bisect
  import random
  from itertools import permutations, accumulate, combinations, product
  import sys
  import string
  from bisect import bisect_left, bisect_right
  from math import factorial, ceil, floor
  from operator import mul
  from functools import reduce
  
  sys.setrecursionlimit(2147483647)
  INF = 10**20
  
  
  def LI():
      return list(map(int, sys.stdin.readline().split()))
  
  
  def I():
      return int(sys.stdin.readline())
  
  
  def LS():
      return sys.stdin.buffer.readline().rstrip().decode("utf-8").split()
  
  
  def S():
      return sys.stdin.buffer.readline().rstrip().decode("utf-8")
  
  
  def IR(n):
      return [I() for i in range(n)]
  
  
  def LIR(n):
      return [LI() for i in range(n)]
  
  
  def SR(n):
      return [S() for i in range(n)]
  
  
  def LSR(n):
      return [LS() for i in range(n)]
  
  
  def SRL(n):
      return [list(S()) for i in range(n)]
  
  
  def MSRL(n):
      return [[int(j) for j in list(S())] for i in range(n)]
  
  
  mod = 1000000007
  
  
  class BIT:
      def __init__(self, size):
          self.bit = [0] * size
          self.size = size
          self.total = 0
  
      def add(self, i, w):
          x = i + 1
          self.total += w
          while x <= self.size:
              self.bit[x - 1] = max(self.bit[x - 1], w)
              x += x & -x
          return
  
      def sum(self, i):
          res = 0
          x = i + 1
          while x:
              res = max(res, self.bit[x - 1])
              x -= x & -x
          return res
  
  
  n = I()
  H = LI()
  A = LI()
  D = {e: i for i, e in enumerate(sorted(H))}
  bit = BIT(n)
  for i in range(n):
      idx = D[H[i]]
      bit.add(idx, bit.sum(idx) + A[i])
  
  print(bit.sum(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."}]