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: Wrong Answer

Solution code:
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(a[0]-1)

Submission status: Wrong Answer

Solution code:
def main():
    K, T = list(map(int, input().split()))
    A = list(map(int, input().split()))
    if T == 1:
        print(K - 1)
    else:
        print(max(0, max(A) - (K + 1) // 2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
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(abs(b - c) - 1)

Submission status: Wrong Answer

Solution code:
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)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
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)
if n==1:
    print(k-1)
else:
    print(max(m - math.ceil(k/2), 0))

Submission status: Wrong Answer

Solution code:
import math
k, t = map(int, input().split())
a = list(map(int, input().split()))
max_a = max(a)
rest_a = sum(a) - max_a
#ans = math.ceil((max_a / (rest_a + 1))) - 1
if max_a % (rest_a + 1) == 0:
    ans = max_a // (rest_a + 1)
    ans -= 1
else:
    ans = max_a // (rest_a + 1)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
k, t = map(int, input().split())
a = list(map(int, input().split()))
max_a = max(a)
rest_a = sum(a) - max_a
ans = math.ceil((max_a / (rest_a + 1))) - 1
print(ans)

Submission status: Wrong Answer

Solution code:
a = list(map(int, input().split()))
largest = a[0]
total = 0

for ai in a:
    total += ai

    if ai > largest:
        largest = ai

print(max(largest-(total-largest+1), 0))

Submission status: Wrong Answer

Solution code:
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-half)
    print(ans)

Submission status: Wrong Answer

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

val = sum(a[:-1])
num = abs(val-a[-1])

if num <= 1 or abs(k-t) <= 1:
    print(0)
else:   
    print(min(num-1, abs(k-t)))

Submission status: Wrong Answer

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

val = sum(a[:-1])
num = abs(val-a[-1])

if k == t or num <= 1:
    print(0)
else:
    print(num-1)

Submission status: Wrong Answer

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

val = sum(a[:-1])
num = abs(val-a[-1])

if num <= 1:
    print(0)
else:
    print(num-1)

Submission status: Wrong Answer

Solution code:
k,t = map(int,input().split())
a = list(map(int,input().split()))
if max(a)<=k-max(a)+1:
  print(0)
elif t == 1:
  print(k-1)
else:
  if max(a) % (k-max(a)+1) == 0:
    print(max(a) // (k-max(a)+1) -1)
  else:
    print(max(a) // (k-max(a)+1))

Submission status: Wrong Answer

Solution code:
k,t = map(int,input().split())
a = list(map(int,input().split()))
if max(a)<=k-max(a)-1:
  print(0)
elif t == 1:
  print(k-1)
else:
  if max(a) % (k-max(a)+1) == 0:
    print(max(a) // (k-max(a)+1) -1)
  else:
    print(max(a) // (k-max(a)+1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
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])
print(cakes)
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(cakes)
print(ans)

Submission status: Wrong Answer

Solution code:
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])
# print(cakes)
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][1]:
        next_cake_num, next_cake_idx = heapq.heappop(cakes)
        next_cake_num -= 1
        heapq.heappush(cakes, [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, [cake_num, cake_idx])
print(ans)

Submission status: Wrong Answer

Solution code:
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])
print(cakes)
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][1]:
        next_cake_num, next_cake_idx = heapq.heappop(cakes)
        next_cake_num -= 1
        heapq.heappush(cakes, [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, [cake_num, cake_idx])
print(ans)

Submission status: Wrong Answer

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: Wrong Answer

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
    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)
        high -= val
        low -= val
        #print('-->', high, low)
    if high == 1 and A:
        high += len(A)
        print(max(0, low - high - 1))
    print(high - 1 + max(low - 1, 0))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

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
    while A:
        #print(A)
        if high == 1:
            A.appendleft(1)
            high = 0
            high = A.pop()

        if not high:
            high = A.pop()
        elif not low:
            low = A.popleft()
        #print(high, low)
        val = min(low, high-1)
        high -= val
        low -= val
        #print('-->', high, low)

    print(high - 1 + max(low - 1, 0))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

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
    while A:
        #print(A)
        #if high == 1:
        #    A.appendleft(1)
        #    high = 0

        if not high:
            high = A.pop()
        elif not low:
            low = A.popleft()
        #print(high, low)
        val = min(low, high-1)
        high -= val
        low -= val
        #print('-->', high, low)

    print(high - 1 + max(low - 1, 0))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

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
    while A:
        #print(A)
        if high == 1:
            A.appendleft(1)
            high = 0

        if not high:
            high = A.pop()
        elif not low:
            low = A.popleft()
        #print(high, low)
        val = min(low, high-1)
        high -= val
        low -= val
        #print('-->', high, low)

    print(high - 1 + max(low - 1, 0))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

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
    while A:
        #print(A)
        if high == 1:
            A.appendleft(1)
            high = 0

        if not high:
            high = A.pop()
        elif not low:
            low = A.popleft()
        #print(high, low)
        val = min(low, high-1)
        high -= val
        low -= val
        #print('-->', high, low)
        if not A and high == 1 and low >= 1:
            low += 1

    print(high - 1 + max(low - 1, 0))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

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
    while A:
        #print(A)
        if high == 1:
            A.appendleft(1)
            high = 0

        if not high:
            high = A.pop()
        elif not low:
            low = A.popleft()
        #print(high, low)
        val = min(low, high)
        high -= val
        low -= val
        #print('-->', high, low)
        if not A and high == 0 and low >= 1:
            low += 1

    print(high - 1 + max(low - 1, 0))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

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
    while A:
        #print(A)
        if high == 1:
            A.appendleft(1)
            high = 0

        if not high:
            high = A.pop()
        elif not low:
            low = A.popleft()
        #print(high, low)
        val = min(low, high)
        high -= val
        low -= val
        #print('-->', high, low)
        if not A and high == 0 and low > 1:
            low += 1

    print(high - 1 + max(low - 1, 0))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

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
    while A:
        #print(A)
        if high == 1 and A:
            A.appendleft(1)
            high = 0

        if not high:
            high = A.pop()
        elif not low:
            low = A.popleft()
        #print(high, low)
        val = min(low, high)
        high -= val
        low -= val
        #print('-->', high, low)

    print(high - 1 + max(low - 1, 0))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

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
    while A:
        #print(A)
        if high == 1:
            A.appendleft(1)
            high = 0

        if not high:
            high = A.pop()
        elif not low:
            low = A.popleft()
        #print(high, low)
        val = min(low, high)
        high -= val
        low -= val
        #print('-->', high, low)

    print(high - 1 + max(low - 1, 0))

if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
k, t, *a = map(int, open(0).read().split())
ans = 0
for i in a:
    if ans > 0:
        ans -= i
    else:
        ans += i
print(abs(ans) - (ans!=0))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    K, T = LI()
    a = LI()

    # aを2グループに分けて交互に食べれば良い
    # 最も差の少ない2グループの分け方を知りたい

    a.sort(reverse=True)
    a_cum = [0] * (T + 1)
    for i in range(T):
        a_cum[i+1] = a_cum[i] + a[i]

    ans = INF
    for i in a_cum:
        ans = min(abs(2 * i - K) - 1, ans)

    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil, atan, pi
def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left
MOD = int(1e9)+7
INF = float('inf')


def solve():
    # n, m = [int(x) for x in input().split()]  
    k, t = [int(x) for x in input().split()]  
    a = [int(x) for x in input().split()]  
    x = k // 2

    if t == 1:
        print(k-1)
        return

    for e in a:
        if e >= x:
            print(e - x)
            return

    print(0)

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""
a b a
d c d
a b a
"""

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil, atan, pi
def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left
MOD = int(1e9)+7
INF = float('inf')


def solve():
    # n, m = [int(x) for x in input().split()]  
    k, t = [int(x) for x in input().split()]  
    a = [int(x) for x in input().split()]  
    x = (k + 1) // 2

    if t == 1:
        print(k-1)
        return

    for e in a:
        if e >= x:
            print(e - x)
            return

    print(0)

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""
a b a
d c d
a b a
"""

Submission status: Wrong Answer

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

A.sort()
t = sum(A) - A[-1]
if T == 1:
    print(K-1)
elif t > A[-1]:
    print(0)
else:
    print(t-1)

Submission status: Wrong Answer

Solution code:
n,t,*aa = map(int, open(0).read().split())

aa.sort(reverse=True)

consum_max = 2 if t>2 else 1

n -= sum(aa[2:])

if t > 1:
    n -= aa[1]*2
n -= consum_max

print(max(0,n))

Submission status: Wrong Answer

Solution code:
k, t = map(int, input().split())
A = tuple(map(int, input().split()))
if t == 1:
    print(k-1)
else:
    while len(A) > 1:
        ma = min(A)
        A = [a - ma for a in A if a-ma > 0]
    
    if len(A) == 0:
        print(0)
    else:
        if A[0] == 1:
            # 頭でも末尾でもおける
            print(0)
        else:
            # 頭or末尾で連続しないようにおいて余った分が連続する
            print(A[0]-2)

Submission status: Wrong Answer

Solution code:
k, t = map(int, input().split())
A = list(map(int, input().split()))
_max = max(A)
flag = False
cnt = 0
for i in range(t):
    if A[i] == _max:
        flag = True
        continue
    cnt += A[i]
    if _max <= cnt:
        print(0)
        exit()
    if A[i] == _max and flag:
        print(0)
        exit()
ans = max(0, _max - cnt - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
k, t = map(int, input().split())
A = list(map(int, input().split()))
_max = max(A)
flag = False
cnt_without_max = 0
for i in range(t):
    if A[i] == _max:
        flag = True
        continue
    cnt_without_max += A[i]
    if A[i] == _max and flag:
        print(0)
        exit()
ans = max(0, _max - cnt_without_max - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
#mod = 10 ** 9 + 7 
mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

K, T = MAP()
a = LIST()
A_max = max(a)

if 2*A_max < K-1:
	print(0)
else:
	c = K - A_max + 1
	print(-(-K+c)//c)

Submission status: Wrong Answer

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

if len(A) == 1:
    print(A[0]-1)
else:
    A.sort()
    A[-1] -= 1
    A.sort()
    
    if A[-1] == A[-2]:
        print(0)
    else:
        while A[-2] > 0:
            A = list(map(lambda x:x-1,A))
        print(A[-1]-1)

Submission status: Wrong Answer

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

if len(A) == 1:
    print(A[0]-1)
else:
    A.sort()
    A[-1] -= 1
    A.sort()
    
    #最後の一種類になるまで交互にできる
    while A[-2] > 0:
        A = list(map(lambda x:x-1,A))
    print(max(0, A[-1]-1))

Submission status: Wrong Answer

Solution code:
import math

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


c = max(a)

if K == 1:
    print(0)
elif T == 1:
    print(K-1)
elif c > math.ceil(K/2):
    if K % 2 == 0:
        print(c-math.ceil(K/2))
    else:
        print((c-math.ceil(K/2))*2)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import math

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


c = max(a)

if K == 1:
    print(0)
elif T == 1:
    print(K-1)
elif c > math.ceil(K/2):
    print(c-math.ceil(K/2))
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import math

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


c = max(a)

if T == 1:
    print(K-1)
elif c > math.ceil(K/2):
    print(c-math.ceil(K/2))
else:
    print(0)

Submission status: Wrong Answer

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

from collections import deque
d={}
numbers=deque([])
for i in range(t):
    d[i+1]=a[i]

d=sorted(d.items(),key=lambda x: x[1],reverse=True)

for j in d:
    numbers.extend([j[0]]*j[1])

seq=[]

for k in range(len(numbers)):
    if k%2==0:
        b=numbers.popleft()
    else:
        b=numbers.pop()
    seq.append(b)


from itertools import groupby

length=0
gr =groupby(seq)
for key, group in gr:
    length=max(length, len(list(group)))

print(length-1)

Submission status: Wrong Answer

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) > 1:
    print(m)
    l = heapq.heappop(m)
    r = heapq.heappop(m)
    print(m)
    if r+1 != 0:
        heapq.heappush(m, l+1)
        heapq.heappush(m, r+1)
    else:
        if l+1 != 0:
            heapq.heappush(m, l+1)
if len(m) == 0:
    print(0)
else:
    print(-m[0]-1)

Submission status: Wrong Answer

Solution code:
K, T = map(int, input().split())
A = sorted(map(int, input().split()))
sum_A = sum(A)
if -1 <= sum_A - 2 * A[-1] <= 1:
    print(0)
else:
    print(abs(sum_A - 2 * A[-1]) - 1)

Submission status: Wrong Answer

Solution code:
k,t = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
if t == 1:
    print(A[0]-1)
    exit()

x = sum(A)
ans = []
for a in A[:-1]:
    x -= a
    if a <= x or a-x-1 == 0: continue
    ans.append(a-x-1)
print(sum(ans))

Submission status: Wrong Answer

Solution code:
k,t = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
if t == 1:
    print(A[0]-1)
    exit()

x = sum(A)
ans = []
for a in A[:-1]:
    x -= a
    if a <= x: continue
    ans.append(a-x)
print(sum(ans)-1 if ans else 0)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
K, T = map(int, sys.stdin.readline().rstrip().split())
A = list(map(int, sys.stdin.readline().rstrip().split()))
A.sort() # 数が大きいものは最後に残す
num = T - 1 # 連続せずに挿入できる箇所
ans = 0 # 連続している部分の数
for a in A:
    add = a - 1
    if ans == 0:
        ans = max(add - num, 0)
    else:
        add = max(0, add - ans)
        ans = max(0, ans - add)
        if add >= num:
            ans = add - num
    num += a - 1 - ans
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
K, T = map(int, sys.stdin.readline().rstrip().split())
A = list(map(int, sys.stdin.readline().rstrip().split()))
A.sort() # 数が大きいものは最後に残す
num = T - 1 # 連続せずに挿入できる箇所
ans = 0 # 連続している部分の数
for a in A:
    add = a - 1
    if ans == 0:
        ans = max(add - num, 0)
    else:
        add = max(0, add - ans)
        ans = max(0, ans - add)
        if add >= num:
            ans += add - num
        else:
            ans = 0
    num += a - 1 - ans
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
K, T = map(int, sys.stdin.readline().rstrip().split())
A = list(map(int, sys.stdin.readline().rstrip().split()))
A.sort() # 数が大きいものは最後に残す
num = T - 1 # 挿入できる箇所（連続していない部分）
ans = 0 # 連続している部分の数
for a in A:
    add = a - 1
    if ans == 0:
        ans = max(add - num, 0)
    else:
        if add >= ans:
            ans = 0
            add -= ans
        else:
            ans -= add
            add = 0
        if add >= num:
            ans = add - num
        else:
            ans = 0
    num += add - ans
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
K, T = map(int, sys.stdin.readline().rstrip().split())
A = list(map(int, sys.stdin.readline().rstrip().split()))
A.sort() # 数が大きいものは最後に残す
num = T - 1 # 挿入できる箇所（連続していない部分）
ans = 0 # 連続している部分の数
for a in A:
    add = a - 1
    if ans == 0:
        ans = max(add - num, 0)
    else:
        if add >= ans:
            ans = 0
            add -= ans
        else:
            ans -= add
            add = 0
        if add >= num:
            ans = add - num
    num += add - ans
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
K, T = map(int, sys.stdin.readline().rstrip().split())
A = np.array(list(map(int, sys.stdin.readline().rstrip().split())))
if T == 1:
    print(K - 1)
    exit()
while np.count_nonzero(A > 0) > 1:
    A -= 1
A_max = np.max(A)
ans = max(A_max - 2, 0)
print(ans)

Submission status: Wrong Answer

Solution code:
k,t=map(int,input().split())
a=list(map(int,input().split()))
if max(a)<=sum(a)-max(a)+1:
    print(0)
else:
    if t==1:
        print(a[0]-1)
    else:
        print(sum(a)-max(a)-1)