#include <bits/stdc++.h>
using namespace std;
void c_p_c() {}
void solution() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  int ans = 0;
  int Max = INT_MIN;
  for (int i = 0; i < n; i++) {
    Max = max(Max, arr[i]);
    ans += Max - arr[i];
  }
  cout << ans << endl;
}
int32_t main() {
  c_p_c();
  solution();
  return 0;
}
