#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5;
long long a[MAXN];
void solve() {
  int N, total = 0;
  cin >> N;
  for (int x = 0; x < N; x++) cin >> a[x];
  for (int x = 0; x < N - 1; x++)
    if (a[x] > a[x + 1]) {
      total += a[x] - a[x + 1];
      a[x + 1] = a[x];
    }
  cout << total;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
