#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<climits>
#include<map>
#include<set>
#include<cassert>
#include<deque>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define INF 1<<27
#define all(n) n.begin(),n.end()
#define insert(a,b,c,d) PP(P(a,b),P(c,d))
#define F first
#define S second
#define pb push_back
#define MAX_N (1<<18)
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();++i)
using namespace std;
typedef pair<int,int> P;
typedef pair<P,P> PP;
typedef long long ll;





template<class T>
class RMQ_segment{
private:
  int n;
  T dat[2*MAX_N-1];
public:
  void init(int n_){
    n = 1;
    while(n<n_)n*=2;
    for(int i=0;i<2*n-1;i++)dat[i] = 0;
  }

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

  T query(int a,int b,int k,int l,int r){
    if(r<=a || b<=l)return 0;
    else if(a<=l && r<=b)return dat[k];
    
      T vl = query(a,b,k*2+1,l,(l+r)/2);
      T vr = query(a,b,k*2+2,(l+r)/2,r);
      return max(vl,vr);
    
  }

  T _query(int a,int b){//nの範囲でミスしないように極力こっちを利用
    return query(a,b,0,0,n);
  }

};


 int x[1000010];
 RMQ_segment<ll> rmq;
int main(){
  int n;
  cin >> n;
 
  rmq.init(n+1);
 
  rep(i,n)cin >> x[i];
  ll mex=0;
  ll sum=0;

  for(int i=0;i<n;i++){
    sum+=x[i];
    ll premex = rmq._query(0,x[i])+x[i];
    rmq.update(x[i],premex);
    mex = max(mex,premex);
   
  }  
  
  cout << sum-mex << endl;

return 0;
}