#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int> >;
using pii = pair<int, int>;
using ppi = pair<pii, int>;
int f_dir[2][4] = {{-1, 0, 1, 0}, {0, 1, 0, -1}};
int e_dir[2][8] = {{-1, -1, -1, 0, 0, 1, 1, 1}, {-1, 0, 1, -1, 1, -1, 0, 1}};
bool is_in_field(int y, int x, int h, int w) {
  return (y >= 0 && y < h && x >= 0 && x < w);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  int n;
  cin >> n;
  int h = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (h > a)
      ans += h - a;
    else
      h = a;
  }
  cout << ans << endl;
}
