#include <bits/stdc++.h>
using namespace std;

// 0-indexed
template <class T>
struct SegmentTree {
  // a,b,c: T, e:T(unit)
  // abc = (ab)c = a(bc)
  // ae = ea = a
  typedef function<T(T, T)> F;
  int n;
  F f;
  T unit;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(int newn, F f, T t) : f(f), unit(t) {
    init(newn);
  }
  void init(int newn) {
    n = 1;
    while(n < newn) n <<= 1;
    dat.assign(n << 1, unit);
  }

  // "go up" process
  void update(int k, T newdata) {
    dat[k += n] = newdata;
    while(k >>= 1) {
      dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
    }
  }
  // [a,b)
  T query(int a, int b) {
    T vl = unit, vr = unit;
    for(int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
      if(l & 1) vl = f(vl, dat[l++]);
      if(r & 1) vr = f(dat[--r], vr);
    }
    return f(vl, vr);
  }
};

long long n;
vector<long long> x, id;

long long solve();

int main() {
  cin >> n;
  x.resize(n);
  for(int i = 0; i < n; ++i) cin >> x[i];
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long res = n * (n + 1) / 2;
  auto f = [&](long long l, long long r) {
    return max(l, r);
  };
  auto idf = [](long long l, long long r) {
    return x[l] < x[r];
  };
  id.resize(n);
  for(int i = 0; i < n; ++i) id[i] = i;
  sort(id.begin(), id.end(), idf);
  SegmentTree<long long> seg(n, f, 0);
  for(int i = 0; i < n; ++i) {
    seg.update(id[i], i + 1 + seg.query(0, id[i]));
  }
  return res - seg.query(0, n);
}
