#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

typedef long long int ll;

const int N = 1 << 17;
ll dat[N * 2 - 1] = { 0 }, n, m;

void update(int i, ll val) {
	i += m - 1;
	dat[i] = val;
	while(i > 0) {
		i = (i - 1) / 2;
		dat[i] = max(dat[i * 2 + 1], dat[i * 2 + 2]);
	}
	return;
}

ll getval(int a, int b, int k, int l, int r) {
	if (r <= a || b <= l) return 0;
	if (a <= l && r <= b) return dat[k];
	else{
		ll vl = getval(a, b, k * 2 + 1, l, (l + r) / 2);
		ll vr = getval(a, b, k * 2 + 2, (l + r) / 2, r);
		return max(vl, vr);
	}
}

int main(){
	ll a, v;
	double p, ip = 0.0, fp;
	cin >> n;
	p = log2(n);
	fp = modf(p, &ip);
	if(fp == 0.0) {
		m = 1 << int(p);
	}
	else{
		m = 1 << (int(p) + 1);
	}
	for(int i = 0;i < n;++i){
		cin >> a;
		v = getval(0, a, 0, 0, m);
		update(a - 1, a + v); // 0-indexedにしたいので
	}
	cout << n * (n + 1) / 2 - dat[0] << endl;
	return 0;
}
