#include <bits/stdc++.h>
using namespace std;
int num[200003];
int main() {
  for (int i = 1; i < 200003; i++) num[i] = 0;
  int a = 0, b = 0, x = 0, y = 0, z = 0;
  long long c = 0;
  scanf("%d", &a);
  for (int i = 1; i <= a; i++) {
    scanf("%d", &num[i]);
  }
  b = num[1];
  c = 0;
  for (int i = 2; i <= a; i++) {
    x = 0;
    if (num[i] >= b) {
      b = num[i];
    } else {
      x = b - num[i];
      c += x;
    }
  }
  printf("%d", c);
}
