#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int sol = 0;
  int maxi = 0;
  cin >> maxi;
  for (int i = 1; i < n; i++) {
    int tmp;
    cin >> tmp;
    sol += max(maxi - tmp, 0);
    maxi = max(maxi, tmp);
  }
  cout << sol;
}
