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()))
a_max = max(set(A))

for i in range(a_max):
    if K >= (i + 2) / (i + 1) * a_max - 1:
        print(i)
        break

Submission status: Wrong Answer

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

if T == 1:
    print(K)
else:
    for i in range(a_max):
        if K >= (i + 2) / (i + 1) * a_max - 1:
            print(i)
            break

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
import heapq
K,T = [int(x) for x in stdin.readline().rstrip().split()]
data = [(-int(b),a) for a,b in enumerate(stdin.readline().rstrip().split())]
heapq.heapify(data)
ans = 0
cake_num = -1
cake_count = 0
flag = True

if T == 1:
  ans = K-1
else:
  while flag:
    c_c,c_n = heapq.heappop(data)
    if c_n == cake_num:
        c_c2,c_n2 = heapq.heappop(data)
        if c_c2 == 0:
            ans = abs(c_c) - 2
            flag = False
        else:
            heapq.heappush(data,(c_c2+1,c_n2))
        heapq.heappush(data,(c_c,c_n))
        #flag = False
    else:
        heapq.heappush(data,(c_c+1,c_n))
    cake_num = c_n
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappop, heappush

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

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

print(-A[0] - 1)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

最大 = max(a)
最大以外 = 0
for 要素 in a:
    if 要素 == 最大: continue
    最大以外 += 要素

print(max(最大-最大以外-1, 0))

Submission status: Wrong Answer

Solution code:
K, T = tuple(map(int, input().split()))
A = list(map(int, input().split()))
if T == 1:
    print(K - 1)
    exit()

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

idx=a.index(max(a))
a[idx]-=1

for i in range(1,K):
    if idx==0:
        if max(a[1:])==0:
            print(a[0]-1)
            exit()
        else:
            idx=a[1:].index(max(a[1:]))+1
            a[idx]-=1
    elif idx==T-1:
        if max(a[:T-1])==0:
            print(a[T-1]-1)
            exit()
        else:
            idx=a[:T-1].index(max(a[:T-1]))
            a[idx]*=1
    elif max(a[:idx])>=max(a[idx+1:]):
        if max(a[0:idx])==0:
            print(a[idx]-1)
            exit()
        else:
            idx=a[0:idx].index(max(a[0:idx]))
            a[idx]-=1
    else:
        idx=a[idx+1:].index(max(a[idx+1:]))+idx+1
        a[idx]-=1

print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

print((max(A)-min(A))//t if len(A)!=1 else k-t)

Submission status: Wrong Answer

Solution code:
# code-festival-2016-qualcB - K個のケーキ / K Cakes
def main():
    K, T = tuple(map(int, input().split()))
    A = tuple(map(int, input().split()))
    if T == 1:  # corner case
        print(K - 1)
        return
    lim = (K + T - 1) // T
    ans = sum((i - lim) // 2 for i in A if i > lim)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
k,t=map(int,input().split())
a=sorted(list(map(int,input().split())))
cnt=a[0]
ans=0
if t==1:
    print(a[0]-1)
    sys.exit()
for i in range(1,t):
    if a[i]<=cnt:
        cnt+=a[i]
    else:
        ans+=a[i]-cnt-1
        cnt+=a[i]
print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

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

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

print(ans-1)

Submission status: Wrong Answer

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

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

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

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

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

print(ans-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
B - K個のケーキ / K Cakes
https://atcoder.jp/contests/code-festival-2016-qualc/tasks/codefestival_2016_qualC_b

"""
import sys


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


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


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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[0]-1)
    exit()
  a=d.pop()
  b=d.popleft()
  if not a==1:
    d.append(a-1)
  d.appendleft(b-1)

Submission status: Wrong Answer

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

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

border = -(-sum(A)//2)
over = max(A) - border

if sum(A) % 2 == 0:
    ans = max(over,0)
else:
    if over < 0:
        ans = 0
    else:
        ans = 2 + over - 1
print(ans)

Submission status: Wrong Answer

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

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

border = -(-sum(A)//2)
over = max(A) - border

ans = max(over,0)
print(ans)

Submission status: Wrong Answer

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

if T == 1:
    ans = A[0]-1
else:
    ans = max(A[-1]-1  - A[-2] - 1,0)
    
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
k,t = map(int,input().split(' '))
cake = list(map(int,input().split(' ')))
sum = 0
maxCake = -1
for i in range(t):
    maxCake = max(maxCake,cake[i])
print(maxCake - 1 - (k - maxCake))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

while a!=[a[0]]+[0]*(t-1):
    a = sorted(a,reverse=True)
    a[0],a[1] = a[0]-a[1],0

print(a[0]-1)

Submission status: Wrong Answer

Solution code:
k,t = map(int, input().split())
list_cake = list((map(int, input().split())))
list_cake.sort()
if len(list_cake) == 1:
    print(k-1)
elif list_cake[-1] <= k/2:
    print(0)
else:
    print(k - (k-list_cake[-1]) * 2)

Submission status: Wrong Answer

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

a.sort()
a.reverse()
#print(a)
res = 0

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

for i in range(t):
    if k - a[i] < k/2:
        res += (-k//2 - k%2 + a[i])
else:
    print(res)

Submission status: Wrong Answer

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

a.sort()
a.reverse()
#print(a)
res = 0

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

for i in range(t):
    if k - a[i] < k/2:
        res += (k//2 + k%2 - a[i])
else:
    print(res)

Submission status: Wrong Answer

Solution code:
k, t = map(int, input().split())
a = list(map(int, input().split()))
while len(a) > 1:
    a.sort()
    a[0] -= 1
    a[-1] -= 1
    if a[0] == 0 or a[-1] == 0:
        a.remove(0)
if len(a) == 0:
    print(0)
else:
    print(a[0]-1)

Submission status: Wrong Answer

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

a.sort()
a.reverse()
#print(a)
res = 0

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

for i in range(t):
    if k - a[i] < k/2:
        res += 1
else:
    print(res)

Submission status: Wrong Answer

Solution code:
k, t = map(int, input().split())
a = list(map(int, input().split()))
a_s = sorted(a, reverse=True)
buf = 0
for i in range(t):
    buf = abs(buf - a_s[i])
print(buf - 1)

Submission status: Wrong Answer

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

A = sorted(list(map(int,input().split())),reverse=True)

BOSS_CAKE = A[0]
first=True
for a in A[1:]:
    if first:
        first=False
        BOSS_CAKE -= a+1
    else:
        BOSS_CAKE -= a
    if BOSS_CAKE<=0:
        BOSS_CAKE = 0
        break
print(BOSS_CAKE)

Submission status: Wrong Answer

Solution code:
import math
def inpl(): return list(map(int, input().split()))
K, T = inpl()
a = inpl()

if max(a)-(sum(a)-max(a))<=1:
    ans = 0
elif T == 1:
    ans = a[0]-1
else:
    ans = math.ceil(max(a)/(sum(a)-max(a))) - 1
print(ans)

Submission status: Wrong Answer

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

if max(a)-(sum(a)-max(a))<=1:
    ans = 0
elif T == 1:
    ans = a[0]-1
else:
    ans = max(a)//(sum(a)-max(a)) - 1
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

if __name__ == "__main__":
    K,T = map(int, input().split())
    data = list(map(int, input().split()))
    cake = []
    for i,x in enumerate(data):
        cake.append([x, i])
    heapq._heapify_max(cake)
    while (len(cake) > 1):
        p = heapq.heappop(cake)
        q = heapq.heappop(cake)
        p[0] -= 1
        q[0] -= 1
        if p[0] > 0:
            heapq.heappush(cake, p)
        if q[0] > 0:
            heapq.heappush(cake, q)
    result = 0
    if len(cake) > 0:
        t = heapq.heappop(cake)
        result = t[0]
    print (max(0, result - 1))

Submission status: Wrong Answer

Solution code:
k_t = input().split()
k = int(k_t[0])
t = int(k_t [1])
t_amount = input().split(' ')
 
while True:
    if t == 1:
        max = k
        break
    max = 0
    for i in range(t-1):
        if t_amount[i+1] >= t_amount [i]:
            max = t_amount[i+1]
        elif max == 0:
            max = t_amount[0]
    t_amount.remove(max)
 
    max2 = 0
    for i in range(t-2):
        if t_amount[i+1] >= t_amount [i]:
            max2 = t_amount[i+1]
        elif max2 == 0 :
            max2 = t_amount[0]
    t_amount.remove(max2)
    if max2 == "0":
        break
    else:
        max = int(max) -1
        max2 = int(max2) -1
        t_amount.append(str(max))
        t_amount.append(str(max2))
 
if max == "0":
    print(0)
else:
    print(int(max)-1)

Submission status: Wrong Answer

Solution code:
k_t = input().split()
k = int(k_t[0])
t = int(k_t [1])
t_amount = input().split(' ')

while True:
    max = 0
    for i in range(t-1):
        if t_amount[i+1] >= t_amount [i]:
            max = t_amount[i+1]
        elif max == 0:
            max = t_amount[0]
    t_amount.remove(max)

    max2 = 0
    for i in range(t-2):
        if t_amount[i+1] >= t_amount [i]:
            max2 = t_amount[i+1]
        elif max2 == 0 :
            max2 = t_amount[0]
    t_amount.remove(max2)
    if max2 == "0":
        break
    else:
        max = int(max) -1
        max2 = int(max2) -1
        t_amount.append(str(max))
        t_amount.append(str(max2))

if max == "0":
    print(0)
else:
    print(int(max)-1)

Submission status: Wrong Answer

Solution code:
a = raw_input().split()
b = raw_input().split()

a=a
b=b
b_max = max(b)



if round(float(a[0])/2) >float(a[0])/2:
    b_mean = round(float(a[0])/2)
else:
    b_mean = round(float(a[0])/2)+1

if a[1]=='1':
    print 0

elif int(b_max) > int(b_mean):
    print int(b_max)-int(b_mean)
else:
    print 0

Submission status: Wrong Answer

Solution code:
# def makelist(n, m):
# 	return [[0 for i in range(m)] for j in range(n)]

# n = int(input())
# a, b = map(int, input().split())
# s = input()


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

a = [0]*T
for i in range(T):
	a[i] = (hoge[i], i)

if T == 1:
	print(K-1)
else:
	a.sort()
	a[-1] = (a[-1][0] - 1, a[-1][1])
	pre = a[-1][1]

	while True:
		a.sort()
		if a[-1][0] == 0:
			print(0)
			break
		elif a[-1][1] != pre:
			a[-1] = (a[-1][0] - 1, a[-1][1])
			pre = a[-1][1]
		else:
			if a[-2][0] == 0:
				print(a[-1][1])
				break
			else:
				a[-2] = (a[-2][0] - 1, a[-2][1])
				pre = a[-2][1]

Submission status: Wrong Answer

Solution code:
a = raw_input().split()
b = raw_input().split()

a=a
b=b
b_max = max(b)
if round(float(a[0])/2) >float(a[0])/2:
    b_mean = round(float(a[0])/2)
else:
    b_mean = round(float(a[0])/2)+1

if int(b_max) > int(b_mean):
    print int(b_max)-int(b_mean)
else:
    print 0

Submission status: Wrong Answer

Solution code:
# def makelist(n, m):
# 	return [[0 for i in range(m)] for j in range(n)]

# n = int(input())
# a, b = map(int, input().split())
# s = input()


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

a = [0]*T
for i in range(T):
	a[i] = (hoge[i], i)

if T == 1:
	print(K-1)
else:
	a.sort()
	a[-1] = (a[-1][0] - 1, a[-1][1])
	pre = a[-1][1]

	while True:
		a.sort()
		if a[-1][0] == 0:
			print(0)
			break
		elif a[-1][1] != pre:
			a[-1] = (a[-1][0] - 1, a[-1][1])
			pre = a[-1][1]
		else:
			if a[-2][0] == 0:
				print(a[-1][1])
				break
			else:
				a[-2] = (a[-2][0] - 1, a[-2][1])
				pre = a[-2][1]
		print(a)

Submission status: Wrong Answer

Solution code:
a = raw_input().split()
b = raw_input().split()

a=a
b=b
b_max = max(b)
b_mean = round(float(a[0])/2)

if int(b_max) > int(b_mean):
    print int(b_max)-int(b_mean)
else:
    print 0

Submission status: Wrong Answer

Solution code:
def read_input():
    l_1 = input()
    day = int(l_1.split(" ")[0])

    num_cake = [int(i) for i in input().split(" ")]

    return day, num_cake

def get_max_index(target_list):
    return [i for i, j in enumerate(target_list) if j == max(target_list)]


def get_second_max_index(target_list):
    return [i for i, j in enumerate(target_list) if j == list(sorted(set(target_list), reverse=True))[1]]


def mog(day, num_cake):
    """

    >>> mog(7, [3,2,2])
    0
    >>> mog(6, [1,4,1])
    1
    >>> mog(100, [100])
    99
    """
    printed = False
    previous_index = -1

    for i in range(0, day):
        # print("num_cake: ", num_cake)
        # print("previous_index: ", previous_index)
        # ate_flag = False
        mog_maybe_index = get_max_index(num_cake)
        # print("mog_maybe_index: {0}".format(mog_maybe_index))
        for n in mog_maybe_index:
            if n == previous_index:
                continue
            else:
                num_cake[n] = num_cake[n] - 1
                # ate_flag = True
                previous_index = n
                break
        # if ate_flag is False:
        try:
            mog_ext = get_second_max_index(num_cake)
            # print("mog_ext", mog_ext)
            if num_cake[mog_ext[0]] == 0:
                printed = True
                print(max(num_cake))
                break
        except IndexError:
            print(max(num_cake))
            printed = True
            break
        num_cake[mog_ext[0]] = num_cake[mog_ext[0]] - 1
        previous_index = mog_ext[0]

    if printed is False:
        print(0)
if __name__ == "__main__":
    day, num_cake = read_input()
    mog(day, num_cake)

Submission status: Wrong Answer

Solution code:
# def makelist(n, m):
# 	return [[0 for i in range(m)] for j in range(n)]

# n = int(input())
# a, b = map(int, input().split())
# s = input()


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

a = [0]*T
for i in range(T):
	a[i] = (hoge[i], i)

if T == 1:
	print(K-1)
else:
	a.sort()
	a[-1] = (a[-1][0] - 1, a[-1][1])
	pre = a[-1][1]

	while True:
		a.sort()
		if a[-1][0] == 0:
			print(0)
			a[T+2]
			break
		elif a[-1][1] != pre:
			a[-1] = (a[-1][0] - 1, a[-1][1])
			pre = a[-1][1]
		else:
			if a[-2][0] == 0:
				print(a[-1][1])				
				break
			else:
				a[-2] = (a[-2][0] - 1, a[-2][1])
				pre = a[-2][1]

Submission status: Wrong Answer

Solution code:
K,T = map(int,input().split())
a =input().split()
a = list(int(i) for i in a)
a.sort()
if T >= 2:
    while a[len(a)-2] >= 1:
        a[len(a)-1] = a[len(a)-1] - 1
        a[len(a)-2] = a[len(a)-2] - 1
        a.sort()
    print(a[len(a)-1]-1)
else:
    print(a[len(a)-1]-1)

Submission status: Wrong Answer

Solution code:
# def makelist(n, m):
# 	return [[0 for i in range(m)] for j in range(n)]

# n = int(input())
# a, b = map(int, input().split())
# s = input()


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

a = [0]*T
for i in range(T):
	a[i] = (hoge[i], i)

if T == 1:
	print(K-1)
else:
	a.sort()
	a[-1] = (a[-1][0] - 1, a[-1][1])
	pre = a[-1][1]

	while True:
		a.sort()
		if a[-1][0] == 0:
			print(0)
			break
		elif a[-1][1] != pre:
			a[-1] = (a[-1][0] - 1, a[-1][1])
			pre = a[-1][1]
		else:
			if a[-2][0] == 0:
				print(a[-1][1])
				break
			else:
				a[-2] = (a[-2][0] - 1, a[-2][1])
				pre = a[-2][1]

Submission status: Wrong Answer

Solution code:
# def makelist(n, m):
# 	return [[0 for i in range(m)] for j in range(n)]

# n = int(input())
# a, b = map(int, input().split())
# s = input()


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

a = [0]*T
for i in range(T):
	a[i] = (hoge[i], i)

if T == 1:
	print(K-1)
else:
	a.sort()
	a[-1] = (a[-1][0] - 1, a[-1][1])
	pre = a[-1][1]

	for i in range(K-1):
		a.sort()
		if a[-1][0] == 0:
			print(0)
			break
		elif a[-1][1] != pre:
			a[-1] = (a[-1][0] - 1, a[-1][1])
			pre = a[-1][1]
		else:
			if a[-2][0] == 0:
				print(a[-1][1])
				break
			else:
				a[-2] = (a[-2][0] - 1, a[-2][1])
				pre = a[-2][1]

Submission status: Wrong Answer

Solution code:
def read_input():
    l_1 = input()
    day = int(l_1.split(" ")[0])

    num_cake = [int(i) for i in input().split(" ")]

    return day, num_cake

def get_max_index(target_list):
    return [i for i, j in enumerate(target_list) if j == max(target_list)]


def get_second_max_index(target_list):
    return [i for i, j in enumerate(target_list) if j == list(sorted(set(target_list), reverse=True))[1]]


def mog(day, num_cake):
    """

    >>> mog(7, [3,2,2])
    0
    >>> mog(6, [1,4,1])
    1
    >>> mog(100, [100])
    99
    """
    printed = False
    previous_index = -1

    for i in range(0, day):
        #print("num_cake: ", num_cake)
        #print("previous_index: ",previous_index)
        ate_flag = False
        mog_maybe_index = get_max_index(num_cake)
        # print("mog_maybe_index: {0}".format(mog_maybe_index))
        for n in mog_maybe_index:
            if n == previous_index:
                continue
            else:
                num_cake[n] = num_cake[n] - 1
                ate_flag = True
                previous_index = n
                break
        if ate_flag is False:
            try:
                mog_ext = get_second_max_index(num_cake)
                # print("mog_ext", mog_ext)
                if mog_ext[0] == 0:
                    printed = True
                    print(max(num_cake))
            except IndexError:
                print(max(num_cake))
                printed = True
                break
            num_cake[mog_ext[0]] = num_cake[mog_ext[0]] - 1
            previous_index = mog_ext[0]

    if printed is False:
        print(0)



if __name__ == "__main__":
    # import doctest
    # doctest.testmod()

    day, num_cake = read_input()
    mog(day, num_cake)

Submission status: Wrong Answer

Solution code:
def read_input():
    l_1 = input()
    day = int(l_1.split(" ")[0])
 
    num_cake = [int(i) for i in input().split(" ")]
 
    return day, num_cake
 
def get_max_index(target_list):
    return [i for i, j in enumerate(target_list) if j == max(target_list)]
 
 
def get_second_max_index(target_list):
    return [i for i, j in enumerate(target_list) if j == list(sorted(set(target_list), reverse=True))[1]]
 
 
def mog(day, num_cake):
    """
 
    >>> mog(7, [3,2,2])
    0
    >>> mog(6, [1,4,1])
    1
    >>> mog(100, [100])
    99
    """
    printed = False
    previous_index = -1
 
    for i in range(0, day):
        # print(num_cake)
        # print(previous_index)
        ate_flag = False
        mog_maybe_index = get_max_index(num_cake)
        # print("mog_maybe_index: {0}".format(mog_maybe_index))
        for n in mog_maybe_index:
            if n == previous_index:
                continue
            else:
                num_cake[n] = num_cake[n] - 1
                ate_flag = True
                previous_index = n
                break
        if ate_flag is False:
            try:
                mog_ext = get_second_max_index(num_cake)
                # print("mog_ext", mog_ext)
            except IndexError:
                print(max(num_cake))
                printed = True
                break
            num_cake[mog_ext[0]] = num_cake[mog_ext[0]] - 1
            previous_index = mog_ext[0]
 
    if printed is False:
        print(0)
 
 
 
if __name__ == "__main__":

    day, num_cake = read_input()
    mog(day, num_cake)

Submission status: Wrong Answer

Solution code:
def read_input():
    l_1 = input()
    day = int(l_1.split(" ")[0])

    num_cake = [int(i) for i in input().split(" ")]

    return day, num_cake

def get_max_index(target_list):
    return [i for i, j in enumerate(target_list) if j == max(target_list)]


def get_second_max_index(target_list):
    return [i for i, j in enumerate(target_list) if j == list(sorted(set(target_list), reverse=True))[1]]


def mog(day, num_cake):
    """

    >>> mog(7, [3,2,2])
    0
    >>> mog(6, [1,4,1])
    1
    >>> mog(100, [100])
    99
    """
    printed = False
    previous_index = -1

    for i in range(0, day):
        # print(num_cake)
        # print(previous_index)
        ate_flag = False
        mog_maybe_index = get_max_index(num_cake)
        # print("mog_maybe_index: {0}".format(mog_maybe_index))
        for n in mog_maybe_index:
            if n == previous_index:
                continue
            else:
                num_cake[n] = num_cake[n] - 1
                ate_flag = True
                previous_index = n
                break
        if ate_flag is False:
            try:
                mog_ext = get_second_max_index(num_cake)
                # print("mog_ext", mog_ext)
            except IndexError:
                print(max(num_cake))
                printed = True
                break
            num_cake[mog_ext[0]] = num_cake[mog_ext[0]] - 1
            previous_index = mog_ext[0]

    if printed is False:
        print(0)



if __name__ == "__main__":
    import doctest
    doctest.testmod()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import re
import math
import itertools
import collections
import bisect
import heapq
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def fib(n): return [(seq.append(seq[i-2] + seq[i-1]), seq[i-2])[1] for seq in [[0, 1]] for i in range(2, n)]
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def eucl(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0
def pscl(num,l=[1]):
    for i in range(num):
        l = map(lambda x,y:x+y,[0]+l,l+[0])
    return l

#n=int(raw_input())
n,k=map(int,raw_input().split())
l=[100-int(x) for x in raw_input().split()]
if k==1:
    print 100-l[0]-1
    exit()
elif len(set(l))==1:
    print 0
    exit()
ans=chk=0
heapq.heappush(l,0)
heapq.heappop(l)
for i in range(n):
    if i%2==0:
        tmp=heapq.heappop(l)
        if tmp+1<100:
            heapq.heappush(l,tmp+1)
    else:
        tmp=heapq.heappop(l)
        tmp2=heapq.heappop(l)
        if tmp2+1<100:
            heapq.heappush(l,tmp2+1)
        heapq.heappush(l,tmp)
    if len(l)==1:
        print 100-l[0]-1
        exit()
print 0


#end = time.clock()
#print end - start

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
import re
import math
import itertools
import collections
import bisect
import heapq
#sys.stdin=file('input.txt')
#sys.stdout=file('output.txt','w')
#10**9+7
mod=1000000007
#mod=1777777777
pi=3.141592653589
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def fib(n): return [(seq.append(seq[i-2] + seq[i-1]), seq[i-2])[1] for seq in [[0, 1]] for i in range(2, n)]
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def eucl(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0
def pscl(num,l=[1]):
    for i in range(num):
        l = map(lambda x,y:x+y,[0]+l,l+[0])
    return l

#n=int(raw_input())
n,k=map(int,raw_input().split())
l=[100-int(x) for x in raw_input().split()]
if k==1:
    print 100-l[0]-1
    exit()
ans=chk=0
heapq.heappush(l,0)
heapq.heappop(l)
for i in range(n):
    if i%2==0:
        tmp=heapq.heappop(l)
        if tmp+1<100:
            heapq.heappush(l,tmp+1)
    else:
        tmp=heapq.heappop(l)
        tmp2=heapq.heappop(l)
        if tmp2+1<100:
            heapq.heappush(l,tmp2+1)
        heapq.heappush(l,tmp)
    if len(l)==1:
        print 100-l[0]-1
        exit()
print 0


#end = time.clock()
#print end - start