n = gets.chomp.to_i
t = []
arr = gets.chomp.split.map(&:to_i)
n.times do |i|
  t << arr.shift
end

def minindexvalue(list)
  min = 10001 
  idx = 1001

  list.each.with_index do |x,i|
    if min > x
      idx = i
      min = x
    end
  end
  return idx
end

total = 0

res = []
until t.empty?
  idx = minindexvalue(t) 
  if idx == 0
    res << t.shift
  else
    total += t[0] + t[idx]
    t[0], t[idx] = t[idx], t[0]
    res << t.shift
  end
end
p total