#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  vector<int> A(1000000001);
  int max_A = 0;
  int ans = 0;
  cin >> N;
  N = N - 1;
  for (int i = 0; i <= N; i++) {
    cin >> A.at(i);
  }
  max_A = A.at(0);
  for (int i = 1; i <= N; i++) {
    if (max_A > A.at(i)) {
      ans += max_A - A.at(i);
    } else {
      max_A = A.at(i);
    }
  }
  cout << ans << endl;
  return 0;
}
