#include <bits/stdc++.h>   #pragma comment(linker,  /stack:64000000 )   using namespace std;   const double PI = 2 * acos(0.0);   const double eps = 1e-9;   template <class T>   T jog(T a, T b) {     return a + b;   }   template <class T>   T bog(T a, T b) {     return a - b;   }   template <class T>   T gon(T a, T b) {     return a * b;   }   template <class T>   T sq(T x) {     return x * x;   }   template <class T>   T gcd(T a, T b) {     return b == 0 ? a : gcd(b, a % b);   }   template <class T>   T lcm(T a, T b) {     return (a / gcd(a, b)) * b;   }   template <class T>   T power(T a, T p) {     T res = 1, x = a;     while (p) {       if (p & 1) res = res * x;       x = x * x;       p >>= 1;     }     return res;   }   template <class T>   T cordinatlenth(T a, T b, T c, T d) {     return sqrt(sq(a - c) + sq(b - d));   }   template <class T>   string toString(T n) {     ostringstream oss;     oss << n;     oss.flush();     return oss.str();   }   int toInt(string s) {     int r = 0;     istringstream sin(s);     sin >> r;     return r;   }   long long bigmod(long long a, long long p, long long mod) {     long long res = 1, x = a;     while (p) {       if (p & 1) res = (res * x) % mod;       x = (x * x) % mod;       p >>= 1;     }     return res;   }   int diraction1[] = {-1, 0, 0, 1, 1, -1, -1, 1};   int diraction2[] = {0, -1, 1, 0, 1, -1, 1, -1};   int horsed1[] = {-2, -2, -1, 1, 2, 2, 1, -1};   int horsed2[] = {1, -1, -2, -2, -1, 1, 2, 2};   void input();   const int INF = 2147483647;   const long long LINF = 9223372036854775807ll;   int n, in[1000010];   int main() {     input();     return 0;   }   void input() {     memset(in, 0, sizeof(in));     cin >> n;     for (int i = 0, tmp; i < n; i++) {       scanf( %d , &tmp);       in[tmp]++;     }     long long ans = 0;     for (int i = 0; i < 1000005; i++) {       if (in[i] == 0 || in[i] == 1) continue;       while (in[i] > 1) {         ans++;         in[i + 1]++;         in[i]--;       }     }     cout << ans << endl;   }   