#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int N = 1e5 + 4;
int main() {
  int n, mx = 0;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int val;
    scanf("%d", &val);
    mx = max(mx, val);
    ans += (mx - val);
  }
  cout << ans;
  return 0;
}
