#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000007;
const long long mod = 1000000007;
const double PI = 3.14159265358979323846;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int cans(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
bool check(string s) {
  int dig = 0;
  for (auto itr : s) {
    dig += (itr - '0');
    dig %= 9;
  }
  return dig % 9 == 0;
}
int main() {
  int N;
  int cost = 0;
  int pre;
  cin >> N;
  cin >> pre;
  for (int i = 0; i < (N - 1); i++) {
    int h;
    cin >> h;
    cost += max(0, pre - h);
    pre = max(h, pre);
  }
  cout << cost << endl;
  return 0;
}
