Problem:
Score : 200 points

Problem Statement

There are K pieces of cakes.
Mr. Takahashi would like to eat one cake per day, taking K days to eat them all.

There are T types of cake, and the number of the cakes of type i (1 ≤ i ≤ T) is a_i.

Eating the same type of cake two days in a row would be no fun,
so Mr. Takahashi would like to decide the order for eating cakes that minimizes the number of days on which he has to eat the same type of cake as the day before.

Compute the minimum number of days on which the same type of cake as the previous day will be eaten.

Constraints

1 ≤ K ≤ 10000

1 ≤ T ≤ 100

1 ≤ a_i ≤ 100

a_1 + a_2 + ... + a_T = K

Input

The input is given from Standard Input in the following format:

K T
a_1 a_2 ... a_T

Output

Print the minimum number of days on which the same type of cake as the previous day will be eaten.

Sample Input 1

7 3
3 2 2

Sample Output 1

0

For example, if Mr. Takahashi eats cakes in the order of 2, 1, 2, 3, 1, 3, 1, he can avoid eating the same type of cake as the previous day.

Sample Input 2

6 3
1 4 1

Sample Output 2

1

There are 6 cakes.
For example, if Mr. Takahashi eats cakes in the order of 2, 3, 2, 2, 1, 2, he has to eat the same type of cake (i.e., type 2) as the previous day only on the fourth day.
Since this is the minimum number, the answer is 1.

Sample Input 3

100 1
100

Sample Output 3

99

Since Mr. Takahashi has only one type of cake, he has no choice but to eat the same type of cake as the previous day from the second day and after.

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Text;
using System.Collections.Generic;
using System.Collections;
using static System.Math;
using System.Linq;
using static System.Array;

public class HelloWorld
{
    static public void Main ()
    {
        //Console.WriteLine(1);
        var KT = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
        
        var K = KT[0];
        var T = KT[1];
        //Console.WriteLine(1);
        var A = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
        //Console.WriteLine(1);
        var pq = new PriorityQueue<long>(4000,true);
        //Console.WriteLine(1);
        for(int i = 0;i < T;i++){
            pq.Enqueue(A[i]);
           // Console.WriteLine(pq.Count);
        }
        //Console.WriteLine(1);
        var top = pq.Dequeue();
       
        while(pq.Count > 0){
            
            
            var next = pq.Dequeue();
            top--;
            next--;
            if(top > 0){
                pq.Enqueue(top);
            }
            
            if(next > 0){
                pq.Enqueue(next);
            }
            
            if(pq.Count > 0){
                top = pq.Dequeue();
            }else{
                top = 1;
            }
            
            
        }
        
        Console.WriteLine(top-1);
    }
    
    public static long RepPow(long x, long p, long m = 998244353){
        if (p == 1) return x % m;
        long b = RepPow(x, p / 2, m) % m; if (p % 2 == 0) { return (b * b) % m; } else { return (x * ((b * b) % m)) % m; }}
        static long Pow(long n, long p, int mod = 998244353) { if (p == 0) return 1;　else if (p % 2 == 0) { var x = Pow(n, p / 2, mod); return (x * x) % mod; } else { return Pow(n, p - 1, mod) * n % mod; } }
        

}

class PriorityQueue<T> where T : IComparable<T> { public readonly T[] Array; public readonly bool Greater; public int Count { get; private set; } = 0; public PriorityQueue(long capacity, bool greater = true) { this.Array = new T[capacity]; this.Greater = greater; } public void Enqueue(T item) { this.Array[this.Count] = item; this.Count += 1; this.UpHeap(); } public T Dequeue() { 
// 先頭要素を末尾にして再構成 
this.Swap(0, this.Count - 1); this.Count -= 1; this.DownHeap(); return this.Array[this.Count]; } private void UpHeap() { var n = this.Count - 1; while (n != 0) { // 親要素の座標 
var parent = (n - 1) / 2; if (this.Compare(this.Array[n], this.Array[parent]) > 0) { this.Swap(n, parent); n = parent; } else { break; } } } private void DownHeap() { var parent = 0; while (true) { var child = 2 * parent + 1; if (child > this.Count - 1) break; if (child < this.Count - 1 && this.Compare(this.Array[child], this.Array[child + 1]) < 0) { // 左より右の子のほうが大きい値の場合、右を入れ替え対象にする
 child += 1; } if (this.Compare(this.Array[parent], this.Array[child]) < 0) { this.Swap(parent, child); parent = child; } else { break; } } } private int Compare(T a, T b) { var c = a.CompareTo(b); if (!this.Greater) { c = -c; } return c; } private void Swap(int a, int b) { var tmp = this.Array[a]; this.Array[a] = this.Array[b]; this.Array[b] = tmp; } }