#include <bits/stdc++.h>
using namespace std;
int main() {
  int c = 0;
  int a;
  cin >> a;
  vector<int> b(a);
  for (int i = 0; i < a; i++) {
    cin >> b.at(i);
  }
  int d = b.at(0);
  for (int i = 0; i < a; i++) {
    if (b.at(i) >= d) {
      int d = b.at(i);
    } else if (b.at(i) < d) {
      int e = d - b.at(i);
      cout << e << endl;
      c += e;
    }
  }
  cout << c << endl;
}
