#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

const int INF = 1 << 24;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int,int> P;

const int MAX_W = 100001;
int N, X[100000];

class SegmentTree{
public:
    SegmentTree(){}
    SegmentTree(int n){init(n);}
    void init(int n){
        size = 1;
        while(size < n){
            size *= 2;
        }
        REP(i, size*2-1){
            data[i] = 0;
        }
    }
    void update(int index, ull v){
        index += size - 1;
        data[index] = v;
        while(index > 0){
            index = (index-1) / 2;
            data[index] = std::max(data[index*2+1], data[index*2+2]);
        }
    }
    ull query(int a, int b){
        return _query(a, b, 0, 0, size);
    }
private:
    ull _query(int a, int b, int k, int l, int r){
        if(b <= l || r <= a){return 0;}
        if(a <= l && r <= b){return data[k];}
        return std::max(_query(a, b, k*2+1, l, (l+r)/2), _query(a, b, k*2+2, (l+r)/2, r));
    }
    ull data[MAX_W*4];
    int size;
};

SegmentTree st;

int main(){
    std::cin >> N;
    st.init(N+1);

    ull sum = 0;
    REP(i, N){
        std::cin >> X[i];
        sum += X[i];
    }

    REP(i, N){
        //std::cout << st.query(0, X[i]) + X[i] << std::endl;
        st.update(X[i], st.query(0, X[i]) + X[i]);
    }
    
    std::cout << sum-st.query(0, 100001) << std::endl;
}