#include<cstdio>
#include<algorithm>
using namespace std;

typedef long long ll;

ll n, x[100001];
ll N, dat[400001], dp[100001];

void init() {
  N = 1;
  while (N < n + 1) N *= 2;
  for (int i = 0; i < 2 * N - 1; i++) dat[i] = 0;
}

void update(ll k, ll 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]);
  }
}

ll query(ll a, ll b, ll k, ll l, ll r) {
  if (r <= a || b <= l) return -1;
  if (a <= l && r <= b) return dat[k];
  else {
    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);
  }
}

ll seg(ll a) {
  //printf("a: %lld ",a);
  return query(0, a, 0, 0, N);
}


int main() {
  scanf("%lld", &n);
  init();
  for (int i = 1; i <= n; i++) scanf("%lld", &x[i]);
  for (int i = 1; i <= n; i++) {
    dp[i] = seg(x[i]) + x[i];
    update(x[i], dp[i]);
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
  printf("%lld\n", n * (n + 1) / 2 - ans);
}
