#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> v(n), srt(n);
  int mx = 0, in = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    if (mx < v[i]) {
      mx = v[i];
      in = i;
    }
  }
  long long int ans = 0, ans2 = 0;
  for (int i = in + 1; i < n; i++) {
    ans += (mx - v[i]);
  }
  for (int i = 1; i < in; i++) {
    if (v[i - 1] > v[i]) {
      ans += v[i - 1] - v[i];
    }
  }
  for (int i = 0; i < in; i++) {
    ans2 += (mx - v[i]);
  }
  for (int i = n - 1; i > in + 1; i--) {
    if (v[i] > v[i - 1]) {
      ans2 += v[i] - v[i - 1];
    }
  }
  cout << min(ans, ans2) << endl;
  return 0;
}
