#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int maxi = 0;
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int aux;
    cin >> aux;
    if (aux < maxi) {
      ans += maxi - aux;
    } else
      maxi = aux;
  }
  cout << ans << "\n";
}
