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 (Python):
K, T = map(int, input().split())
A = list(map(int, input().split()))
d = [[i, c] for i, c in enumerate(A)]

if len(d) == 1:
    print(K-1)
    exit()

s = ''
ans = 0
pre = None
for i in range(K):
    d = sorted(d, key=lambda x: -x[1])
    if pre == d[0][0]:
        if pre != d[1][0] and d[1][1] > 0:
            d[1][1] -= 1
            pre = d[1][0]
            s += str(pre)
        else:
            d[0][1] -= 1
            pre = d[0][0]
            s += str(pre)
            ans += 1
    else:
        d[0][1] -= 1
        pre = d[0][0]
        s += str(pre)
print(ans)

Solution (Python):
k, t = map(int, input().split())
a = list(map(int,input().split()))
m = max(a)
s = sum(a) - m
if m <= s:
    print(0)
else:
    print(m - s - 1)

Solution (Python):
K, T = map(int, input().split())
A = list(map(int, input().split()))
ma = max(A)
rem = K-ma
print(max(0, ma-rem-1))

Solution (Python):
K, T = map(int, input().split())
A = list(map(int, input().split()))

print(max(0, max(A) - (K-max(A)+1)))

Solution (Python):
k,t=map(int,input().split())
lists=list(map(int,input().split()))
MAX=max(lists)
sums=sum(lists)
print(max(2*MAX-1-sums,0))

Solution (Python):
k, t = map(int, input().split())
a = sorted(list(map(int, input().split())))
print(max(a[-1]-sum(a[:-1])-1, 0))

Solution (Python):
K, T = map(int, input().split())
A = list(map(int, input().split()))
A_max = max(A) 

ans = 0
half_K = (K + 1) // 2
if A_max <= half_K:
  print(ans)
  quit()
else:
  A_ex = K - A_max + 1
  K_syo = A_max // A_ex
  K_amari = A_max % A_ex
  ans += (K_syo) * K_amari
  ans += (K_syo - 1) * (A_ex - K_amari)
  print(ans)

Solution (Python):
k, t = list(map(int, input().split()))
a = list(map(int, input().split()))
a_max = max(a)
print(max(a_max - 1 - (k - a_max), 0))

Solution (Python):
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    k, t = map(int, input().split())
    a = list(map(int, input().split()))
    if t == 1:
        print(a[0] - 1)
    else:
        maxa = max(a)
        hoka = sum(a) - maxa
        if maxa <= hoka:
            print(0)
        else:
            print(maxa - hoka - 1)

if __name__ == '__main__':
    main()

Solution (Python):
K, T = map(int, input().split())
A = list(map(int, input().split()))
m = max(A)
print(max(0, 2*m-1-K))

Solution (Python):
k,t=map(int,input().split())
a=list(map(int,input().split()))
x=sum(a)-max(a)
print(max(0,max(a)-1-x))

Solution (Python):
K, T = map(int, input().split())
A = list(map(int, input().split()))
maxA = max(A)
ans = max(maxA - 1 - (K - maxA), 0)
print(ans)

Solution (Python):
K,T=map(int,input().split())
A=list(map(int,input().split()))

if T==1:
  print(A[0]-1)
  exit()
  
ans=abs(A[0]-A[1])-1
k=A[0]+A[1]
for i in range(2,T):
  if ans>=A[i]:
    ans-=A[i]
  else:
    ans=max(A[i]-k-1,0)
  k+=A[i]  

print(ans)

Solution (Python):
a,b = map(int,input().split())
an = [int(i) for i in input().split()]
print(max(max(an)*2-sum(an)-1,0))

Solution (Python):
K,T = map(int,input().split())
A = list(map(int,input().split()))

if T == 1:
  print(A[0]-1)
else:
  A.sort()
  print(max(A[-1]-1-sum(A[:T-1]),0))

Solution (Python):
k, t = map(int, input().split())
a = [[int(x), i] for i, x in enumerate(input().split())]
pc = -1
while(True):
    a.sort(reverse=True)
    for i in range(t):
        if a[i][0] > 0 and a[i][1] != pc:
            a[i][0] -= 1
            pc = a[i][1]
            break
    else: break
print(sum([x for x, _ in a]))

Solution (Python):
k,t = map(int,input().split())
a= list(map(int,input().split()))
sa = sum(a)
ma = max(a)

print(max(ma-(sa-ma)-1,0))

Solution (Python):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

K, T = list(map(int, sys.stdin.readline().split()))
A = list(map(int, sys.stdin.readline().split()))


def argmax(except_i=None):
    m = 0
    ret = -1
    for i, a in enumerate(A):
        if a > m and i != except_i:
            ret = i
            m = a
    return ret


prev = argmax()
A[prev] -= 1

ans = 0
for _ in range(K - 1):
    i = argmax(prev)
    if i < 0:
        ans += 1
        A[prev] -= 1
    else:
        A[i] -= 1
        prev = i
print(ans)

Solution (Python):
from sys import stdin
import heapq
K,T = [int(x) for x in stdin.readline().rstrip().split()]
data = [(-int(b),a) for a,b in enumerate(stdin.readline().rstrip().split())]
heapq.heapify(data)
ans = 0
cake_num = -1
cake_count = 0
flag = True
 
if T == 1:
  ans = K-1
else:
  while flag:
    c_c,c_n = heapq.heappop(data)
    if c_n == cake_num:
        c_c2,c_n2 = heapq.heappop(data)
        if c_c2 == 0:
            ans = abs(c_c)
            flag = False
        else:
            heapq.heappush(data,(c_c2+1,c_n2))
        cake_num = c_n2
        heapq.heappush(data,(c_c,c_n))
        
    else:
        heapq.heappush(data,(c_c+1,c_n))
        cake_num = c_n
    if c_c == 0:
      ans = 0
      flag = False
    
print(ans)

Solution (Python):
from sys import stdin

K,T = [int(x) for x in stdin.readline().rstrip().split()]
data = [(int(b),a) for a,b in enumerate(stdin.readline().rstrip().split())]

ans = 0
cake_num = -1
flag = True
 
if T == 1:
    ans = K-1
else:
    while flag:
        data.sort()
        
        c_c,c_n = data.pop()
        
        if c_n == cake_num:
            c_c2,c_n2 = data.pop()
            if c_c2 == 0:
                ans = c_c
                flag = False
            else:
                data.append((c_c2-1,c_n2))
            data.append((c_c,c_n))
            cake_num = c_n2
            
        else:
            data.append((c_c-1,c_n))
            cake_num = c_n
        if c_c == 0:
            ans = 0
            flag = False
        
print(ans)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int,input().split()))
a_sum = sum(a)
a_max = max(a)
x = a_max - (a_sum - a_max) - 1
print(x if x > 0 else 0)

Solution (Python):
K, T = map(int, input().split())
A = list(map(int, input().split()))
maxA = max(A)
ans = maxA - 1 - (K - maxA)
print(max(ans, 0))

Solution (Python):
from heapq import heapify, heappop, heappush

K, T = map(int, input().split())
A = [-a for a in map(int, input().split())]

heapify(A)
while len(A) > 1:
    a, b = -heappop(A), -heappop(A)
    heappush(A, -(a - b))

print(max(0, -A[0] - 1))

Solution (Python):
k,t=map(int,input().split())
l=list(map(int,input().split()))
m=max(l)
n=sum(l)-m
if m<=n+1:
    print(0)
else:
    print(m-n-1)

Solution (Python):
k,t = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
if k >= a[0]*2:
    print(0)
else:
    print(a[0]*2-k-1)

Solution (Python):


K,T=map(int,input().split())
A=list(map(int,input().split()))

all=sum(A)
high=max(A)

print(max(0,high-(all-high)-1))

Solution (Python):
K, T = map(int,input().split())
List = [int(i) for i in list(input().split())]
List.sort(reverse = True)

Answer = max(List[0]-1-(K-List[0]), 0)

print(Answer)

Solution (Python):
K,T=map(int,input().split())
A=list(map(int,input().split()))
M=max(A)
print(max(M-1-(K-M),0))

Solution (Python):
k,t=map(int,input().split())
a=list(map(int,input().split()))
b=max(a)

if k%2==0:
    if 0<=2*(b-k//2)-1:
        print(2*(b-k//2)-1)
    else:
        print(0)

else:
    if 0<=2*(b-(k+1)//2):
        print(2*(b-(k+1)//2))
    else:
        print(0)

Solution (Python):
k, t, *a = list(map(int, open(0).read().split()))

最大 = max(a)
print(max(最大-(k-最大)-1, 0))

Solution (Python):
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

k,n = inpl()
a = inpl()
a.sort()
print(max(0, a[-1]*2 - k - 1))

Solution (Python):
k, t = map(int, input().split())
a = sorted(list(map(int, input().split())))

temp = []
temp.append(a.pop())
temp[-1] -= 1

while sum(a) != 0:
    a = sorted(a)
    temp.append(a.pop())
    temp[-1] -= 1
    a.append(temp.pop(0))

print(temp[0])

Solution (Python):

import collections
k, t = map(int,input().split())
a = list(map(int, input().split()))

mode = max(a)

ans = max(0, (k-1) - (k-mode)*2)
print(ans)

Solution (Python):
K, T = tuple(map(int, input().split()))
A = list(map(int, input().split()))
max_val = max(A)
print(max(max_val - 1 -(K - max_val), 0))

Solution (Python):
K,T = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

A.sort(reverse=True)

print(max(A[0]-1-(K-A[0]),0))

Solution (Python):
import heapq

k, t = map(int, input().split())
a = list(map(int, input().split()))

q = []
for i in range(t):
    heapq.heappush(q, (a[i] * -1, i))

cakes = [-1]
for _ in range(k):
    count1, cake1 = heapq.heappop(q)
    if cakes[-1] == cake1 and len(q) > 0:
        count2, cake2 = heapq.heappop(q)
        cakes.append(cake2)

        heapq.heappush(q, (count1, cake1))
        if count2 + 1 < 0:
            heapq.heappush(q, (count2 + 1, cake2))
    else:
        cakes.append(cake1)
        if count1 + 1 < 0:
            heapq.heappush(q, (count1 + 1, cake1))

ans = sum(1 for i in range(len(cakes) - 1) if cakes[i] == cakes[i + 1])
print(ans)

Solution (Python):
K,T=map(int,input().split())
A=list(map(int,input().split()))

if max(A)-(sum(A)-max(A))<=1:
  print(0)
else:
  print(max(A)-(sum(A)-max(A))-1)

Solution (Python):


K, T = map(int, input().split())
A = list(map(int, input().split()))

if len(A) == 1:
    print(A[0] - 1)
else:
    A.sort()
    max_num = A.pop()
    tmp = max_num - sum(A)
    if tmp > 1:
        print(tmp - 1)
    else:
        print(0)

Solution (Python):
K, T = map(int, input().split())
a = list(map(int, input().split()))

a.sort()
sum_a = sum(a[:-1])
ans = a[-1] - sum_a - 1

print(max(ans, 0))

Solution (Python):
k,t=map(int,input().split())
A=[int(i) for i in input().split()]
a=max(A)
if a<=(k-1)//2+1:
  print(0)
else:
  if k%2==1:
    print(2*(a-((k-1)//2+1)))
  else:
    print(2*(a-((k-1)//2+1))-1)

Solution (Python):
K,T=map(int,input().split())
a=list(map(int,input().split()))

temp=sum(a)-max(a)
print(max(max(a)-temp-1,0))

Solution (Python):
def main():

    K, T = map(int, input().split())
    A = list(map(int, input().split()))
    A.sort(reverse = True)
    if K % 2 == 1 and A[0] > (K+1)//2:
        return (A[0] - (K+1)//2) * 2
    elif K % 2 == 0 and A[0] > K  // 2:
        if A[0] - K // 2 == 1:
            return 1
        else:
            return 1 + (A[0] - K // 2 - 1) * 2
    else:
        return 0

if __name__ == '__main__':
    print(main())

Solution (Python):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque




def main():
    k, t = map(int, input().split())
    data = list(map(int, input().split()))
    ans = 0
    ikeru = (k + 1) // 2
    for i in range(t):
        if data[i] > ikeru:
            if k % 2:
                ans += (data[i] - ikeru) * 2
            else:
                ans += (data[i] - ikeru) * 2 - 1
    print(ans)







if __name__ == '__main__':
    main()

Solution (Python):
K, T = map(int, input().split())
As = list(map(int, input().split()))
r = max(0, K-1-(K-max(As))*2)
print(r)

Solution (Python):
k, t = [ int(v) for v in input().split() ]
cake_list = [ int(v) for v in input().split() ]

a = max(cake_list)
print(max(0,2*a-k-1))

Solution (Python):



def main():
    k, t = map(int, input().split())
    a = list(map(int, input().split()))
    max_value = 0
    max_value_index = -1
    ans = 0

    for index, ai in enumerate(a):
        if ai > max_value:
            max_value = ai
            max_value_index = index

    prev = max_value_index
    a[prev] -= 1

    
    
    
    
    for i in range(k - 1):
        max_value = 0
        max_value_index = -1

        
        for j in range(t):
            if a[j] > max_value and j != prev:
                max_value = a[j]
                max_value_index = j

        if max_value_index != -1:
            prev = max_value_index
            a[max_value_index] -= 1
        else:
            a[prev] -= 1
            ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
K,T,*A=map(int,open(0).read().split());print(max(0,(max(A)+-K//2)*2-(K+1)%2))

Solution (Python):
K,T,*A=map(int,open(0).read().split())
if K%2:print(max(0,(max(A)+-K//2)*2))
else:print(max(0,(max(A)+-K//2)*2-1))

Solution (Python):
k,t=map(int,input().split())
a=[int(i) for i in input().split()]

a=sorted(a,reverse=True)

if len(a)==1:
	ans=a[0]-1

else:
	while True:
		if a[1]==0:
			ans=max(a[0]-1,0)
			break
		a[0]-=1
		a[1]-=1
		a=sorted(a,reverse=True)
print(ans)

Solution (Python):
K, T = map(int, input().split())
a = list(map(int, input().split()))
if T == 1:
  print(K-T)
else:
  m = max(a)
  M = K-m+1
  if M >= m:
    print(0)
  else:
    print(m-M)

Solution (Python):
k,t = map(int, input().split())
a=sorted(list(map(int, input().split())))
count=0

while(len(a)>0):
    a[-1]-=1
    if len(a)>1:
        a[-2]-=1
    else:
        count=a[0]
        break
    a=sorted([i for i in a if i>0])
print(count)

Solution (Python):
import math
K, T = map(int, input().split())
a = [int(i) for i in input().split()]
d = max(a) - math.ceil(K/2)
print(max(0,(d-1)*2+1))

Solution (Python):
k, t = map(int, input().split())
a = sorted([int(i) for i in input().split()])
print(0) if a[-1]-1 < sum(a[:-1]) else print(a[-1]-1-sum(a[:-1]))

Solution (Python):
K,T = map(int,input().split())
a = list(map(int,input().split()))

a = sorted(a,reverse = True)
ans = a.pop(0) - sum(a) - 1
if ans >= 0:
    print(ans)
else:
    print(0)

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

k,t = LI()
a = LI()
maxa = max(a)
ans = max(maxa-1-(sum(a)-maxa),0)
print(ans)

Solution (Python):
k,t=map(int,input().split())
a=list(map(int,input().split()))
print(max(max(a)-1-(k-max(a)),0))

Solution (Python):
K, T = map(int, input().split())
a = list(map(int, input().split()))
print(max(max(a) - 1 - (K - max(a)), 0))

Solution (Python):
k, t = map(int, input().split())
a = list(map(int, input().split()))

max_ = max(a)
sum_nokori = sum(a) - max(a)
print(max(max_ - sum_nokori - 1, 0))

Solution (Python):
k,_,*a=map(int,open(0).read().split());print(max(0,2*max(a)+~k))

Solution (Python):
k,t,*a=map(int,open(0).read().split());print(max(0,2*(max(a)+k//-2)-1))

Solution (Python):
K, T = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

a = max(A)
print(max(a - 1 - (K - a), 0))

Solution (Python):

def main():
    
    K, T = tuple(map(int, input().split()))
    A = sorted(map(int, input().split()))
    ans = max(0, A[-1] - 1 - sum(A[:-1]))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def solve(k, t ,a):
    if t == 1:
        return k-1
    Q = [[i, num] for i, num in enumerate(a)]
    prev = -1
    ans = 0
    for i in range(k):
        Q = sorted(Q, key=lambda _:-_[1])
        i1, num1 = Q[0]
        i2, num2 = Q[1]
        if (prev != i1) or (num2 == 0):
            Q[0][1] -= 1
            if prev == i1:
                ans += 1
            prev = i1
        else:
            Q[1][1] -= 1
            prev = i2
    return ans

k, t = map(int, input().split())
a = list(map(int, input().split()))
print(solve(k, t, a))

Solution (Python):
k,t=map(int,input().split());a=list(map(int,input().split()));print(max(0,2*max(a)-1-k))

Solution (Python):
K, T = map(int, input().split())
a = list(map(int, input().split()))
print(max(0, 2 * max(a) - sum(a) - 1))

Solution (Python):
k, t = map(int, input().split())
a = list(map(int, input().split()))

pre = -1
ans = 0
for i in range(k):
    m = 0
    mi = 0
    for j in range(t):
        if m < a[j] and j != pre:
            m = a[j]
            mi = j
    if m == 0:
        a[pre] -= 1
        ans += 1
    else:
        a[mi] -= 1
        pre = mi
print(ans)

Solution (Python):

k,t=map(int,input().split())
a=list(map(int,input().split()))

max_a=max(a)
print(max(max_a-(k-max_a)-1,0))

Solution (Python):


from heapq import heappop,heappush

k,t=map(int,input().split())
a=list(map(int,input().split()))
que=[]
for i in range(t):
    hq=(-a[i],i)
    heappush(que,hq)

pre_v,pre_idx=heappop(que)
hq=(pre_v+1,pre_idx)
heappush(que,hq)

cnt=0

for i in range(k-1):
    v,idx=heappop(que)
    if idx==pre_idx :
        if que:
            hq=(v,idx)
            v,idx=heappop(que)
            heappush(que,hq)    
        else:
            cnt+=-v
            break
    v+=1
    hq=(v,idx)
    pre_v,pre_idx=hq
    if v==0:continue
    heappush(que,hq)

print(cnt)

Solution (Python):
K, T = map(int,input().split())
A = list(map(int,input().split()))

max_a = max(A)
sum_a = sum(A)
print(max(max_a - (sum_a-max_a) -1, 0))

Solution (Python):
k,t=input().split()
print(max(max(map(int,input().split()))*2-int(k)-1,0))

Solution (Python):
k,t = map(int, input().split())
a = list(map(int, input().split()))

b = max(a)

c = k-b


print(max(k-2*c-1,0))

Solution (Python):
K,T=map(int,input().split())
print(max(2*(max(map(int,input().split()))-K//2)-1-K%2,0))

Solution (Python):
K, T = map(int,input().split())
a = max([int(v) for v in input().split()])
print(max(a-1-K+a,0))

Solution (Python):
K,T=map(int,input().split())
a=sorted([int(i) for i in input().split()], reverse=True)

print(max(0,a[0]-sum(a[1:])-1))

Solution (Python):
k, t = map(int, input().split())
a = list(map(int, input().split()))
print(max(0, max(a) * 2 - k - 1))

Solution (Python):
def main():
    K, T = map(int, input().split())
    A = list(map(int, input().split()))
    mx = max(A)
    if K % 2 == 0:
        ans = max((mx - K//2) * 2 - 1, 0)
    else:
        ans = max((mx - K//2 - 1) * 2, 0)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import numpy as np


def input():
    return sys.stdin.readline().rstrip()


def main():
    k, t = [int(e) for e in input().split()]
    aTop = max([int(e) for e in input().split()])
    print(max(0, (aTop * 2) - k - 1))


main()

Solution (Python):
k,t=map(int,input().split())
a=list(map(int,input().split()))
A=max(a)
print(max(A-1-(k-A),0))

Solution (Python):
k, t = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

a.sort()
if a[-1] > sum(a[:t-1])+1:
    print(a[-1]-sum(a[:t-1])-1)
else:
    print(0)

Solution (Python):
K, T = map(int, input().split())
a = sorted(map(int, input().split()), reverse=True)

if T == 1:
    print(K-1)
    exit()

print(max(a[0]-sum(a[1:])-1,0))

Solution (Python):
K, T = map(int, input().split())
*A, = map(int, input().split())
A = sorted(A, reverse=True)
print(max(A[0] - sum(A[1:]) - 1, 0))

Solution (Python):
K,T = map(int,input().split())
A = [int(x) for x in input().split()]
x = max(A)
other = K - x

answer = max(0, x - other - 1)
print(answer)

Solution (Python):
def inpl(): return list(map(int, input().split()))

K, T = inpl()
A = inpl()
ans = K

if T == 1:
    print(K-1)
    exit()

for i in range(T):
    tmp = max(A[i]-(K-A[i]+1), 0)
    OK = False
    for k in range(T):
        if i == k:
            continue
        if (K-A[k]-1) <= 0:
            continue
        OK = True
        tmp = max(tmp, A[k]-(K-A[k]-1))
    if OK:
        ans = min(tmp, ans)

for i in range(T):
    for j in range(i+1, T):
        tmp = max(A[i]-(K-A[i]), 0)
        tmp = max(tmp, A[j]-(K-A[j]))
        OK = False
        for k in range(T):
            if (i == k) or (j == k):
                continue
            if (K-A[k]-1) <= 0:
                continue
            OK = True
            tmp = max(tmp, A[k]-(K-A[k]-1))
        if OK:
            ans = min(tmp, ans)

print(ans)

Solution (Python):
k,t = map(int,input().split())
a = list(map(int,input().split()))
ans = max(a)-(sum(a)-max(a))-1
print(max(0,ans))

Solution (Python):
k,t=map(int,input().split())
a=[int(i) for i in input().split()]
a.sort()
s=sum(a[:-1])
print(max(0,k-s*2-1))

Solution (Python):
K,T = list(map(int, input().split()))
A = list(map(int, input().split()))

n = max(A)

print(max(0,K-2*(K-n)-1))

Solution (Python):

"""
B - K個のケーキ / K Cakes
https://atcoder.jp/contests/code-festival-2016-qualc/tasks/codefestival_2016_qualC_b

"""
import sys


def solve(K, T, cakes):
    if T == 1:
        return K-1
    m = (K + 1)//2
    return max(cakes[-1]-m, 0, cakes[-1]-(K-cakes[-1])-1)


def main(args):
    K, T = map(int, input().split())
    cakes = sorted([int(a) for a in input().split()])
    ans = solve(K, T, cakes)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
K, T = map(int, input().split())
a = list(map(int, input().split()))

print(max(0, max(a)*2 - sum(a) - 1))

Solution (Python):
k,t = map(int,input().split())
a = list(map(int,input().split()))
print(max(0,max(a)*2-sum(a)-1))

Solution (Python):
K,T = [int(i) for i in input().split()]
li = [int(i) for i in input().split()]

max_cake = max(li)
without_cake = sum(li) - max_cake

if (max_cake - without_cake - 1 < 0) :
    print(0)
else :
    print(max_cake - without_cake - 1)

Solution (Python):
k,f =map(int,input().split())
a = list(map(int,input().split()))
a.append(0)
a.sort()
t = a[-1]
s = sum(a[:-1])
if 1 == f:
    print(t-1)
elif t<=s:
    print(0)
else:
    print(t-s-1)

Solution (Python):
k,t=map(int,input().split())
A=[int(i) for i in input().split()]
m=max(A)
print(max(m-1-(k-m),0))

Solution (Python):
K,T=map(int,input().split());print(max(2*max(map(int,input().split()))-K-1,0))

Solution (Python):
K, T = map(int, input().split())
A = list(map(int, input().split()))

mx = max(A)
print(max(2 * mx- K -1 ,0))

Solution (Python):
K,T=map(int,input().split())
*A,=map(int,input().split())
m=max(A)
print(max(0,m-(K-m+1)))

Solution (Python):
k,t = map(int,input().split())
a = list(map(int,input().split()))
M = max(a)
ans = max(2*M -sum(a)-1,0)
print(ans)

Solution (Python):
K,T=map(int,input().split())
A=list(map(int,input().split()))
from heapq import heappush,heappop
from collections import deque

A.sort(reverse=True)
d=deque(A)
d2=deque(A)
ans=A[0]
mod=1
while d:
  if len(d)==1:
    print(d[0]-1)
    exit()
  a=d.pop()
  b=d.popleft()
  if not a==1:
    d.append(a-1)
  if not b==1:
    d.appendleft(b-1)
print(0)

Solution (Python):
K,T=map(int,input().split())
a=list(map(int,input().split()))

if (sum(a)-max(a))+1>=max(a):
    print(0)
else:
    print(max(a)-(sum(a)-max(a)+1))

Solution (Python):
K,T = map(int,input().split())
A = sorted(list(map(int,input().split())))

ans = max(max(A) - sum(A[:-1]) -1 ,0)
print(ans)

Solution (Python):
k,t,*a=map(int,open(0).read().split());print(max(max(a)*2-sum(a),1)-1)

Solution (Python):
k,t,*a=map(int,open(0).read().split());print(max([i+i-k-1for i in a]+[0]))

Solution (Python):
k,t,*a=map(int,open(0).read().split());print(max([i+i-k-1for i in a]+[0]))

Solution (Python):
n,m=map(int,input().split())
print(max(0,max(list(map(int,input().split())))*2-n-1))

Solution (Python):
k,t=map(int,input().split())
l=[int(i) for i in input().split()]
if t==1:
    print(l[0]-1)
    exit()
while 1:
    l.sort(reverse=True)
    if l[1]==0: break
    l[0]-=l[1]
    l[1]=0

print(max(0,l[0]-1))

Solution (Python):
n,m=map(int,input().split())
arr=list(map(int,input().split()))
maximum=max(arr)
if (maximum-1)<=(sum(arr)-maximum):
  print(0)
else:
  print((maximum-1)-(sum(arr)-maximum))

Solution (Python):
k,t=map(int,input().split())
a=list(map(int,input().split()))
tm=max(a)
print(max(tm-1-(k-tm),0))

Solution (Python):
K, T = [int(x) for x in input().split()]
a_s = [int(x) for x in input().split()]

a_max = max(a_s)
a_others = K - a_max
ans = max(0, a_max - a_others - 1)

print(ans)

Solution (Python):


import sys
from heapq import heappop, heappush, heapify

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

K,T=MAP()
A=LIST()
A=[(-A[i], i) for i in range(T)]
heapify(A)

prev=-1
while len(A):
    v,i=heappop(A)
    if i!=prev:
        v+=1
        prev=i
        if v!=0:
            heappush(A, (v, i))
    else:
        if len(A)==0:
            print(-v)
            exit()
        v2,i2=heappop(A)
        v2+=1
        prev=i2
        if v2!=0:
            heappush(A, (v2, i2))
        heappush(A, (v, i))
print(0)

Solution (Python):
k,t=map(int,input().split())
l=list(map(int,input().split()))
if len(l)==1:
  print(k-1)
else:
  if k%2==0:
    print(max(0,(max(l)-k//2)*2-1))
  else:
    print(max(0,(max(l)-k//2-1)*2))

Solution (Python):
K, T = map(int, input().split())
a = list(map(int, input().split()))
print(max(max(a)-(sum(a)-max(a))-1, 0))

Solution (Python):
k, t = map(int, input().split())
alist = list(map(int, input().split()))

maxa = max(alist)
if k - maxa >= maxa:
  print(0)
else:
  print(maxa - (k - maxa) - 1)

Solution (Python):
K,T = map(int,input().split())
a = list(map(int,input().split()))

a.sort()
print(a[T-1]-sum(a[:T-1])-1 if a[T-1]>sum(a[:T-1]) else 0)

Solution (Python):
k,t=map(int,input().split());a=list(map(int,input().split()));m=max(a);print(max(0,m-(k-m)-1))

Solution (Python):
K,T=map(int,input().split())
l=list(map(int,input().split()))
sm=sum(l)
mx=max(l)
if mx-1<=sm-mx:
  print(0)
else:
  print(2*mx-1-sm)

Solution (Python):
K, T = map(int, input().split())
a = [int(_) for _ in input().split()]
maxcake = max(a)
print(max(0, 2*maxcake-K-1))

Solution (Python):
N,T = map(int,input().split())
A = [int(i) for i in input().split()]

print(max(0,max(A)-(N-max(A))-1))

Solution (Python):
_ = input()
l = list(map(int,input().split()))
print(max(0, max(l)*2 - sum(l) - 1))

Solution (Python):
k,t = map(int,input().split())
a = list(map(int,input().split()))
b = max(a)
print(max(0,2*b-k-1))

Solution (Python):
K,T=map(int,input().split(' '))
A=sorted(list(map(int,input().split(' '))))[::-1]
print(max(0, A[0]-1-sum(A[1:])))

Solution (Python):
K,T = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
if A[len(A)-1] <= K//2:
  print(0)
else:
  print(A[len(A)-1]*2-K-1)

Solution (Python):
k,t = map(int,input().split())
a = list(map(int,input().split()))
at = max(a)
ans = max(at - 1 -(k - at),0)
print(ans)

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

k,t = li()
a = list(li())

ans = 0

for ai in a:
    if  k%2 == 1 and ai > k//2 + 1:
        ans = (ai - (k//2+1)) * 2
            
    elif k%2 == 0  and ai > k//2:
        ans = 1 + (ai - (k//2+1)) * 2
        
print(ans)

Solution (Python):
k, t = map(int, input().split())
a = [[int(m), 0] for m in input().split()]
prohibit = 1
ans = 0
if t == 1:
    ans = a[0][0] - 1
else:
    for i in range(k):
        a.sort(reverse=True)
        if a[1][0] == 0:
            ans = a[0][0] - 1
            break
        else:
            if i == 0:
                a[1][1] = 1
            if a[0][1] == 0:
                a[0][0] -= 1
                a[0][1] = 1
                prohibit = 0
            elif a[1][1] == 0:
                a[1][0] -= 1
                a[1][1] = 1
                prohibit = 1
            for j in range(t):
                if prohibit != j:
                    a[j][1] = 0
print(ans)

Solution (Python):
k,t,*a=map(int,open(0).read().split())
b=sorted(a)
try:
    c=b[-1]-sum(b[:-1])-1
    if c>0:
        print(c)
    else:
        print(0)
except:
    print(b[0]-1)

Solution (Python):
K, T = map(int, input().split())
As = list(map(int, input().split()))

maxA = max(As)
sumA = sum(As)
print(max(0, maxA - (sumA - maxA) - 1))

Solution (Python):
K,T = map(int,input().split())
A = list(map(int,input().split()))

mx = max(A)
if mx <= (K+1)//2:
    print(0)
    exit()

if K%2:
    print(2 *(mx - (K+1)//2))
else:
    print(2 * (mx - K//2) - 1)

Solution (Python):
K,T = map(int,input().split())
A = list(map(int,input().split()))

mx = max(A)
if mx <= (K+1)//2:
    print(0)
    exit()

if K%2:
    print(2 *(mx - (K+1)//2))
else:
    print(2 * (mx - K//2) - 1)

Solution (Python):
k,t = (int(i) for i in input().split())
a = list(int(i) for i in input().split())
print(max(0,2*max(a)-sum(a)-1))

Solution (Python):
K,T = map(int,input().split())
A = list(map(int, input().split()))

dom = max(A)
other = sum(A) - dom

if K%2 >0:
  print(max(0, (dom-(K+1)//2)*2))
else:
  print(max(0, (dom-K//2)*2 -1))

Solution (Python):


k,t = map(int,input().split(' '))
cake = list(map(int,input().split(' ')))
sum = 0
maxCake = -1
for i in range(t):
    maxCake = max(maxCake,cake[i])
print(max(maxCake - 1 - (k - maxCake), 0))

Solution (Python):
k,t=map(int,input().split())
d=list(map(int,input().split()))
d.sort()
print(max(0,2*d[-1]-sum(d)-1) if t!=1else k-1)

Solution (Python):
K,T = map(int,input().split())

A = sorted(list(map(int,input().split())),reverse=True)
at = A[0]

ans = max(0,at - 1 - (K - at))

print(ans)