#include <bits/stdc++.h>
using namespace std;
long long ar[100005];
int main() {
  long long a, b, c, d, e, x, y, z;
  cin >> z;
  for (a = 0; a < z; a++) {
    cin >> ar[a];
  }
  x = 0;
  d = 0;
  for (a = 0; a < z - 1; a++) {
    if (ar[a] > ar[a + 1]) x = x + ar[a] - ar[a + 1];
  }
  cout << x << endl;
  return 0;
}
