#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> heights(n);
  for (int& x : heights) {
    scanf("%d", &x);
  }
  int max_h = heights[0];
  int answer = 0;
  for (int i = 1; i < n; ++i) {
    if (heights[i] > max_h) {
      max_h = heights[i];
    } else {
      answer += (max_h - heights[i]);
    }
  }
  printf("%d\n", answer);
  return 0;
}
