#include <cmath>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
sort(arr,arr+n);
int count=0;
int start=0,last=n-1;
bool flag=false;
while(last>start)
{
flag=false;
while(arr[last]-arr[start]>1)
{
arr[start]=arr[start]+1;
arr[last]=arr[last]-1;
count++;
if(arr[last]<arr[last-1])
{
last--;
flag=true;
}
if(arr[start+1]<arr[start])
{
start++;
flag=true;
}
if(flag)
break;
}
if(!flag)
{
last--;
start++;
}
}
cout<<count<<endl;
return 0;
}
I2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8dW5vcmRlcmVkX21hcD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIAp7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGludCBuOwogICAgY2luPj5uOwogICAgaW50IGFycltuXTsKCiAgICBmb3IoaW50IGk9MDtpPG47aSsrKQogICAgICAgIGNpbj4+YXJyW2ldOwoKICAgIHNvcnQoYXJyLGFycituKTsKCiAgICBpbnQgY291bnQ9MDsKICAgIGludCBzdGFydD0wLGxhc3Q9bi0xOwogICAgYm9vbCBmbGFnPWZhbHNlOwogICAKICAgIHdoaWxlKGxhc3Q+c3RhcnQpCiAgICB7CiAgICAgICAgZmxhZz1mYWxzZTsKICAgICAgICB3aGlsZShhcnJbbGFzdF0tYXJyW3N0YXJ0XT4xKQogICAgICAgIHsKICAgICAgICAgICAgYXJyW3N0YXJ0XT1hcnJbc3RhcnRdKzE7CiAgICAgICAgICAgIGFycltsYXN0XT1hcnJbbGFzdF0tMTsKICAgICAgICAgICAgY291bnQrKzsKICAgICAgICAgICAgaWYoYXJyW2xhc3RdPGFycltsYXN0LTFdKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBsYXN0LS07CiAgICAgICAgICAgICAgICBmbGFnPXRydWU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYoYXJyW3N0YXJ0KzFdPGFycltzdGFydF0pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHN0YXJ0Kys7CiAgICAgICAgICAgICAgICBmbGFnPXRydWU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYoZmxhZykKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgICAgICBpZighZmxhZykKICAgICAgICB7ICAgCiAgICAgICAgICAgIGxhc3QtLTsKICAgICAgICAgICAgc3RhcnQrKzsKICAgICAgICB9CiAgICB9CiAgICBjb3V0PDxjb3VudDw8ZW5kbDsKICAgIHJldHVybiAwOwp9Cg==