#include <bits/stdc++.h>
using namespace std;
int a = 0, b = 0, c = 0;
signed main() {
  cin >> a;
  vector<int> A(a);
  for (int i = 0; i < a; i++) {
    cin >> A.at(i);
  }
  for (int i = 0; i < a; i++) {
    b = max(b, A[i]);
    if (A[i] >= b) {
      c += 0;
    } else {
      c += b - A[i];
    }
  }
  cout << c << endl;
}
