#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  int a;
  int b = 0;
  int ans = 0;
  cin >> n;
  for (int i = 0; i < (int)(n); i++) {
    cin >> a;
    if (b < a) {
      b = a;
    }
    if (b > a) {
      ans = ans + b - a;
    }
  }
  cout << ans << endl;
}
