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())
cakes = list(map(int, input().split()))
max_c = 0
sum_c = 0
for cake in cakes:
  max_c = max(max_c, cake)
  sum_c += cake
print(max(0, 2*max_c-sum_c-1))

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

Solution (Python):
K,T=map(int,input().split())
a=list(map(int,input().split()))
while len(a)>1:
  a.sort(reverse=True)
  a[0]-=a.pop()
print(max(0,a[0]-1))

Solution (Python):
import sys
stdin=sys.stdin

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()

k,t=lp()
a=lp()

x=max(a)

if k%2==0:
  if x<=(k//2):
    print(0)
  else:
    rest=k-x
    print(x-(rest+1))
    
else:
  if x<=(k//2)+1:
    print(0)
  else:
    rest=k-x
    print(x-(rest+1))

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

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

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


if __name__ == '__main__':
    main()

Solution (Python):
K, T = map(int, input().split())
N = (K + 1) // 2
A = list(map(int, input().split()))
a = max(A)
if a <= N:
  print(0)
else:
  print(2 * (a - N) - 1)

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

m = max(lis)
lis = sorted(lis)

if lis[-1:]:
    pass

if sum(lis[:-1]) >= m:
    print(0)
else:
    print(m-1-sum(lis[:-1]))

Solution (Python):
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  


K,T = MI()
A = LI()
print(max(0,2*max(A)-K-1))

Solution (Python):
k, t = map(int, input().split())
A = sorted(map(int, input().split()))
if t == 1:
    print(k - 1)
else:
    dev = k - A[-1] + 1
    ans = max(0, (A[-1] // dev - 1) * dev + A[-1] % dev)
    print(ans)

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

m = max(al)
print(max(0, k-(k-m)*2-1))

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

K, T = mapint()
As = list(mapint())
maxi = max(As)
rest = K-maxi
if maxi<=rest+1:
    print(0)
else:
    maxi -= rest+1
    print(maxi)

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

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

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

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

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():

    k, t = LI()
    a = LI()
    a.sort(reverse=True)

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


main()

Solution (Python):
K, T = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
if K == T:
    print(0)
    exit()

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

b = a[0]
c = a[1]

for i in range(2, T):
    if b >= c:
        c += a[i]
    else:
        b += a[i]

print(max(abs(b - c) - 1, 0))

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

maxnum = max(A)
print(max(0, maxnum-1-(k-maxnum)))

Solution (Python):
k,t=map(int,input().split())
a=[int(x) for x in input().split()]
a.sort()
now1=0
now2=0
al=sum(a)
ans=float('inf')
for i in range(t):
  now1+=a[i]
  now2=al-now1
  ans=min(ans,abs(now1-now2)-1)

if ans<=0:
  print(0)
else:
  print(ans)

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

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

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

Solution (Python):
t, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)
if t%2==0:
    if t//2>=a[0]:
        ans = 0
    else:
        tmp = a[0] - (t//2)
        ans = 1 + 2*(tmp-1)
elif t%2!=0:
    if (t+1)//2>=a[0]:
        ans = 0
    else:
        tmp = a[0] - ((t+1)//2)
        ans = 2 * tmp

print(ans)

Solution (Python):
import heapq
from typing import List, Any, Tuple


def read_int() -> int:
    return int(input().strip())


def read_ints() -> List[int]:
    return list(map(int, input().strip().split(' ')))


def solve() -> Any:
    K, T = read_ints()
    A = read_ints()
    cakes: List[List[int]] = [[-A[i], i] for i in range(T)]
    heapq.heapify(cakes)
    last_eaten = -1
    count = 0
    while cakes:
        frequent0 = heapq.heappop(cakes)
        if frequent0[1] == last_eaten:
            if cakes:
                frequent1 = heapq.heappop(cakes)
                last_eaten = frequent1[1]
                frequent1[0] += 1
                if frequent1[0] != 0:
                    heapq.heappush(cakes, frequent1)
            else:
                count += 1
                last_eaten = frequent0[1]
                frequent0[0] += 1
        else:
            frequent0[0] += 1
            last_eaten = frequent0[1]
        if frequent0[0] != 0:
            heapq.heappush(cakes, frequent0)
    return count


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

Solution (Python):
import sys, math
from functools import lru_cache
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

K, T = mi()
a = list(mi())

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

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

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

if t == 1:
    print(k-1)
else:
    if max_A -1 > sum(A) - max_A:
        print((max_A -1) - sum(A) + max_A)
    else:
        print(0)

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    return C

K,T=mint()
A=lint()
print(max(0,max(A)-(K-max(A))-1))

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

Solution (Python):

import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    K, T = MI()
    types = LI()

    types.sort(reverse=True)
    max_num = types[0]
    others_num = sum(types[1:])
    ans = max(max_num - 1 - others_num, 0)
    print(ans)


main()

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

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

Solution (Python):

k, t = map(int,input().split())
A=list(map(int,input().split()))
m=max(A)
s=k-m
print(max(m-1-s, 0))

Solution (Python):

import math
from collections import deque
import copy
import bisect
k, n = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
m=max(A)
s=k-m
print(max(m-1-s, 0))

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

Solution (Python):
import sys



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


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    k, t = input_int_list()
    A = input_int_list()
    _max = max(A)
    ans = (_max - (k - _max)) - 1
    if ans < 0:
        ans = 0
    print(ans)
    return


if __name__ == "__main__":
    main()

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

Solution (Python):
import heapq
def main():
    k,t=map(int,input().split())
    a=list(map(lambda x: (-int(x[1]), x[0]+1), enumerate(input().split())))
    heapq.heapify(a)
    p=None
    while len(a) > 0:
        n=heapq.heappop(a)
        if len(a) == 0:
            print(-n[0]-1)
            break
        if p == n[1] and len(a) > 0:
            n2 = n
            n = heapq.heappop(a)
            heapq.heappush(a, n2)
        if n[0] < -1:
            heapq.heappush(a, (n[0]+1, n[1]))
        p = n[1]
    
if __name__ == "__main__":
    main()

Solution (Python):


import math

import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd



k,t=map(int,input().split())
a=list(map(int,input().split()))
x=max(a)
if k-x>=x-1:
    print(0)
else:
    print(2*x-1-k)

Solution (Python):


import heapq

k, t = list(map(int, input().split()))
cakes = list(map(lambda x: int(x)*-1, input().split()))
heapq.heapify(cakes)
ans = 0
top_change = True
for i in range(k):
    if top_change:
        first = heapq.heappop(cakes)
        first += 1
        if len(cakes) == 0:
            ans += first*-1
            break
        if first < cakes[0]:
            top_change = False
        else:
            top_change = True
        if first != 0:
            heapq.heappush(cakes, first)
    else:
        first = heapq.heappop(cakes)
        second = heapq.heappop(cakes)
        second += 1
        heapq.heappush(cakes, first)
        if second != 0:
            heapq.heappush(cakes, second)
        top_change = True

print(ans)

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

largest = max(a)
print(max(largest-(k-largest+1), 0))

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

Solution (Python):
H,W=list(map(int,input().split()))
l=list(map(int,input().split()))
l=[i*-1 for i in l]
import heapq
B=0
heapq.heapify(l)
for i in range(H):
   A=heapq.heappop(l)
   A+=1
   heapq.heappush(l,B)
   B=A
   l=[i for i in l if i!=0]
   if len(l)==0:
      print(H-1-i)
      exit()

Solution (Python):
import math
K,T=map(int,input().split())
A=list(map(int,input().split()))
if T==1:
    print(K-1)
else:
    half=math.ceil(K/2)
    ans=0
    for a in A:
        if a>half:
            ans+=(a-1-(K-a))
    print(ans)

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

b = 0
for i in a[::-1]:
    b = abs(b-i)
print(max(b-1, 0))

Solution (Python):
import sys
import math
import itertools
import collections
from collections import deque
from collections import defaultdict

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD2 = 998244353
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    K, T = NMI()
    A = NLI()

    max_A = max(A)
    
    if T == 1:
        print(K-1)
    else:
        if max_A <= math.ceil(K/2):
            print(0)
        else:
            if K % 2 == 0:
                print(2*(max_A-math.ceil(K/2))-1)
            else:
                print(2*(max_A-math.ceil(K/2)))


if __name__ == '__main__':
    main()

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

print(max((max(cake) - 1 - (K -  max(cake))), 0))

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


if __name__ == "__main__":
    main()

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 = list(map(int, input().split()))
nums = list(map(int, input().split()))

m = max(nums)
rest = k - m
if m - rest - 1 > 0:
    print(m - rest - 1)
else:
    print(0)

Solution (Python):
K,T=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
a.reverse()
ans=2*a[0]-sum(a)-1
if ans<0:
    ans=0
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N, T = map(int, input().split())
a = list(map(int, input().split())) + [-N]
last = T
res = N
for t in range(N):
  x = T
  for i in range(T):
    if i == last: continue
    if a[x] <= a[i] and (a[i] > 0): x = i
  if x == T: x = last
  a[x] -= 1
  res -= x != last or (t == 0)
  last = x
print(res)

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

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

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


K, T, *A = map(int, open(0).read().split())
pq = [(-a, i) for i, a in enumerate(A)]
heapify(pq)

ans = 0
prev = -1
for _ in range(K):
    v, k = heappop(pq)
    if v == 0:
        heappush(pq, (v, k))
        ans += 1
    elif k != prev:
        prev = k
        heappush(pq, (v + 1, k))
    elif len(pq) > 1 and pq[0][0] < 0:
        v_nx, k_nx = heappop(pq)
        prev = k_nx
        heappush(pq, (v_nx + 1, k_nx))
        heappush(pq, (v, k))
    else:
        ans += 1
        heappush(pq, (v + 1, k))
print(ans)

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

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re












import sys
sys.setrecursionlimit(10000000)


mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n,t = readInts()
T = readInts()
ma = max(T)
rest = n - ma
print(max(0,ma - rest - 1))

Solution (Python):
K,T = map(int,input().split())
A = list(map(int,input().split()))
a = max(A)
if a<=(K+1)//2:
    print(0)
else:
    print(2*a-K-1)

Solution (Python):
K, T = map(int,input().split())
a = sorted(list(map(int,input().split())))
m = a[-1]
o = K-m
if m <= o:
    print(0)
else:
    print(m-o-1)

Solution (Python):
k,t = map(int, input().split())
a = sorted(list(map(int, input().split())))
Sum = sum(a)-a[-1]
Max = a[-1]
if Sum >= Max: ans = 0
else: ans = Max-Sum-1
print(ans)

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

prev = -1
ans = 0
for i in range(k):
    max_v = max(al)
    max_ind = al.index(max_v)
    max_v = 0
    max_i = -1
    for j, v in enumerate(al):
        if max_v < v and j != prev:
            max_v = v
            max_i = j
    if max_i != -1:
        al[max_i] -= 1
        prev = max_i
    else:
        ans += 1

print(ans)

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

Solution (Python):
K,T = list(map(int,input().split()))
A = list(map(int,input().split()))
MA = max(A)
out = 0
if MA > K-MA+1:
    out += MA-(K-MA+1)
print(out)

Solution (Python):

printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7


def ddprint(x):
  if DBG:
    print(x)

k,t = inm()
a = inl()
b = k-max(a)
print(max(0,k-1-2*b))

Solution (Python):

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

A = sorted(A, reverse=True)

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

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    K, T = map(int, readline().split())
    A = list(map(int, readline().split()))
    if sum(A)-max(A)+1 <max(A):
        print(max(A)*2-sum(A)-1)
    else:
        print(0)



if __name__ == '__main__':
    main()

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

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

ma = a[0]
ex = sum(a[1:])
diff =  ma - ex
if diff > 0:
    print(diff - 1)
else:
    print(0)

Solution (Python):
k, t = map(int, input().split())
a = list(map(int, input().split()))
num = max(a)
if (k + 1) // 2 >= num:
    print(0)
else:
    print(num * 2 - k - 1)

Solution (Python):
import heapq
K, T = map(int, input().split())
a = tuple(map(int, input().split()))
cakes = []
heapq.heapify(cakes)
for i in range(T):
    heapq.heappush(cakes, [-1*a[i], i])

now = -1
ans = 0
for _ in range(K):
    cake_num, cake_idx = heapq.heappop(cakes)
    cake_num *= -1
    if cake_idx == now and len(cakes) > 1 and cakes[0][0] != 0:
        next_cake_num, next_cake_idx = heapq.heappop(cakes)
        next_cake_num *= -1
        next_cake_num -= 1
        heapq.heappush(cakes, [-1 * next_cake_num, next_cake_idx])
        now = next_cake_idx
    else:
        cake_num -= 1
        if now == cake_idx:
            ans += 1
        now = cake_idx
    heapq.heappush(cakes, [-1 * cake_num, cake_idx])

print(ans)

Solution (Python):
k,t=map(int,input().split())
a=list(map(int,input().split()))
a_m=0
sm =0
for i in range(1,t):
  if a[a_m] < a[i]:
    sm += a[a_m]
    a_m = i
  else:
    sm += a[i]

print(max(0, a[a_m]-sm-1))

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

mt = max(t)

print(max(0, mt-1-(k-mt)))

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):

import sys

sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math




from collections import deque


INF = 1 << 50

def run():
    K, T, *A = map(int, read().split())
    max_a = max(A)
    print(max(max_a-1-K+max_a, 0))

if __name__ == "__main__":
    run()

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

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

Solution (Python):
import sys
import heapq

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

    h = []
    for i in range(T):
        heapq.heappush(h, (-A[i], i))

    ans = 0
    p = -1

    while h:
        c = heapq.heappop(h)
        if p == c[1]:
            if h:
                c2 = heapq.heappop(h)
                heapq.heappush(h, c)
                x = c2[0]
                y = c2[1]
                p = y
                x += 1
                if x != 0:
                    heapq.heappush(h, (x, y))
            else:
                ans = c[0]
                break
        else:
            x = c[0]
            y = c[1]
            x += 1
            p = y
            if x != 0:
                heapq.heappush(h, (x, y))

    print(-ans)


if __name__ == '__main__':
    main()

Solution (Python):

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

Solution (Python):

k, t, *a = map(int, open(0).read().split())
a.sort()
c = [0]
for i in a:
    c.append(c[-1]+i)
print(min(abs(c[-1] - c[i]*2) - (c[-1]!= c[i]*2) for i in range(t+1)))

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

A.sort()
print(max(A[-1]-sum(A[:-1])-1, 0))

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

B=max(A)

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

if K%2:
    X=(K+1)//2
    if X>=B:
        print(0)
    else:
        C=B-X
        print(2*C)
else:
    Y=K//2
    if Y>=B:
        print(0)
    else:
        C=B-Y
        print(2*C-1)

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

B=max(A)

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

if K%2:
    X=(K+1)//2
    if X>=B:
        print(0)
    else:
        C=B-X
        print(2*C)
else:
    Y=K//2
    if Y>=B:
        print(0)
    else:
        C=B-Y
        print(2*C-1)

Solution (Python):
K,T=map(int,input().split())
*A,=map(int,input().split())
ans=0
H=0--K//2
for a in A:
    if a>H:
        ans+=2*a-K-1
print(ans)

Solution (Python):






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





if T == 1:
    ans = K-1
else:
    mylist = sorted(a)
    ans = max(mylist[-1]-sum(mylist[:-1])-1,0)
print(ans)

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

a.sort()

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

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

dp = [[False] * (k//2+1) for _ in range(t+1)]

dp[0][0] = True

for i in range(t):
    for j in range(k//2+1):
        if dp[i][j]:
            dp[i+1][j] = True
            if j + a[i] < k//2+1:
                dp[i+1][j + a[i]] = True

cnt = 0
for i,e in enumerate(dp[-1]):
    if e:
        cnt = i

ans = k - (2 * cnt + 1)

print(max(ans, 0))

Solution (Python):
import sys

input = sys.stdin.readline


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

    prev_idx = -1
    ans = 0
    for _ in range(K):
        cur_idx = -1
        max_num = 0
        for i, a in enumerate(A):
            if i == prev_idx:
                continue
            if a > max_num:
                max_num = a
                cur_idx = i
        if max_num == 0:
            ans = A[prev_idx]
            break
        A[cur_idx] -= 1
        prev_idx = cur_idx

    print(ans)


if __name__ == "__main__":
    main()

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

max_cake = max(a)

if max_cake >= K - max_cake:
    result = max_cake - (K - max_cake) - 1
else:
    result = 0

print(result)

Solution (Python):
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)


input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  
    fac = [0] * SIZE  
    finv = [0] * SIZE  
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    
    
    
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    
    return X.flatten().tolist()[0]

class TwoDimGrid:
    
    def __init__(self, h, w, wall="
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        
        
        
        
        

def solve():
    k, t= getList()
    nums = getList()
    mx = max(nums)
    if mx + mx - 1 <= k:
        print(0)
        return
    else:
        print(mx-1 - (k - mx))
        return
def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    
    solve()

Solution (Python):
n, k = map(int, input().split())
l = sorted(list(map(int, input().split())))
mx = l[-1]
s = sum(l[:-1])
print(max(0, mx-1-s))

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

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

x = max(a)
a.pop(a.index(max(a)))
y = sum(a)

if x > y:
  print(x-y-1)
else:
  print(0)

Solution (Python):
import sys
R1 = input().split()
R2 = input().split()
K = int(R1[0])
T = int(R1[1])
answer = int(0)
A = []
for i in range (0, T):
    A.append(int(R2[i]))
A.sort()
A.reverse()
if T == 1:
    print(A[0] - 1)
    sys.exit()
if T == 2:
    print(A[0] - A[1] - 1)
    sys.exit()
for i in range (0, K):
    C = A[0]
    if C - 1 > A[1]:
        if A[1] == 0:
            answer += 1
        if A[1] != 0:
            K -= 1
            D = int(A[1])
            A.remove(D)
            F = int(1)
            left = int(0)
            right = int(T - 2)
            while (left <= right):
                mid = int(left + (right - left) / 2)
                if A[mid] >= D - 1:
                    left = mid + 1
                    F = max(F, mid + 1)
                else:
                    right = mid - 1
            A.insert(F, D - 1)
        A[0] -= 1
    if C - 1 == A[1]:
        A[0] -= 1
    if C == A[1]:
        F = int(0)
        left = int(0)
        right = int(T - 1)
        while (left <= right):
            mid = int(left + (right - left) / 2)
            if A[mid] == C:
                left = mid + 1
                F = max(F, mid + 1)
            else:
                right = mid - 1
        A.insert(F, C - 1)
        A.remove(C)
print(answer)

Solution (Python):
def main():
    K, T = map(int, input().split())
    A = list(map(int, input().split()))
    A.append(0)
    A.sort(reverse=True)
    ans =max(0, max(A) - sum(A[1:]) - 1)
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    k, t = map(int, readline().split())
    a = list(map(int, readline().split()))
    ans = 0
    prev = -1

    for _ in range(k):
        max_a = 0
        cur = -1
        for i in range(t):
            if i != prev:
                if a[i] > max_a:
                    max_a = a[i]
                    cur = i
        if cur != -1:
            a[cur] -= 1
            prev = cur

    print(sum(a))


if __name__ == '__main__':
    main()

Solution (Python):
def resolve():
    '''
    code here
    '''
    import collections
    K , T = [int(item) for item in input().split()]
    As = [int(item) for item in input().split()]
    As.sort(reverse=True)

    cnt = collections.Counter(As)
    interval_num = As[0]-1

    res = interval_num - sum(As[1:])

    if res <= 0:
        print(0)
    else:
        print(res)

if __name__ == "__main__":
    resolve()