#include <bits/stdc++.h>
using namespace std;
int N, ans, mx;
int main() {
  cin >> N;
  int arr[N];
  for (int i = 0; i < N; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < N; i++) {
    mx = max(mx, arr[i]);
    int hell = mx;
    ans += hell - arr[i];
  }
  cout << ans;
  return 0;
}
