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