#include "bits/stdc++.h"
using namespace std;
typedef long long LL;
LL bit[100001];
int n;
LL max_q(int i) {
	LL ret = 0;
	while (i > 0) {
		ret = max(ret, bit[i]);
		i -= i&-i;
	}
	return ret;
}
void update(int i, LL x) {
	bit[i] = max(bit[i], x);
	while (i <= n) {
		bit[i] = max(bit[i], x);
		i += i&-i;
	}
}
int main() {
	cin >> n;
	LL sum = 0;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		LL r = max_q(x - 1);
		update(x, r + x);
		sum += x;
	}
	cout << sum - max_q(n) << endl;
}
