#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 0

int N;
ll* table;

void init(int first_N){
	while(N < first_N)N *= 2;
}

void update(int loc,ll value){
	loc += N-1;

	table[loc] = value;

	if(N == 1)return;

	int parent = (loc-1)/2;

	while(true){
		table[parent] = max(table[2*parent+1],table[2*parent+2]);

		if(parent == 0)break;
		else{
			parent = (parent-1)/2;
		}
	}
}


ll query(int search_left,int search_right,int node_id,int node_left,int node_right){

	if(search_right < node_left || search_left > node_right)return NUM;

	if(search_left <= node_left && search_right >= node_right){
		return table[node_id];
	}

	ll left_max = query(search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2);
	ll right_max = query(search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right);

	return max(left_max,right_max);
}


int main(){

	int first_N;
	table = new ll[263000];
	N = 1;

	scanf("%d",&first_N);

	init(100001);

	for(int i = 0; i <= 2*N-2; i++)table[i] = NUM;

	ll tmp,left_max,sum = 0;

	for(int loop = 0; loop < first_N; loop++){

		scanf("%lld",&tmp);

		left_max = query(0,tmp-1,0,0,N-1);

		update(tmp,left_max+tmp);

		sum += tmp;
	}

	ll ans = 0;

	for(int i = 1; i <= 100000; i++){
		ans = max(ans,table[N-1+i]);
	}

	printf("%lld\n",sum - ans);

	return 0;
}