#include <bits/stdc++.h>
using namespace std;
const int mxN = 2 * (1e5);
vector<int> heights;
int main() {
  int n, x;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    heights.push_back(x);
  }
  int curr_max = heights[0];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    curr_max = max(curr_max, heights[i]);
    ans += (curr_max - heights[i]);
  }
  cout << ans;
}
