class SegmentTree
	def initialize(arr, l = 0, r = arr.size - 1)
		@l = l
		@r = r
		if l == r
			@max = arr[l]
		else
			c = (l + r) / 2
			@left_child = self.class.new(arr, l, c)
			@right_child = self.class.new(arr, c+1, r)
			lm = @left_child.max
			rm = @right_child.max
			@max = (lm > rm ? lm : rm)
		end
	end

	def query(r)
		return @max if r == @r
		if r <= @left_child.r
			return @left_child.query(r)
		else
			lm = @left_child.max
			rm = @right_child.query(r)
			return lm > rm ? lm : rm
		end
	end

	def update(i, x)
		if @l == @r
			@max = x	
		elsif i <= @left_child.r 
			@left_child.update(i, x)
			@max = @left_child.max if @left_child.max > @max
		else
			@right_child.update(i, x)
			@max = @right_child.max if @right_child.max > @max
		end
	end

	attr_reader :max, :l, :r
end

n = gets.to_i
t = SegmentTree.new([0] * (n+1))
gets.split.map(&:to_i).each do |x|
	t.update(x, t.query(x - 1) + x)
end
p n * (n + 1) / 2 - t.max