#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a;
  int zuidazhi = 0;
  int sum = 0;
  cin >> n;
  while (n > 0) {
    cin >> a;
    if (a < zuidazhi)
      sum += zuidazhi - a;
    else
      zuidazhi = a;
    n--;
  }
  cout << sum;
  return 0;
}
