#include <bits/stdc++.h>
int main(void) {
  int n;
  int a[200000] = {0};
  scanf("%d", &n);
  int count = 0;
  int c = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  for (int j = 0; j < n - 1; ++j) {
    for (int k = j + 1; k < n; ++k) {
      if (a[j] > a[k]) {
        count += a[j] - a[k];
        if (k == n - 1) {
          ++c;
          break;
        }
      }
      if (a[j] < a[k]) {
        j = k - 1;
        break;
      }
    }
    if (c == 1) {
      break;
    }
  }
  printf("%d\n", count);
}
