def minimum_cost_sort(ary, n)
  cost = 0
  (n-1).downto(0) { |i|
    max = ary[0..i].max
    idx = ary.index(max)
    if i != idx
      tmp = ary[idx]
      ary[idx] = ary[i]
      ary[i] = tmp
      cost += ary[i] + ary[idx]
    end
  }
  cost
end

# main
n = gets.to_i
ary = gets.split(' ').map(&:to_i)
w = minimum_cost_sort(ary, n)
puts w