#!/usr/bin/env ruby

def c140e(p, vec=:first, indent='')
  if p.size < 2
    return 0
  end

  # get the index of max, 2nd-max
  max = []
  if p[0] < p[1]
    max = [1, 0]
  else
    max = [0, 1]
  end
  (2...p.size).each do |index|
    if p[max[0]] < p[index]
      max = [index, max[0]]
    elsif p[max[1]] < p[index]
      max = [max[0], index]
    end
  end

  # call the left side, mid, right side
  second = p[max[1]]

  unless max[0] < max[1]
    max = [max[1], max[0]]
  end

  left = c140e(p[0...max[1]], :left, (vec!=:right) ? indent+'  ' : indent+'| ')

  mid = second * (1 + max[0]) * (p.size - max[1])
  $stderr.puts "#{indent}+- p=#{p} max=#{max} num=#{mid}"

  dup = c140e(p[max[0]+1...max[1]])

  right = c140e(p[max[0]+1...p.size], :right, (vec!=:left) ? indent+'  ' : indent+'| ')
  return left + mid + right - dup
end

ARGF.gets
p = ARGV.gets.split.map(&:to_i)
puts c140e(p)
