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 bisect import bisect_left as bl
  from bisect import bisect_right as br
  from heapq import heappush,heappop
  import math
  from collections import *
  from functools import reduce,cmp_to_key
  import sys
  input = sys.stdin.readline
   
  M = mod = 10**9 + 7
  def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
  def inv_mod(n):return pow(n, mod - 2, mod)
   
  def li():return [int(i) for i in input().rstrip('\n').split()]
  def st():return input().rstrip('\n')
  def val():return int(input().rstrip('\n'))
  def li2():return [i for i in input().rstrip('\n')]
  def li3():return [int(i) for i in input().rstrip('\n')]
  
  
  class Node:
      def __init__(self,s,e):
          self.start=s
          self.end=e
          self.val = 0
          self.left=None
          self.right=None
   
  def build(nums,l,r):
      if l == r:
          temp = Node(l,l)
          temp.val = 0
      else:
          mid=(l+r)>>1
          temp=Node(l,r)
          temp.left=build(nums,l,mid)
          temp.right=build(nums,mid+1,r)
          temp.val = temp.left.val + temp.right.val
      return temp
   
  def update(root,start,va):
      if root.start==root.end==start:
          root.val = va
      elif root.start<=start and root.end>=start:
  
          root.left=update(root.left,start,va)
          root.right=update(root.right,start,va)
          root.val = max(root.left.val,root.right.val)
      return root
      
  def query(root,start,end):
      if root.start>=start and root.end<=end:return root.val
      elif root.start>end or root.end<start:return 0
      else:
          temp1 = query(root.left,start,end)
          temp2 = query(root.right,start,end)
          return max(temp1,temp2)
          
  
  root = build([],0,200005)
  n = val()
  h = li()
  a = li()
  ans = 0
  
  for i in range(n):
      curr = query(root,-1,h[i])
      temp = curr + a[i]
      ans = max(ans,temp)
      root = update(root,h[i],temp)
  print(ans)from bisect import bisect_left as bl
  from bisect import bisect_right as br
  from heapq import heappush,heappop
  import math
  from collections import *
  from functools import reduce,cmp_to_key
  import sys
  input = sys.stdin.readline
   
  M = mod = 10**9 + 7
  def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
  def inv_mod(n):return pow(n, mod - 2, mod)
   
  def li():return [int(i) for i in input().rstrip('\n').split()]
  def st():return input().rstrip('\n')
  def val():return int(input().rstrip('\n'))
  def li2():return [i for i in input().rstrip('\n')]
  def li3():return [int(i) for i in input().rstrip('\n')]
  
  
  class Node:
      def __init__(self,s,e):
          self.start=s
          self.end=e
          self.val = 0
          self.left=None
          self.right=None
   
  def build(nums,l,r):
      if l == r:
          temp = Node(l,l)
          temp.val = 0
      else:
          mid=(l+r)>>1
          temp=Node(l,r)
          temp.left=build(nums,l,mid)
          temp.right=build(nums,mid+1,r)
          temp.val = temp.left.val + temp.right.val
      return temp
   
  def update(root,start,va):
      if root.start==root.end==start:
          root.val = va
      elif root.start<=start and root.end>=start:
  
          root.left=update(root.left,start,va)
          root.right=update(root.right,start,va)
          root.val = max(root.left.val,root.right.val)
      return root
      
  def query(root,start,end):
      if root.start>=start and root.end<=end:return root.val
      elif root.start>end or root.end<start:return 0
      else:
          temp1 = query(root.left,start,end)
          temp2 = query(root.right,start,end)
          return max(temp1,temp2)
          
  
  root = build([],0,200005)
  n = val()
  h = li()
  a = li()
  ans = 0
  
  for i in range(n):
      curr = query(root,-1,h[i])
      temp = curr + a[i]
      ans = max(ans,temp)
      root = update(root,h[i],temp)
  print(ans)
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."}]