#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll inf = 1e18;
using vb = vector<bool>;
using vc = vector<char>;
using vd = vector<double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
int a[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int b[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
inline ll power(ll x, ll y) {
  ll temp;
  if (y == 0) return 1;
  temp = power(x, y / 2);
  if (y % 2 == 0)
    return (temp * temp);
  else
    return (x * temp * temp);
}
bool sortbysec(pll &a, pll &b) { return (a.second < b.second); }
signed main() {
  ll n;
  cin >> n;
  vll v(n);
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
  }
  ll x = (*max_element((v).begin(), (v).end()));
  ll ans = 0;
  ll p;
  for (ll i = 0; i < n; i++) {
    if (v[i] == x) {
      p = i;
      break;
    }
  }
  for (ll i = p + 1; i < n; i++) {
    ans += abs(x - v[i]);
  }
  for (ll i = 0; i < p - 1; i++) {
    if (v[i] > v[i + 1]) {
      ans += abs(v[i] - v[i + 1]);
    }
  }
  cout << ans << "\n";
}
