#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <functional>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
typedef long long ll;
const ll p2=1<<17;
ll a[100000],seg[p2*2-1];
void update(int x){
	while(true){
		x=(x-1)/2;
		seg[x]=max(seg[x*2+1],seg[x*2+2]);
		if(x==0) return;
	}
}
ll segmax(int a,int b,int l,int r,int k){
	if(b<=l||r<=a) return 0;
	if(a<=l&&r<=b) return seg[k];
	return max(segmax(a,b,l,(l+r)/2,k*2+1),segmax(a,b,(l+r)/2,r,k*2+2));
}
int main(){
	int N;
	cin>>N;
	rep(i,N) cin>>a[i];
	rep(i,N) a[i]--;
	rep(i,N){
		seg[p2-1+a[i]]=segmax(0,a[i],0,p2,0)+a[i]+1;
		update(p2-1+a[i]);
	}
	ll mx=0;
	rep(i,N) mx=max(mx,seg[p2-1+i]);
	cout<<(ll)N*(N+1)/2-mx<<endl;
}