#include<iostream>
#include<algorithm>

using namespace std;
typedef long long ll;

struct SegTree {
	int n = 1 << 18;
	ll data[1 << 19];
	
	SegTree() {
		for (int i = 0; i < (1 << 19); i++)data[i] = 0;
	}

	void set(int pos, ll value) {
		int a = pos + n - 1;
		data[a] = value;
		while (a > 0) {
			a = (a - 1) / 2;
			data[a] = max(data[a * 2 + 1], data[a * 2 + 2]);
		}
	}

	//[l,r)
	ll get(int l, int r, int k, int a, int b) {
		if (a >= r || b <= l)return 0;
		if (l <= a && b <= r)return data[k];
		else {
			ll v1, v2;
			v1 = get(l, r, k * 2 + 1, a, (a + b) / 2);
			v2 = get(l, r, k * 2 + 2, (a + b) / 2, b);
			return max(v1, v2);
		}
	}

};

SegTree st;
ll N;

int main() {
	cin >> N;
	ll ans = N * (N + 1) / 2;

	for (int i = 0; i < N; i++) {
		ll a;
		cin >> a;
		st.set(a, st.get(0, a, 0, 0, 1 << 18) + a);
	}

	ans -= st.get(0, (1 << 18), 0, 0, 1 << 18);

	cout << ans << endl;

	return 0;
}
