#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> v(N);
  for (int i = 0; i < N; i++) {
    cin >> v.at(i);
  }
  long long ans = 0;
  int Max = 0;
  for (int i = 0; i < N; i++) {
    Max = max(Max, v.at(i));
    int Bi = Max;
    ans += Bi - v.at(i);
  }
  cout << ans << endl;
}
