#include <bits/stdc++.h>
using namespace std;
int main() {
  string str;
  getline(cin, str, '\n');
  int N = atoi(str.c_str());
  int sum = 0;
  int cnt = 0;
  int h = 0;
  std::string buf;
  while (getline(cin, buf, ' ')) {
    if (cnt == 0) {
      h = atoi(buf.c_str());
    } else {
      int aCurrent = atoi(buf.c_str());
      int springboardHeight = 0;
      if (h > aCurrent) {
        springboardHeight = h - aCurrent;
      } else {
        h = aCurrent;
      }
      sum += springboardHeight;
    }
    cnt++;
  }
  cout << sum << endl;
  return 0;
}
