import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'dart:math';

Stream<String> get lineStream =>
    stdin.transform(utf8.decoder).transform(const LineSplitter());

Future<List<String>> readLines() async {
  final lines = <String>[];
  await for (var line in lineStream) {
    lines.add(line);
  }
  return lines;
}

void main() async {
  final lines = await readLines();
  final n = int.parse(lines[0]);
  final a = lines[1].split(' ').map((e) => int.parse(e)).toList();
  var d = 0;
  var sum = 0;
  for (var i = 1; i < n; i++) {
    final diff = a[i] - a[i - 1];
    if (diff < 0) {
      sum += diff.abs();
      a[i] = a[i] + diff.abs();
    }
  }
  print(sum + d);
}
