$n = gets.to_i

values = gets.chomp.split(" ").map(&:to_i)
costs = gets.chomp.split(" ").map(&:to_i)

hash = {}
$n.times do |i|
  hash[i] = {value:values[i], cost:costs[i]}
end

def solve(start,hash , cost, got)
  if got.include?(hash[start])
    return cost
  end

  got << hash[start]
  cost << cost.last + hash[start][:value] - hash[start][:cost]
  $n.times do |i|
    solve(i,hash,cost,got)
  end

  return cost
end

max = 0
$n.times do |i|
  max = [max,solve(i,hash, [0], []).max].max
end

puts max