#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
const long long llinf = 0x3f3f3f3f3f3f3f3f;
const long long mod = 1000000007;
int vis[maxn];
long long b[maxn];
struct node {
  long long a;
  int id;
} a[maxn];
bool cmp(node a, node b) { return a.a < b.a; }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) vis[i] = 0;
  long long maxx = 0;
  int index = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &a[i].a);
    b[i] = a[i].a;
    if (maxx <= a[i].a || a[i].a > a[i - 1].a) {
      maxx = a[i].a;
      index = i;
    }
    a[i].id = index;
  }
  sort(a + 1, a + 1 + n, cmp);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int tem = a[i].id;
    if (a[i].a < b[tem] && vis[tem] == 0) {
      vis[tem] = 1;
      ans += b[tem] - a[i].a;
    }
  }
  printf("%I64d\n", ans);
}
