#include <bits/stdc++.h>
inline int read() {
  char c = getchar();
  int x = 0, s = 1;
  while (c < '0' || c > '9') {
    if (c == '-') s = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * s;
}
using namespace std;
const int N = 1e6 + 5;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const int mod = 998244353;
const double II = acos(-1);
const double PP = (II * 1.0) / (180.00);
signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, Max = 0, sum = 0;
  cin >> n;
  int arr[n + 5];
  for (int i = 0; i < n; i++) cin >> arr[i];
  Max = arr[0];
  for (int i = 1; i < n; i++) {
    if (arr[i] < Max) sum += (Max - arr[i]);
    Max = max(Max, arr[i]);
  }
  cout << sum << endl;
}
