#include <bits/stdc++.h>
using namespace std;
struct edge {
  long long to, cost;
};
const long long INF = 100000000000000000;
const long long INF2 = 9223372036854775807;
const long long MOD = 1000000007;
long long ceil2(long long a, long long b) {
  if (a % b == 0) {
    return a / b;
  } else {
    return a / b + 1;
  }
}
long long pow2(long long a, long long b) {
  long long r = 1;
  for (long long i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
long long Log2(long long a) {
  long long t = 0;
  while (1) {
    if (a == 0 || a == 1) {
      break;
    }
    a /= 2;
    t++;
  }
  return t;
}
long long N;
long long A[200010];
signed main() {
  cin >> N;
  for (long long i = 1; i <= N; i++) {
    cin >> A[i];
  }
  long long L = 0;
  long long ans = 0;
  for (long long i = 0; i < (long long)(N); i++) {
    if (A[i] < A[i + 1]) {
      L = (((long long)(A[i + 1]) > (long long)(L)) ? (A[i + 1]) : (L));
    } else {
      ans += L - A[i + 1];
    }
  }
  cout << ans << endl;
  return 0;
}
