#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 * 2;
long long n;
int arr[maxn];
int arr2[maxn];
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    arr2[i] = arr[i];
  }
  sort(arr2, arr2 + n);
  int index, largest = arr2[n - 1];
  for (int i = 0; i < n; i++)
    if (arr[i] == largest) {
      index = i;
      break;
    }
  int ans = 0;
  for (int i = index + 1; i < n; i++) {
    ans += largest - arr[i];
  }
  for (int i = 1; i < index; i++) {
    if (arr[i] < arr[i - 1]) ans += arr[i - 1] - arr[i];
  }
  cout << ans << endl;
  return 0;
}
