#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  int min_idx = 0, max_idx = 0;
  int min_val = INT_MAX, max_val = INT_MIN;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] <= min_val) {
      min_val = a[i];
      min_idx = i;
    }
    if (a[i] >= max_val) {
      max_val = a[i];
      max_idx = i;
    }
  }
  long long ans = 0;
  for (int i = max_idx + 1; i < n; i++) {
    ans += (max_val - a[i]);
  }
  for (int i = max_idx - 1; i >= 0; i--) {
    ans += max(-a[i] + a[i - 1], 0);
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tc = 1;
  while (tc--) {
    solve();
  }
  return 0;
}
