#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
int n;
int x[100001];
ll dp[1<<20];
int n_;
ll query(int a,int b,int k=0,int l=0,int r=n_){
	if(a<=l && r<=b)return dp[k];
	if(r<=a || b<=l)return 0;
	ll vl=query(a,b,k*2+1,l,(l+r)/2);
	ll vr=query(a,b,k*2+2,(l+r)/2,r);
	return max(vl,vr);
}

void update(int k,ll a){
	k+=n_-1;
	dp[k]=a;
	while(k>0){
		k=(k-1)/2;
		dp[k]=max(dp[k*2+1],dp[k*2+2]);
	}
}

int main(void){
	scanf("%d",&n);
	n_=1;
	while(n_<n)n_*=2;
	ll sum=0;
	for(int i=0;i<n;i++){
		scanf("%d",&x[i]);
		sum+=(ll)x[i];
	}
	for(int i=0;i<n;i++){
		ll val=query(0,x[i]-1);
		update(x[i]-1,(ll)x[i]+val);
	}
	printf("%lld\n",sum-dp[0]);
	return 0;
}