#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int max_A = 0;
  int ans = 0;
  cin >> N;
  vector<int> A(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++) {
    max_A = max(max_A, A.at(i));
    ans += max_A - A.at(i);
  }
  cout << ans << endl;
  return 0;
}
