#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int MAX32 = 2147483647;
const double PI = 3.14159265358979323846;
int main() {
  int n, a, sum = 0, maxim = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    maxim = max(a, maxim);
    sum += maxim - a;
  }
  cout << sum << endl;
}
