#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n;
  cin >> n;
  long long int arr[n];
  long long int max_ele = INT_MIN;
  for (long long int i = 0; i < n; i++) {
    cin >> arr[i];
    max_ele = max(max_ele, arr[i]);
  }
  vector<long long int> store;
  long long int ans = 0;
  for (long long int i = 0; i < n - 1; i++) {
    if (arr[i] < arr[i + 1]) store.push_back(arr[i]);
  }
  for (long long int i = 0; i < store.size(); i++)
    ans += abs(max_ele - store[i]);
  cout << ans << "\n";
  return 0;
}
