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.



Sample input:
7 3
3 2 2


Sample output:
0

Submission status: Accepted

Solution code:
k, t = map(int, raw_input().split())
a = map(int, raw_input().split())
a = sorted(a,reverse=True)
b = range(len(a))
c = a[:]
a = []
for i in xrange(len(b)):
    a.append([c[i], i])
d = 0
y = -1
while True:
    # print a
    # print y
    if len(a) == 1:
        d += a[0][0]-1
        break
    else:
        if a[0][0] == 0:
            break
        elif a[0][1] != y:
            a[0] = [a[0][0]-1,a[0][1]]
            y = a[0][1]
        else:
            if a[1][0] == 0:
                d += a[0][0]
                break
            a[1] = [a[1][0]-1,a[1][1]]
            y = a[1][1]
        a = sorted(a,reverse=True)
# if t == 1:
#     d = t-1
# else:
#     d = a[0] - sum(a[1:]) -1
#     if d < 0:
#         d = 0
print d

Submission status: Accepted

Solution code:
k,t=map(int,input().split())
arr=list(map(int,input().split()))
amax = max(arr)
arest = k-amax
if arest>=amax-1:
    print(0)
else:
    print(amax-1-arest)

Submission status: Accepted

Solution code:
l=list(map(int, input().split()))
r=list(map(int, input().split()))
print(max(0,2*max(r)-l[0]-1))

Submission status: Accepted

Solution code:
cakeNum, dayNum = map(int, raw_input().split(' '))
cakes = []
map(cakes.append, map(int, raw_input().split(' ')))

if max(cakes) * 2 > sum(cakes):
	print max(cakes) * 2 - sum(cakes) - 1
else:
	print 0

Submission status: Accepted

Solution code:
input = raw_input().split(' ')
K = int(input[0])
T = int(input[1])
input = raw_input().split(' ')
max_num = 0
sum_num = 0
for i in range(0, T):
  temp = int(input[i])
  if max_num < temp:
    max_num = temp
  sum_num += temp

if max_num <= sum_num - max_num + 1:
  print "0"
else:
  print max_num - (sum_num - max_num) - 1

Submission status: Accepted

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

if T == 1:
    print(A[0] - 1)
    exit()

prev_argmax = -1
for k in range(K):
    if prev_argmax == -1:
        temp_max = -1
        for i, a in enumerate(A):
            if a > temp_max:
                temp_max = a
                argmax = i
    else:
        temp_max = -1
        for i, a in enumerate(A):
            if i != prev_argmax:
                if a > temp_max:
                    temp_max = a
                    argmax = i
        if temp_max == -1:
            temp_max = max(A[:prev_argmax] + A[prev_argmax + 1:])
            for i, a in enumerate(A):
                if i != prev_argmax and a == temp_max:
                    argmax = i
                    break
    if temp_max == 0:
        break
    A[argmax] -= 1
    prev_argmax = argmax
ans = 0
for i, a in enumerate(A):
    if i != prev_argmax:
        ans += max(0, a - 1)
    else:
        ans += max(0, a)
print(ans)

Submission status: Accepted

Solution code:
k,t=map(int,input().split())
a=[int(i) for i in input().split()]
m=2*max(a)-sum(a)
print(0 if m<=1 else m-1)

Submission status: Accepted

Solution code:
import collections

k, t = [int(x) for x in input().split()]
a = collections.Counter(dict(enumerate([int(x) for x in input().split()])))

last = None
ans = 0
for i in range(k):
    mc = a.most_common(2)

    if len(mc) == 1 or mc[1][1] == 0 or mc[0][0] != last:
        now = mc[0][0]
    else:
        now = mc[1][0]

    ans += last == now
    a[now] -= 1
    last = now

print(ans)

Submission status: Accepted

Solution code:
s = input()
nums = [int(x) for x in input().split()]
mx = max(nums)
remain = sum(nums) - mx
print(0 if mx-remain <= 0 else mx-remain-1)

Submission status: Accepted

Solution code:
k, t = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a.sort()
a.reverse()
same_eaten = 0

a[0] -= 1
last_index = 0
k -= 1
for _ in range(k):
    eaten = False
    for i in range(len(a)):
        if i != last_index and a[i] > 0:
            eaten = True
            a[i] -= 1
            if a[i+1] > a[i]:
                last_index = i+1
            else:
                last_index = i
            a.sort()
            a.reverse()
            break
    if not eaten:
        same_eaten += 1
print(same_eaten)

Submission status: Accepted

Solution code:
def main():
    K, T = map(int, input().split())
    a = list(map(int, input().split()))

    previous_value = -1
    result = []
    for i in range(K):
        max_a = -1
        for (index, value) in enumerate(a):
            if max_a < value and previous_value != index and value > 0:
                max_a = value
                next_value = index
        a[next_value] -= 1
        result.append(next_value)
        previous_value = next_value

    r = 0
    for i in range(K-1):
        if result[i] == result[i+1]:
            r+=1
    print(r)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
n = stdin.readline()
n = n.strip().split(" ")
K = int(n[0])
T = int(n[1])
n = stdin.readline()
n = n.strip().split(" ")
a = []
for t in n:
	a.append(int(t))

idx = np.argmax(a)

res = np.max(a) -1
for b in range(len(a)):
	if b != idx:
		res -= a[b]
if res < 0:
	res = 0
print res

Submission status: Accepted

Solution code:
k,t=map(int,raw_input().split())
a=map(int,raw_input().split())
a.sort()
for i in xrange(t):
    j=t-1
    while 1:
        if i==j:
            if a[i]-1<0:
                print(0)
            else:
                print(a[i]-1)
            exit()
        if a[j]>=a[i]:
            a[j]-=a[i]
            break
        else:
            a[i]-=a[j]
            a[j]=0
            j-=1

Submission status: Accepted

Solution code:
k, t = map(int, raw_input().split())

a = map(int, raw_input().split())

m = max(a)

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

Submission status: Accepted

Solution code:
k, t = map(int, raw_input().split())
a = map(int, raw_input().split())

while len(a) > 1:
    a.sort()
    tmp = abs(a.pop() - a.pop())
    if tmp:
        a.append(tmp)

if not a:
    print 0
else:
    print a[0] - 1

Submission status: Accepted

Solution code:
k,n = map(int, raw_input().split())
a = map(int, raw_input().split())

m = max(a) - 1
rest = sum(a) - max(a)
print m - rest if m - rest >= 0 else 0

Submission status: Accepted

Solution code:
K,T = map(int,raw_input().split())
A = map(int,raw_input().split())

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

Submission status: Accepted

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

ans = 0

x = -1
for i in range(K):
    m1 = -1
    m2 = -1
    i1 = -1
    i2 = -1
    for j in range(len(a)):
        if a[j] > m1:
            i2 = i1
            i1 = j
            m2 = m1
            m1 = a[j]
        elif a[j] > m2:
            m2 = a[j]
            i2 = j
    if x != i1 and m1 > 0:
        a[i1] -= 1
        x = i1
    elif x != i2 and m2 > 0:
        a[i2] -= 1
        x = i2
    else:
        ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
K, T = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a)-max(a)
if max(a) - s < 2:
	print(0)
else:
	print(max(a)-s-1)

Submission status: Accepted

Solution code:

def read():
    return int(input())

def reads(sep=None):
    return list(map(int, input().split(sep)))

def main():
    k, t = reads()
    a = [list(x) for x in list(enumerate(reads()))]
    n = -1
    c = 0
    while sum([x[1] for x in a]):
        a = sorted(a, key=lambda x:x[1], reverse=True)
        if a[0][0] != n:
            n = a[0][0]
            a[0][1] -= 1
        else:
            if len(a) == 1:
                c += 1
                a[0][1] -= 1
            elif 0 < a[1][1]:
                a[1][1] -= 1
                n = a[1][0]
            else:
                a[0][1] -= 1
                c += 1
    print(c)

main()

Submission status: Accepted

Solution code:
#coding: utf-8

import re

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

count = 0
n = None #
for j in range(K):
    m = 0
    t = None
    for i,c in enumerate(a):
        if c > m and (n is None or i != n):
            m = c
            t = i
    if t is None:
        count += 1
        t = n
    a[t] -= 1
    n = t
print(count)

Submission status: Accepted

Solution code:
k, t = map(int, raw_input().split())
a = map(int, raw_input().split())
ans = 0
mx = max(a)
if mx*2 > k+1:
    ans = mx*2-k-1
print ans

Submission status: Accepted

Solution code:
k,t=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
a.sort(reverse=True)
if len(a) ==1 :
    print(k-1)
else:
    m = 0
    for i in a:
        if(m<i):
            m = i - m
        else:
            m = m - i
    print(max(m-1,0))

Submission status: Accepted

Solution code:
import numpy as np


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

def main():
    if len(A) < 2:
        return K - 1
    s = -1
    answer = 0
    for k in range(K):
        A.sort(reverse=True)
        a, i = A[0]
        b, j = A[1]

        if i != s:
            A[0] = (a - 1, i)
            s = i
            continue

        if b == 0:
            A[0] = (a - 1, i)
            s = i
            answer += 1
            continue

        A[1] = (b - 1, j)
        s = j
    return answer

print(main())

Submission status: Accepted

Solution code:
K, T = map(int, raw_input().split())

a = map(int, raw_input().split())

ans = 2 * max(a) - K - 1

if ans < 0:
    print 0
else:
    print ans

Submission status: Accepted

Solution code:
K, T = map(int, raw_input().split())
nums = [int(t) for t in raw_input().split()]

max_1 = max(nums)
nums.remove(max_1)
sum_remain = sum(nums)
if sum_remain >= max_1-1:
    print 0
else:
    print (max_1 - sum_remain - 1)

Submission status: Accepted

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

if T == 1:
    print(K - 1)
else:
    max_ai = max(a)
    slit = max_ai - 1
    if slit > sum(a) - max_ai:
        print(slit - (sum(a) - max_ai))
    else:
        print(0)

Submission status: Accepted

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

M = max(a)
print(max(0, 2*(M-(k//2+1))+1-k%2))

Submission status: Accepted

Solution code:
import sys

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

a = max(As)
b = a - (K - a)

if b >= 1:
    print(b-1)
else:
    print(0)

Submission status: Accepted

Solution code:
n, k = map(int, raw_input().split())
a = map(int, raw_input().split())
x = max(a)
x = x - (n - x) - 1
if k == 1:
   x = a[0] - 1
print max(x, 0)

Submission status: Accepted

Solution code:
def solve():
    total, num_types = map(int, raw_input().split())
    type_counts = sorted(map(int, raw_input().split()))
    big = type_counts[-1]
    rest = total - big
    return max(0, big - rest - 1)

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

Submission status: Accepted

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

a.sort()

maxi = a[-1]
if K /2>= maxi+1:
    print(0)
    exit()

ans =None
if K%2==0:
    ans =(maxi - K//2)*2 -1

else:
    ans = (maxi - K//2 - 1)*2
if ans<0:
    ans=0


print(ans)

Submission status: Accepted

Solution code:
import bisect
import sys
import math
import itertools
sys.setrecursionlimit(10000)

INF = float('inf')

# input macro
def i():
    return int(raw_input())
def ii():
    return map(int,raw_input().split(" "))
def s():
    return raw_input()
def ss():
    return raw_input().split(" ")
def slist():
    return list(raw_input())
def join(s):
    return ''.join(s)

#memoize macro
def memoize(f):
    cache = {}
    def helper(*args):
        if args not in cache:
            cache[(args)] = f(*args)
        return cache[args]
    return helper

nh=zip([1,0,-1,0],[0,1,0,-1])
mod=1000000007

###########
k,t=ii()
a=ii()
cur=a.index(max(a))
a[cur]-=1
cnt=0
for j in range(k-1):
    tmp=[a[j] for j in range(t) if j!=cur and a[j]!=0]
    if len(tmp)!=0:
        cur=a.index(max(tmp))
        a[cur]-=1
    else:
        a[cur]-=1
        cnt+=1
print cnt

Submission status: Accepted

Solution code:
kt = list(map(lambda number: int(number), input().split(' ')))
a_list = list(map(lambda number: int(number), input().split(' ')))

a_list.sort(reverse=True)

remain = kt[0] - a_list[0]
print(max(-1 * (remain + 1 - a_list[0]), 0))

Submission status: Accepted

Solution code:
from collections import Counter
K, T = [int(x) for x in raw_input().strip().split()]
a = [int(x) for x in raw_input().strip().split()]
amax = max(a)
aothersum = sum([x for x in a if x!=amax])
ac = Counter(a)
aothersum += (ac[amax]-1)*amax

if amax <= aothersum:
  print 0
else:
  print amax - aothersum - 1

Submission status: Accepted

Solution code:
from functools import reduce
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
s = reduce(lambda sum,x: sum+x, a)
m = max(a)
x = m - (s-m)
if x > 0:
    print(x-1)
else:
    print(0)

Submission status: Accepted

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

# def remax(seq):
#     m = max(seq)
#     for idx,i in enumerate(seq):
#         if m==i:
#             return (idx, m)
#
# lastK = -1
# cnt = 0
# for _k in range(K):
#     remax
#

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

Submission status: Accepted

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


def argmax(l, excep):
    m = -1
    ix = -1

    for i, e in enumerate(l):
        if i == excep:
            continue

        if e > m:
            m = e
            ix = i

    return ix, m


t = sum(a)
b = -1
ans = 0

for i in range(t):
    ix, m = argmax(a, b)

    if m > 0:
        b = ix
        a[ix] -= 1

    else:
        if b == -1:
            break

        if a[b] != 0:
            ans += 1
            a[b] -= 1
        else:
            break

print(ans)

Submission status: Accepted

Solution code:
K, T = map(int, raw_input().split())
A = map(int, raw_input().split())
A = sorted([[i, j] for i, j in enumerate(A)], key=lambda x:x[1])
last = -1
for i in xrange(K):
    A = sorted(A, key=lambda x:x[1])
    if len(A) == 1:
        print A[0][1] - 1 + (last == A[0][0])
        exit()
    if last == A[-1][0]:
        A[-2][1] -= 1
        last = A[-2][0]
        if A[-2][1] == 0:
            A.pop(-2)
    else:
        A[-1][1] -= 1
        last = A[-1][0]
        if A[-1][1] == 0:
            A.pop()
print 0

Submission status: Accepted

Solution code:
K,T = map(int ,raw_input().split(" "))
A = map(int,raw_input().split(" "))

a = max(A)
b = K
if K%2 == 1:
    b+=1

if (b/2 >= a):
    print 0
else:
    c = K-a
    
    z = a - c - 1
    print z

Submission status: Accepted

Solution code:
K, T = map(int, raw_input().split())
a = map(int, raw_input().split())

max_a = max(a)
other = K-max_a
if max_a-other > 0:
    print max_a-other-1
else:
    print 0

Submission status: Accepted

Solution code:
K, T = map(int, raw_input().split())
a = map(int, raw_input().split())
total = sum(a)
maxcake = max(a)
other = total - maxcake
print max(0, maxcake - other - 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math,string,itertools,fractions,heapq,collections,re,array,bisect

def s():
    return raw_input().strip()
def n():
    return int(raw_input())
def d():
    return float(raw_input())

def ls():
    return raw_input().strip().split()
def ln():
    return map(int, raw_input().strip().split())
def ld():
    return map(float, raw_input().strip().split())

def fs():
    return [raw_input().strip() for i in xrange(input())]
def fn():
    return [int(raw_input().strip()) for i in xrange(input())]
def fd():
    return [float(raw_input().strip()) for i in xrange(input())]

EPS = 1e-9

#http://www.deqnotes.net/acmicpc/2d_geometry/lines
def dot(a, b):
    return a.real * b.real + a.imag * b.imag

def cross(a, b):
    return a.real * b.imag - a.imag * b.real

def distance(a, b, c):
    if dot(b-a, c-a) <= EPS:
        return abs(c-a)
    if dot(a-b, c-b) <= EPS:
        return abs(c-b)
    return abs(cross(b-a, c-a)/(b-a))

K, T = ln()
A = ln()

A.sort(reverse=True)

f = A[0]
S = sum(A) - f

print max(0, f - S - 1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from __future__ import division, print_function, absolute_import, unicode_literals

import sys

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

if T == 1:
    print(a[-1]-1)
    sys.exit()
maxc = a[-1] - sum(a[:-1]) - 1
print(max(maxc, 0))

Submission status: Accepted

Solution code:
K, T = map(int, raw_input().split())
a_list = map(int, raw_input().split())

a_max = max(a_list)
if 2*a_max - 1 <= K:
    print 0
else:
    print (2*a_max - 1) - K

Submission status: Accepted

Solution code:
from random import random
import math
import re
import fractions

K, T = map(int, raw_input().split(" "))
A = map(int, raw_input().split(" "))
s = 0
m = 0
for a in A:
    s += a
    m = max(a, m)
print max(m * 2 - s - 1, 0)

Submission status: Accepted

Solution code:
k, t = map(int, raw_input().split())
s = map(int, raw_input().split())
s = sorted(s, reverse=True)
if s[0] > sum(s[1:]):
    print s[0] - sum(s[1:]) - 1
else:
    print 0

Submission status: Accepted

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

Submission status: Accepted

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

cnt = collections.Counter(a)
m = max(cnt)
if m<=  k-m+1:
    print(0)
else:
    print(2*m-k-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import string,itertools,fractions,heapq,re,array,bisect
#from math import *
#from collections import Counter
def rl(s): return xrange(len(s))


INF = 2147483647


import sys
stdin = sys.stdin


K, T = map(int, stdin.readline().strip().split())
aa = map(int, stdin.readline().strip().split())

aa.sort()
m = aa[-1]

can_fit = (K + 1) // 2
m -= can_fit
rr = 0
if m > 0:
    if K % 2 == 0:
        rr += 1
        m -= 1
    rr += m*2

print rr

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

K, T = map(int, raw_input().split())
a = map(int, raw_input().split())

A = [[-ai, i] for i, ai in enumerate(a)]
heapq.heapify(A)
ans = 0
b = -1
for i in xrange(K):
    t = heapq.heappop(A)
    if t[1] == b and A:
        u = heapq.heappop(A)
        if u[0] < -1:
            heapq.heappush(A, [u[0] + 1, u[1]])
        b = u[1]
        heapq.heappush(A, t)
    else:
        if t[1] == b:
            ans += 1
        if t[0] < -1:
            heapq.heappush(A, [t[0] + 1, t[1]])
        b = t[1]
print ans

Submission status: Accepted

Solution code:
K, T = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
m = max(a)
s = sum(a) - m
if s < m:
    print(m - s - 1)
else:
    print(0)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import heapq

K,T = map(int,input().split())
As = list(map(int,input().split()))
mx = max(As)
others = K - mx
print(max(mx - 1 - others, 0))

Submission status: Accepted

Solution code:
k = int(raw_input().split()[0])
a = [int(a) for a in raw_input().split()]
ans = 0
for a in a:
  if k - a < a:
    ans = a - (k - a) - 1
print ans

Submission status: Accepted

Solution code:
x = raw_input()
K,T = [int(i) for i in x.split()]

a = raw_input()
a = [int(i) for i in a.split()]

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**10
mod = 10**9 + 7


def f():
    k,t = list(map(int, input().split()))
    a = list(map(int, input().split()))
    s = sum(a)
    m = max(a)
    return max((m-(s-m)-1), 0)

print(f())

Submission status: Accepted

Solution code:
k, t = map(int, raw_input().split())
a = map(int, raw_input().split())

if t == 1:
    print  k - 1
    import sys
    sys.exit()

m = max(a)
p = a.index(m)

k -= 1
a[p] -= 1

while k:
    m = max(a[:p] + a[p + 1:])
    if m == 0:
        break
    p = a.index(m)
    k -= 1
    a[p] -= 1

print max(a)

Submission status: Runtime Error

Solution code:
k, t = list(map(int, input().split()))
nums = list(map(int, input().split()))

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

Submission status: Runtime Error

Solution code:
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:]))

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
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 itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
from collections import deque
#from decimal import Decimal

INF = 1 << 50

def run():
    K, T, *A = map(int, read().split())
    A = sorted(A)
    A = deque(A)
    high, low = 0, 0
    last = None
    while A:
        #print(A)
        if high == 1:
            A.appendleft(1)
            high = A.pop()

        if not high:
            high = A.pop()
            if high == 1:break
        elif not low:
            low = A.popleft()
        #print(high, low)       
        
        val = min(low, high-1)
        if not val:
            last = None
        else:
            last = 'LOW'
        high -= val
        low -= val
        #print('-->', high, low)
    if high == 1
        if last == None:
            print(max(low-high-1, 0))
            return
        else:
            print(max(low - 1, 0)))
            return
    print(high - 1 + max(low - 1, 0))

if __name__ == "__main__":
    run()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
import heapq
K, T = map(int, input().split())
m = [-int(i) for i in input().split()]
heapq.heapify(m)

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

while len(m) > 0:
    l = heapq.heappop(m)
    if len(m) == 0:
        heapq.heappush(m, l)
        break
    r = heapq.heappop(m)
    if r+1 == 0:
        continue
    else:
        heapq.heappush(m, l+1)
        heapq.heappush(m, r+1)
print(-m[0])

Submission status: Runtime Error

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

ans = max(a)

for i in a:
    if i != max(a):
        ans -= i
print(max(0, ans)

Submission status: Runtime Error

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

ans = max(a)

for i in a:
  if i != max(a):
    ans -= i
print(max(0, ans)

Submission status: Runtime Error

Solution code:
from sys import stdin

def main():
    import heapq
    input = stdin.readline
    input()
    a = [-1 * int(i) for i in input().split()]
    heapq.heapify(a)
    while len(a) > 1:
        u = heapq.heappop(a)
        v = heapq.heappop(a)
        if u + 1 < 0:
            heapq.heappush(a, u + 1)
        if v + 1 < 0:
            heapq.heappush(a, v + 1)
    print(abs(a[0] + 1))
main()

Submission status: Runtime Error

Solution code:
k,t = map(int,input().split())
A = list(map(int,input().split()))
at = max(A)
print(max(2*at−1−k,0))

Submission status: Runtime Error

Solution code:
k,t = map(int,input().split())
A = list(map(int,input().split()))
at = max(A)
print(max(at−1−(k−at),0))

Submission status: Runtime Error

Solution code:
k, t = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
double_cnt = 0
total = 0
for i in range(t):
  b = a[i] - bouble_cnt
  c = total - (bouble_cnt + 1) + 2
  bouble_cnt = max(0, b-c)
  total += a[i]
print(bouble_cnt)

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    k,t = LI()
    a = LI()
    q = []
    for i in a:
        heappush(q,-i)
    while len(q) > 1:
        x = heappop(q)
        if not q:
            heappush(q,x)
            break
        y = heappop(q)
        x += 1
        y += 1
        if x:
            heappush(q,x)
        if y:
            heappush(q,y)
    print(-q[0]-1)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    k,t = LI()
    a = LI()
    q = []
    for i in a:
        heappush(q,-i)
    while len(q) > 1:
        x = heappop(q)
        y = heappop(q)
        x += 1
        y += 1
        if x:
            heappush(q,x)
        if y:
            heappush(q,y)
    print(-q[0]-1)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
k,t=map(int,input().split())
l=[0]*t
a=list(map(int,input().split()))
for i in a:
  l[i-1]+=1
print(max(0,max(l)-(k+1)//2))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import heapq
K, T = map(int, input().split())
L = list(map(lambda x: -int(x), input().split()))

heapq.heapify(L)

while L and len(L) > 1:
    a = heapq.heappop(L)
    b = heapq.heappop(L)
    r = b-a
    if r > 0:
        heapq.heappush(L, -r)
print(-heapq.heappop(L)-1)

Submission status: Runtime Error

Solution code:
K, T = map(int, input().split())
A = [int(a) for a in input().split()]
A.sort()
sumA = 0
for a in A[:N-1]: sumA += a
print(max(0, A[N-1] - 1 - sumA))

Submission status: Runtime Error

Solution code:
k,t=map(int,input().split())
a=[]
for i in range(t):
    a.append(int(input()))
if max(a)*2-sum(a)-1<0:
    print(0) 
else:
    print(max(a)*2-sum(a)-1)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

a=sorted(a,reverse=True)

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)

Submission status: Runtime Error

Solution code:
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 a >= 0:
    print(ans)
else:
    print(0)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <math.h>
#include <fstream>
#include <algorithm>
#include <string>
#include <string.h>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
long long int maxi(long long int A, long long int B) {
	return (A > B) ? A : B;
}
int main() {
	int K, T, a, maxi = -1;
	cin >> K >> T;
	for (int i = 0; i < T; i++)
	{
		cin >> a;
		maxi = max(a, maxi);
	}
	cout << max(0, maxi - (K - maxi) - 1);
}

Submission status: Runtime Error

Solution code:
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)
  a=d.pop()
  b=d.popleft()
  if not a==1:
    d.append(a-1)
  if not b==1:
    d.appendleft(b-1)

Submission status: Runtime Error

Solution code:
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)
while d:
  if len(d)==1:
    print(d-1)
    exit()
  a=d.pop()
  b=d.popleft()
  if not a==1:
    d.append(a-1)
  d.appendleft(b-1)

Submission status: Runtime Error

Solution code:
k,t=map(int,input().split())
l=[int(i) for i in input().split()]
x=[int(i) for i in l]
nin=min(x)
nen=t

while 1:
    w=[]
    for i in x:
        if i-nin!=0:
            w.append(i-nin)
    x=[i for i in w]
    nin=min(x)
    if len(x)==1:
        print(x[0]-nin)
        exit()
    elif len(x)==0:
        print(0)
        exit()
    nen=len(x)