#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<double,int> P;
typedef pair<P,ll> T;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-15
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repn(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++)
#define EQ(a,b) (abs((a)-(b))<eps)
template<class T> void chmin(T& a,const T& b){if(a>b)a=b;}
template<class T> void chmax(T& a,const T& b){if(a<b)a=b;}
const int SIZE = 1<<18;
struct segtree{
	ll seg[SIZE*2];
	void update(int k,ll x){
		k+=SIZE-1;
		seg[k]=x;
		while(k){
			k = (k-1)/2;
			seg[k]=max(seg[k*2+1],seg[k*2+2]);
		}
	}
	ll query(int a,int b,int k=0,int l=0,int r=SIZE){
		if(r<=a||b<=l)return 0;
		else if(a<=l&&r<=b)return seg[k];
		else{
			ll lch = query(a,b,k*2+1,l,(l+r)/2);
			ll rch = query(a,b,k*2+2,(l+r)/2,r);
			return max(lch,rch);
		}
	}
}seg;
ll n;
vector<int> x;
int main(){
	scanf("%lld",&n);
	x.resize((int)n);
	for(int i=0;i<n;i++)scanf("%d",&x[i]);
	for(int i=0;i<n;i++){
		seg.update(x[i],(ll)x[i]+seg.query(0,x[i]));
	}
	cout << n*(n+1)/2ll-seg.query(0,SIZE) << endl;
	return 0;
}