class SegmentTree def initialize(arr) @h = (arr.length - 1).bit_length @n = 2**@h @ie = Float::INFINITY @tree = Array.new(2*@n, @ie) 0.upto(arr.length-1) {|i| @tree[@n + i] = arr[i] } (@n-1).downto(1) {|i| @tree[i] = [@tree[2 * i], @tree[2 * i + 1]].min } end def set(idx, x) idx += @n @tree[idx] = x while idx > 0 do idx >>= 1 @tree[idx] = [@tree[2 * idx], @tree[2 * idx + 1]].min end end def query(lt, rt) lt += @n rt += @n vl = vr = @ie while rt - lt > 0 do if lt & 1 != 0 then vl = [vl, @tree[lt]].min lt += 1 end if rt & 1 != 0 then rt -= 1 vr = [@tree[rt], vr].min end lt >>= 1 rt >>= 1 end [vl, vr].min end end INF = Float::INFINITY N = gets.to_i A = gets.split(" ").map{|s| s.to_i} S = A.map.with_index{|x, i| [x, i]}.sort_by!{|x| x[0]} res = INF