#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 5;
const long long MOD = 1e9 + 7;
int main() {
  int n;
  long long sol = 0;
  int maxx = 0;
  for (int i = 0; i < n; i++) {
    int first;
    cin >> first;
    if (first > maxx)
      maxx = first;
    else
      sol += maxx - first;
  }
  cout << sol << endl;
  return 0;
}
