#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int maxHeight = 0;
  int totalHeight = 0;
  while (N--) {
    int height;
    cin >> height;
    if (height >= maxHeight) {
      maxHeight = height;
    } else if (height < maxHeight) {
      totalHeight += maxHeight - height;
    }
  }
  cout << totalHeight;
  return 0;
}
