#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  t = 1;
  while (t--) {
    long long n, maxx = 0, sum = 0;
    cin >> n;
    long long a[n];
    for (int i = 0; i <= n - 1; i++) {
      cin >> a[i];
      maxx = max(maxx, a[i]);
    }
    int j = 0;
    for (int i = 0; i <= n - 1; i++) {
      if (a[i] == maxx) {
        j = i;
        break;
      }
    }
    for (int i = j; i <= n - 1; i++) sum += maxx - a[i];
    for (int i = j; i >= 1; i--) {
      if (a[i] < a[i - 1]) sum += a[i - 1] - a[i];
    }
    cout << sum;
  }
  cerr << "Time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
  return 0;
}
