#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int sum[10000000];
  int s;
  int flag = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> sum[i];
    if (sum[i] < sum[i - 1]) {
      if (flag == 0) {
        s = sum[i] - sum[i - 1];
        sum[i - 1] = sum[i - 1] + s;
        flag++;
      } else {
        s = sum[i] - sum[i - 1];
        sum[i - 1] = sum[i - 1] + s;
        s += s;
      }
    }
  }
  cout << s << endl;
}
