# 参考・参照サイト
# https://ikatakos.com/pot/programming_algorithm/contest_history/atcoder/2019/0106_educational_dp_2

# require 'pry'
N = gets.to_i
ind = -1
height = gets.split.map do |high|
  ind += 1
  t = high.to_i
  [t, ind]
end
beauty = gets.split.map(&:to_i)
# p height
# binding.pry
n2 = 1 << N.bit_length
OFFSET = n2 - 1
@data = Array.new((n2 << 1) - 1, 0)

def update(k, x)
  i = k + OFFSET
  while i >= 0
    if @data[i] < x
      @data[i] = x
    else
      break
    end
    i = (i - 1) / 2
  end
end

def get_max(k)
  i = k + OFFSET
  ret = @data[i]
  while i > 0
    if i % 2 == 0
      ret = @data[i-1] if ret < @data[i-1]
    end
    i = (i - 1) / 2
  end
  return ret
end

srt = height.sort_by{|x|x[0]}
srt.each do |v, i|
  update(i , get_max(i) + beauty[i])
end
puts @data[0]