#include <bits/stdc++.h>
using namespace std;
int main() {
  {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
  };
  int n;
  cin >> n;
  long long int a[n];
  long long int sum = 0, sum1 = 0;
  long long int ans = 0, t;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ans = max(ans, a[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == ans) {
      t = i;
    }
  }
  for (int i = 1; i <= t - 1; i++) {
    if (a[i] > a[i + 1]) {
      sum += (a[i] - a[i + 1]);
    } else {
      continue;
    }
  }
  for (int i = t + 1; i <= n; i++) {
    sum1 += (ans - a[i]);
  }
  if (t == n) {
    cout << sum << endl;
  } else {
    cout << sum + sum1 << endl;
  }
  return 0;
}
