#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   int t;   int n;   struct node {     long long id, val;     bool flag;     friend bool operator<(node a, node b) {       if (a.val == b.val) return a.flag == false;       return a.val > b.val;     }   };   int main() {     while (~scanf( %d , &n)) {       priority_queue<node> q;       long long num;       scanf( %lld , &num);       node nxt;       nxt.id = 1, nxt.val = num, nxt.flag = false;       q.push(nxt);       long long ans = 0;       for (int i = 2; i <= n; ++i) {         scanf( %lld , &num);         nxt.flag = false;         if (q.top().val < num) {           node now = q.top();           q.pop();           ans = ans + num - now.val;           if (now.flag) {             now.flag = false;             q.push(now);           }           nxt.flag = true;         }         nxt.id = i;         nxt.val = num;         q.push(nxt);       }       printf( %lld n , ans);     }     return 0;   }   