#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> height(N);
  for (int i = 0; i < N; i++) cin >> height[i];
  int ma = 0, su = 0;
  for (int i = 0; i < N; i++) {
    if (height[i] > ma) ma = height[i];
    su += ma - height[i];
  }
  cout << su << endl;
}
