#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e8;
const int mod = 1e9 + 7;
int n, x, t, a[mxN], b[mxN], c[mxN];
map<int, int> hash1;
void solve() {
  cin >> n;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
    c[i] = a[i];
  }
  for (int i = 1; i < n; i++) {
    if (c[i] - c[i - 1] < 0) {
      sum += abs(c[i] - c[i - 1]);
      c[i] = c[i - 1];
    }
  }
  int sum1 = 0;
  for (int i = n - 2; i >= 0; i--) {
    if (b[i] - b[i + 1] > 0) {
      sum1 += abs(b[i] - b[i + 1]);
      b[i] = b[i + 1];
    }
  }
  cout << min(sum, sum1) << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  t = 1;
  for (int i = 1; i <= t; ++i) {
    solve();
  }
}
