#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll find(const vector<ll>& b, int s) {
	ll res = 0;
	for (int i = s; i > 0; i = i & (i - 1))
		res = max(res, b[i]);
	return res;
}

void add(vector<ll>& b, int s, ll val) {
	int n = b.size();
	for (int i = s; i < n; i += (i & -i))
		b[i] = max(b[i], val);
}

int main()
{
	cin.sync_with_stdio(false);
	int n;
	cin >> n;
	ll sum = 0;
	vector<ll> bit(n + 1);
	for (int i = 0, x; i < n; i++) {
		cin >> x; sum += x;
		add(bit, x, find(bit, x - 1) + x);
	}
	printf("%lld\n", sum - find(bit, n));
	return 0;
}