#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  scanf("%d", &n);
  vector<int> heights(n, 0);
  for (int i = 0; i < n; i++) {
    scanf("%d", &heights[i]);
  }
  int ans = 0;
  int m_height = heights[0];
  for (int i = 1; i < n; i++) {
    if (m_height > heights[i]) {
      ans += m_height - heights[i];
      heights[i] = m_height;
    } else {
      m_height = heights[i];
    }
  }
  cout << ans << "\n";
}
