Problem:


Score : 700 points

Problem Statement

There are N cats.
We number them from 1 through N.

Each of the cats wears a hat.
Cat i says: "there are exactly a_i different colors among the N - 1 hats worn by the cats except me."

Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i ≤ N-1

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print Yes if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print No otherwise.

Sample Input 1

3
1 2 2

Sample Output 1

Yes

For example, if cat 1, 2 and 3 wears red, blue and blue hats, respectively, it is consistent with the remarks of the cats.

Sample Input 2

3
1 1 2

Sample Output 2

No

From the remark of cat 1, we can see that cat 2 and 3 wear hats of the same color.
Also, from the remark of cat 2, we can see that cat 1 and 3 wear hats of the same color.
Therefore, cat 1 and 2 wear hats of the same color, which contradicts the remark of cat 3.

Sample Input 3

5
4 3 4 3 4

Sample Output 3

No

Sample Input 4

3
2 2 2

Sample Output 4

Yes

Sample Input 5

4
2 2 2 2

Sample Output 5

Yes

Sample Input 6

5
3 3 3 3 3

Sample Output 6

No



Sample input:
3
1 2 2


Sample output:
Yes

Submission status: Accepted

Solution code:
n=int(input())
a=map(int,input().split())
c=[0]*n
col=0
mx=0
for i in a:
	if c[i]==0:
		col+=1
	c[i]+=1
	mx=max(mx,i)
if col>2:
	print("No")
elif col==1:
	if mx==n-1 or 2*mx<=n:
		print("Yes")
	else:
		print("No")
else:
	x,y=c[mx-1],c[mx]
	if x+1<=mx and mx<=x+y/2:
		print("Yes")
	else:
		print("No")

Submission status: Accepted

Solution code:
N = int(input())
ls = list(map(int, input().split()))
o = max(ls)
p = min(ls)

#if ((o == p) and (N >= 2 * o)):
if (o == p):
    if (o == (N - 1) or (N >= 2 * o)):
        print('Yes')
    else:
        print('No')
elif ((o - p) == 1):
    u = len([ x for x in ls if x == p])
    if ((o - u) > 0 and 2 * (o - u) <= (N - u)):
        print('Yes')
    else:
        print('No')
else:
    print('No')

Submission status: Accepted

Solution code:
import sys
if __name__ == "__main__":
    N = int(input())
    evidences = list(map(int, input().split()))
    evidences = list(sorted(evidences))
    unique_evidence = set(evidences)
    if (evidences[-1] - evidences[0] > 1):
        print ("No")
        sys.exit()
    if (len(unique_evidence) == 1):
        group = evidences[0];
        # all unique
        if (group == N - 1):
            print ("Yes")
            sys.exit()
        if (N - 2 * group < 0):
            print ("No")
            sys.exit()
        print ("Yes")
        sys.exit()
    # evidences[-1] - evidences[0] > 1
    min_val = min(evidences)
    max_val = max(evidences)

    min_val_cnt = 0
    max_val_cnt = 0
    for val in evidences:
        if (val == min_val):
            min_val_cnt += 1
        else:
            max_val_cnt += 1
    # violate not unique
    if (max_val_cnt <= 1):
        print ("No")
        sys.exit()
    #rest cat
    rest = N
    rest -= min_val_cnt

    min_group = min_val_cnt + 1
    max_group = min_val_cnt + (max_val_cnt // 2)
    if min_group <= evidences[-1] and evidences[-1] <= max_group:
        print ("Yes")
        sys.exit()
    print ("No")

Submission status: Accepted

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

mx = max(a)
mn = min(a)
if (mx-mn) >= 2 :
    print('No')
elif mx == mn == N-1 :
    print('Yes')
elif mx == mn :
    if N >= mx*2 :
        print('Yes')
    else :
        print('No')
elif (mx-mn) == 1 :
    mx_cnt = 0
    mn_cnt = 0
    for i in range(N) :
        if a[i] == mx :
            mx_cnt += 1
        else :
            mn_cnt += 1

    if mn < mn_cnt :
        print('No')
    elif mx_cnt >= (mx-mn_cnt)*2:
        print('Yes')
    else :
        print('No')

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split(' ')]
maxA = max(A)
minA = min(A)

if maxA - minA > 1:
    print('No')
elif maxA == minA:
    if (N >= 2 * maxA) or maxA == N - 1:
        print('Yes')
    else:
        print('No')
else:
    sum = 0
    for i in A:
        if i == minA:
            sum += 1
    if sum >= maxA:
        print('No')
    elif 2 * (maxA - sum) <= N - sum:
        print('Yes')
    else:
        print('No')

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
mina=min(a)
maxa=max(a)
if mina == maxa:
    if 2*maxa <= n or maxa == n-1:
        print('Yes')
    else:
        print('No')
elif maxa - mina == 1:
    cntmin = a.count(mina)
    if maxa - cntmin <= 0:
        print('No')
    elif n + cntmin >= 2 * maxa:
        print('Yes')
    else:
        print('No')
else:
    print('No')

Submission status: Accepted

Solution code:
import numpy as np

N=int(input())
line=input()
arr=np.array([int(ai) for ai in line.split()])

maxNum=np.max(arr)
minNum=np.min(arr)

if maxNum==minNum:#全て等しい場合
    if maxNum==(N-1):#全てC-1のとき
        print("Yes")
    else:#全てCのとき
        C=maxNum
        numUniq=0
        if numUniq>=2*C-N and numUniq<C:
            print("Yes")
        else:
            print("No")
elif maxNum==minNum+1:
    C=maxNum
    numUniq=(arr==minNum).sum()
    if numUniq>=2*C-N and numUniq<C:
        print("Yes")
    else:
        print("No")
else:
    print("No")

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]

A_set = sorted(set(A))
if len(A_set) == 1:
    print("Yes" if A_set[0] <= N // 2 or A_set[0] == N - 1 else "No")
elif len(A_set) == 2 and A_set[0] + 1 == A_set[1]:
    cnt_min = A.count(A_set[0])
    cnt_max = N - cnt_min
    if cnt_min <= A_set[0] and cnt_min + 1 <= A_set[1] <= cnt_min + cnt_max // 2:
        print("Yes")
    else:
        print("No")
else:
    print("No")

Submission status: Accepted

Solution code:
import sys

if __name__ == '__main__':
    N = int(input())
    a = list(map(int, input().split()))

    min_num = min(a)
    max_num = max(a)

    if max_num - min_num >= 2:
        print("No")
    elif max_num == min_num :
        if max_num == N -1 or 2*max_num <= N:
            print("Yes")
        else:
            print("No")
    else:
        x = a.count(min_num)
        y = a.count(max_num)
        if x < max_num and 2 * (max_num - x) <= y:
            print("Yes")
        else:
            print("No")

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
dic = {}
amin = n - 1
amax = 1
for ai in a:
    if ai in dic:
        dic[ai] += 1
    else:
        dic[ai] = 1
    amin = min(amin, ai)
    amax = max(amax, ai)

if 1 < amax - amin:
    print('No')
elif amin == amax:
    if a[0] == n - 1 or 2 * a[0] <= n:
        print('Yes')
    else:
        print('No')
else:
    if dic[amax] == 1:
        print('No')
    elif dic[amin] + 1 <= amax <= dic[amin] + dic[amax] // 2:
        print('Yes')
    else:
        print('No')

Submission status: Accepted

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

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    n = I()
    a = LI()
    d = collections.defaultdict(int)
    for c in a:
        d[c] += 1
    t = sorted(list(d.keys()))
    if t[-1] - t[0] > 1:
        return 'No'
    if len(t) == 1:
        if t[0] == n - 1:
            return 'Yes'
        if t[0] > n / 2.0:
            return 'No'
        return 'Yes'
    b = t[1] - d[t[0]]
    n2 = d[t[1]]
    if n2 == 2 and b == n2 - 1:
        return 'Yes'
    if b < 1:
        return 'No'
    if b >= n2 - 2:
        return 'No'
    if b > n2 / 2.0:
        return 'No'

    return 'Yes'


print(main())

Submission status: Accepted

Solution code:
# coding: utf-8
import array, bisect, collections, copy, heapq, itertools, math, random, re, string, sys, time
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7


def II(): return int(input())
def ILI(): return list(map(int, input().split()))
def IAI(LINE): return [ILI() for __ in range(LINE)]
def IDI(): return {key: value for key, value in ILI()}


def read():
    N = II()
    a = ILI()
    return (N, a)


def solve(N, a):
    a_max = max(a)
    a_min = min(a)
    ans = ""
    if a_max - a_min > 1:
        ans = "No"
    elif a_max - a_min == 1:
        n_alone = a.count(a_min)
        n_group = a.count(a_max)
        if n_alone < a_max and 2 * (a_max - n_alone) <= n_group:
            ans = "Yes"
        else:
            ans = "No"
    elif a_max - a_min == 0:
        if a[0] == N - 1 or a[0] * 2 <= N:
            ans = "Yes"
        else:
            ans = "No"

    return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
length = int(input())
target = [int(n) for n in input().split(" ")]
if max(target) - min(target) > 1:
    print("No")
elif max(target) >= length:
    print("No")
elif max(target) - min(target) == 1 and (target.count(min(target)) == min(target) and target.count(max(target)) > 1 or (target.count(min(target)) <= min(target) and target.count(max(target)) // 2 >= max(target) - target.count(min(target)) )  ):
    print("Yes")
elif max(target) - min(target) == 0 and ( (length % 2 == 0 and min(target) * 2 <= length) or (length % 2 == 1 and 2 * (min(target) - 1) + 3 <= length)  or min(target) + 1 == length):
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def ris(): return list(input())
def pli(): return "".join(list(map(str, ans)))
 
 
N = ri()
a = rli()
ma = max(a)
mi = min(a)
if(ma - mi > 1):
    print("No")
elif(ma - mi == 1):
    l_ma = a.count(ma)
    l_mi = a.count(mi)
    if(ma <= l_mi):
        print("No")
    elif(l_mi + l_ma/2 >= ma):
        print("Yes")
    else:
        print("No")
else:
    if(a[0] == N-1):
        print("Yes")
    elif(N/a[0] >= 2):
        print("Yes")
    else:
        print("No")

Submission status: Accepted

Solution code:
import collections as co
n = int(raw_input())
aa = map(int, raw_input().split())

c = co.Counter(aa).most_common()

if len(c) > 2:
  print 'No'
  exit()

if len(c) == 1:
  cc = c[0][0]
  if cc == n-1:
    print 'Yes'
  elif cc*2 <= n:
    print 'Yes'
  else:
    print 'No'
  exit()

l = c[0]
s = c[1]
if l[0] < s[0]:
  l,s = s,l

if l[0] != s[0] + 1:
  print 'No'
  exit()

if (n-l[0])+1 <= l[1] <= (n-l[0])*2:
# if (n-l[0])+1 <= l[1] <= (n-l[0])*2 and n-(n-l[0])*2 <= s[1] <= l[0] - 1:
  print 'Yes'
else:
  print 'No'

Submission status: Accepted

Solution code:
n=int(raw_input())
a=map(int,raw_input().split())
if max(a)-min(a)>=2:
    print "No"
    exit()
if max(a)==min(a):
    if max(a)==n-1:
        print "Yes"
    elif max(a)*2>n :
        print "No"
    else:
        print "Yes"
    exit()
cntmx=a.count(max(a))
cntmn=a.count(min(a))
if cntmn>=max(a):
    print "No"
elif (max(a)-cntmn)*2>n-cntmn:
    print "No"
else:
    print "Yes"

Submission status: Accepted

Solution code:
N = int(input())
la = list(map(int, input().split()))

m = max(la)
if (m == N - 1) and (len(set(la)) == 1):
    print('Yes')
else:
    d = N - m
    cnt = la.count(m)
    if ((cnt >= d + 1) and (cnt <= 2 * d)):
        if cnt < N:
            if (len(set(la)) == 2) and min(la) == m - 1:
                print('Yes')
            else:
                print('No')
        else:
            print('Yes')
    else:
        print('No')

Submission status: Accepted

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
C = max(a)
ans = ""

if C - min(a) > 1:
	ans = "No"
elif C == min(a):
	if N >= 2 * C or N == C + 1:
		ans = "Yes"
	else:
		ans = "No"
else:
	if C - a.count(C - 1) > 0 and N - a.count(C - 1) >= 2 * (C - a.count(C - 1)):
		ans = "Yes"
	else:
		ans = "No"
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))

l.sort()
ans = "Yes"
count = 1
if l[0] == l[-1]:
  if ((n // 2) < l[0]) and (l[0] != 1) and (l[0] != n - 1):
    ans = "No"
elif l[-1] - l[0] >= 2:
    ans = "No"
else:
  for i in range(n-1):
    if l[i+1] - l[i] == 0:
      count += 1
    elif l[i+1] - l[i] == 1:
      break
  umax = count + (n - count) // 2
  if (l[-1] > umax) or (l[-1] <= count):
    ans = "No"

print(ans)

Submission status: Accepted

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

seta = set(a)
if len(seta) == 1:
    if a[0] in (0, N-1) or a[0] <= N // 2:
        print("Yes")
    else:
        print("No")
elif len(seta) == 2:
    a1 = min(seta)
    a2 = max(seta)
    counta1 = a.count(a1)
    if a2 != a1 + 1:
        print("No")
    elif counta1 < a2 and counta1 + 2 * (a2 - counta1) <= N:
        print("Yes")
    else:
        print("No")
else:
    print("No")

Submission status: Accepted

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

is_ok = True

color_num = a[0]
unique = []
for i, a_i in enumerate(a[1:], 1):
    if a_i < len(unique):
        is_ok = False
        break
    if a_i == color_num - 1:
        unique.append(i)
    elif a_i != color_num:
        is_ok = False
        break
if len(unique) >= color_num:
    if len(unique) == color_num and len(unique) == N:
        is_ok = True
    else:
        is_ok = False
if N // color_num == 1:
    unique_num_min = color_num - (N % color_num)
    if len(unique) < unique_num_min:
        is_ok = False

color_num = a[0] + 1
unique = [0]
if not is_ok:
    is_ok = True
    for i, a_i in enumerate(a[1:], 1):
        if a_i < len(unique):
            is_ok = False
            break
        if a_i == color_num - 1:
            unique.append(i)
        elif a_i != color_num:
            is_ok = False
            break
    if len(unique) >= color_num:
        if len(unique) == color_num and len(unique) == N:
            is_ok = True
        else:
            is_ok = False
    if N // color_num == 1:
        unique_num_min = color_num - (N % color_num)
        if len(unique) < unique_num_min:
            is_ok = False

if is_ok:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
N=int(input())

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


#1匹からでは不可能なので
if a[-1]-a[0]>1:
	print("No")
else:
	#全部が同じではない場合
	for i in range(1,N):
		#a[i]の要素が1個増えたタイミング
		if a[i]!=a[0]:
			if 1<=a[-1]-i<=int((N-i)/2):
				print("Yes")
			else:
				print("No")
			break
	else:
		#全部同じ場合
		if 1<=a[0]<=int(N/2) or a[0]==N-1:
			print("Yes")
		else:
			print("No")

Submission status: Accepted

Solution code:
import sys
N=int(input())
a=[int(i) for i in input().split()]
a.sort()

if a[N-1]-a[0]>1:
  print("No")
  sys.exit()
if a[0]==N-1:
  print("Yes")
  sys.exit()
if a[N-1]==1:
  print("Yes")
  sys.exit()
if a[0]==a[N-1]:
  if 2*a[0]<=N:
    print("Yes")
  else:
    print("No")
  sys.exit()

count=a.index(a[N-1])
if count+1<=a[N-1] and a[N-1]<=count+int((N-count)/2):
  print("Yes")
else:
  print("No")

Submission status: Accepted

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

cnt = 0
for i in range(n):
    if a[i] == a[0]:
        cnt = i+1
    else:
        break

if a[-1] - a[0] > 1:
    print('No')
    exit()
elif a[-1] - a[0] == 1:
    actual = a[-1]
    if actual >= n:
        print('No')
    elif cnt <= actual - 1 and actual <= cnt  + (n - cnt) // 2:
        print('Yes')
    else:
        print('No')
    exit()
elif a[-1] - a[0] == 0:
    solve = False
    actual = a[0]
    if n >= actual * 2 or n-1 == actual:
        solve = True
    if solve:
        print('Yes')
    else:
        print('No')
    exit()
else:
    print('No')

Submission status: Accepted

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

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

N = int(input())
As = list(map(int, input().split()))

mn = min(As)
mx = max(As)
if mx > mn + 1:
    print('No')
elif mx == mn:
    if 1 <= mx <= N // 2 or mx == N - 1:
        print('Yes')
    else:
        print('No')
else:
    mn_num = As.count(mn)
    mx_num = As.count(mx)
    k = mn + 1 - mn_num
    # print("can i make {} groups from {}?".format(k, mx_num))
    if mx_num == 0:
        if k == 0:
            print('Yes')
        else:
            print('No')
    else:
        if 1 <= k <= (mx_num // 2):
            print('Yes')
        else:
            print('No')

Submission status: Accepted

Solution code:
import sys

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

T=0
for i in a:
    if i != a[0]:
        if i - a[0] >= 2 or a[0] - i >= 2:
            print "No"
            sys.exit()
        T=max(i,a[0])
        break

if T == 0:
    if a[0] <= N/2 or a[0] == N-1:
        print "Yes"
    else:
        print "No"
    sys.exit()

uni=0
nu=0
for i in a:
    if i==T:
        nu+=1
    else:
        uni+=1

if T>=uni+1 and T<=uni+nu/2:
    print "Yes"
else:
    print "No"

Submission status: Accepted

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math

N, = map(int, sys.stdin.readline().rstrip().split())
a_s = list(map(int, sys.stdin.readline().rstrip().split()))

mina = min(a_s)
maxa = max(a_s)
if (maxa - mina > 1):
    print("No")
    exit(0)
if(maxa == mina):
    if(maxa <= N//2 or N == (maxa+1)):
        print("Yes")
    else:
        print("No")
    exit(0)
if maxa - mina == 1:
    alones = sum(a == mina for a in a_s)
    #print(alones)
    #print(N-alones)
    #print(maxa-alones)
    if  (N-alones) >= (maxa - alones) * 2 and alones < maxa:
        print("Yes")
    else:
        print("No")
    exit(0)

raise Exception()
exit(0)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

# sys.stdin = open('b1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_int():
    return int(input())


def read_str_list():
    return input().split()


def read_str():
    return input()


yes, no = 'Yes', 'No'


def solve(n, a):
    s = set(a)
    c = Counter(a)
    if len(s) == 1:
        a = list(s)[0]
        n_colors = a + 1
        if n_colors == n:
            return yes
        n_colors = a
        if 2 * n_colors <= n:
            return yes
    if len(s) == 2:
        a, b = sorted(s)
        if a + 1 == b:
            n_colors = b
            appear_once = c[a]
            appear_twice_or_more = n_colors - appear_once
            if appear_once < n_colors:
                if c[b] >= 2 * appear_twice_or_more:
                    return yes
    return no

n = read_int()
a = read_int_list()
res = solve(n, a)
print(res)

Submission status: Accepted

Solution code:
import sys

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

def no():
	print "No"
	exit(0)

def yes():
	print 'Yes'
	exit(0)

a = list(sorted(a))
if a[0] == a[-1] and a[0] == n - 1:
	yes()

X = max(a)
alone = 0
many = 0

for e in a:
	if e != X and e != X - 1:
		no()
	if e == X - 1:
		alone += 1
	else:
		many += 1

rest = X - alone

if rest < 0:
	no()

if rest == 0 and many > 0:
	no()

if rest * 2 > many:
	no()

yes()

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())

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


X = Counter(a)

min_val = min(a)
max_val = max(a)
if not min_val == max_val-1 and not min_val==max_val:
    print("No")
    exit()
if max_val==min_val and max_val == N-1:
    #all unique
    print("Yes")
    exit()

#only 1 color cat
unique_cat = X[min_val] if min_val!=max_val else 0

# 2 or more cat color  : max_val - unique_cat
last_cat =  N- unique_cat
last_colors = max_val - unique_cat


#print(last_cat,last_colors,unique_cat)
if last_cat>0 and last_colors <=0:
    print("No")
elif  0< 2*last_colors <= last_cat:
    print("Yes")

else:
    print("No")

Submission status: Accepted

Solution code:
n = int(input())
array = [int(x) for x in input().split(' ')]
maxa = max(array)
mina = min(array)
if maxa - mina > 1:
    print('No')
elif maxa == mina:
    if n >= 2 * maxa:
        print('Yes')
    elif maxa == n-1:
        print('Yes')
    else:
        print('No')
else:
    sum = 0
    for j in array:
        if j == mina:
            sum += 1
    if sum >= maxa:
        print('No')
        exit()
    if 2*(maxa - sum) <= n - sum:
        print('Yes')
    else:
        print('No')

Submission status: Accepted

Solution code:

def read_data():
    N = int(input())
    As = list(map(int, input().split()))
    return N, As

def solve(N, As):
    x = max(As)
    y = min(As)
    if x - y > 1:
        return False
    if x == y:
        if x == N - 1:
            return True
        if x * 2 <= N:
            return True
        return False
    n_solo = As.count(y)
    if x < n_solo + 1:
        return False
    n_group = x - n_solo
    if n_group * 2 <= N - n_solo:
        return True
    return False

N, As = read_data()
if solve(N, As):
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int, raw_input().split())
b = max(a)
c = min(a)
if b - c > 1:
    print "No"
elif b == c:
    print "Yes" if b == n - 1 or 2 * b <= n else "No"
else:
    t = sum(c == x for x in a)
    if t >= b:
        print "No"
    else:
        print "Yes" if (b - t) * 2 <= n - t else "No"

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(500000)


def dfs(A):
  #if sum(A) > M:
  #  return
  if len(A) == N-1:
    temp = M-sum(A)
    if A[-1] <= temp:
      A.append(temp)
      B.append(list(A)) #listとしないとAppendできない。
      A.pop()
    return
  if len(A) >= 1: #数列がある場合には最後の数字を取得し、そこから追加のFor loopを回す。
    t = A[-1]
  else:
    t = 1
  for i in range(t,M):
    A.append(i)
    dfs(A)
    A.pop() #ここで消さないとFor loopの中で不要に添え字が増えてしまう。

M = int(input())
X = list(map(int,input().split()))
X.sort()
if max(X)-min(X) > 1:
  print("No")
  exit()
if min(X) == max(X):
  N = min(X) #色の数
else:
  N = min(X)+1
B = []
dfs([])
#print(B)

for t in B:
  feasible = []
  for i in range(N):
    if t[i] == 1:
      feasible.append(N-1)
    else:
      for j in range(t[i]):
        feasible.append(N)
  feasible.sort()
  #print(feasible,X)
  if feasible == X:
    print("Yes")
    exit()
if min(X) == max(X):
  N+=1
B = []
dfs([])
#print(B)
for t in B:
  feasible = []
  for i in range(N):
    if t[i] == 1:
      feasible.append(N-1)
    else:
      for j in range(t[i]):
        feasible.append(N)
  feasible.sort()
  #print(feasible,X)
  if feasible == X:
    print("Yes")
    exit()
print("No")

Submission status: Runtime Error

Solution code:
def dfs(A):
  #if sum(A) > M:
  #  return
  if len(A) == N-1:
    temp = M-sum(A)
    if A[-1] <= temp:
      A.append(temp)
      B.append(list(A)) #listとしないとAppendできない。
      A.pop()
    return
  if len(A) >= 1: #数列がある場合には最後の数字を取得し、そこから追加のFor loopを回す。
    t = A[-1]
  else:
    t = 1
  for i in range(t,M):
    A.append(i)
    dfs(A)
    A.pop() #ここで消さないとFor loopの中で不要に添え字が増えてしまう。

M = int(input())
X = list(map(int,input().split()))
X.sort()
if max(X)-min(X) > 1:
  print("No")
  exit()
if min(X) == max(X):
  N = min(X) #色の数
else:
  N = min(X)+1
B = []
dfs([])
#print(B)

for t in B:
  feasible = []
  for i in range(N):
    if t[i] == 1:
      feasible.append(N-1)
    else:
      for j in range(t[i]):
        feasible.append(N)
  feasible.sort()
  #print(feasible,X)
  if feasible == X:
    print("Yes")
    exit()
if min(X) == max(X):
  N+=1
B = []
dfs([])
#print(B)
for t in B:
  feasible = []
  for i in range(N):
    if t[i] == 1:
      feasible.append(N-1)
    else:
      for j in range(t[i]):
        feasible.append(N)
  feasible.sort()
  #print(feasible,X)
  if feasible == X:
    print("Yes")
    exit()
print("No")

Submission status: Runtime Error

Solution code:
yes="Yes"
no="No"
n=int(input())
a=list(map(int,input().split()))
if max(a)-min(a)>1:exit(print(no))
if a==[a[0]]*n:
  if a[0]==n-1:print(yes)
  elif 2*a[0]<=n:print(yes)
  else:print(no)
else:
  mi=a.count(min(a))
  ma=n-mi
  if mi<max(a):print(yes)
  elif 2*(max(a)-mi)<=y:print(yes)
  else:print(no)

Submission status: Runtime Error

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
A = sorted(map(int, readline().split()))

def main(A):
    N = len(A)
    if A[0] == A[-1]:
        raise
        x = A[0]
        return x == N - 1 or 2 * x <= N
    if A[0] + 1 != A[-1]:
        return False
    a, b = A[0], A[-1]
    full = b
    unique = sum(x == a for x in A)
    return N - unique >= 2 * a

print('Yes' if main(A) else 'No')

Submission status: Runtime Error

Solution code:
from collections import Counter

N = int(input())
A = Counter(map(int, input().split()))

if len(A.keys()) >= 3:
    print('No')
    exit()

if len(A.keys()) == 1:
    p = list(A.keys())[0]
    print('Yes' if 2 * p <= N or p == N - 1 else 'No')
    exit()

p1, p2 = sorted(A.keys())
1 / 0
print('Yes' if [p1, p2] == [1, 2] and A[1] == 1 else 'No')

Submission status: Runtime Error

Solution code:
from collections import Counter

N = int(input())
A = Counter(map(int, input().split()))

if len(A.keys()) >= 3:
    print('No')
    exit()

if len(A.keys()) == 1:
    print('Yes' if list(A.keys())[0] == N - 1 or list(A.keys())[0] == 2 else 'No')
    exit()

p1, p2 = A.keys()
1 / 0
print('Yes' if abs(p1 - p2) == 1 and max(p1, p2) <= N - 1 and A[min(p1, p2)] == 1 else 'No')

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
if len(set(a))==1:
    if n>=2*a[0] or n-1==a[0]:print("Yes")
    else:print("No")
elif len(set(a))==3:print("No")
else:
    s,S=sorted(list(set(a)))
    if n+a.count(s)>=2*S:print("Yes")
    else:print("No")

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

YES = "Yes"  # type: str
NO = "No"  # type: str


def solve(N: int, a: "List[int]"):
    from collections import Counter
    ma, mi = max(a), min(a)
    if ma >= N or mi < 1:
        raise
    if ma - mi > 1:
        return False
    # 全員aloneか、全員pairもしくはcrowd
    if ma == mi:
        return ma == N - 1 or  2 * ma <= N
    c = Counter(a)
    # pairがpairでない
    if c[ma] % 2 != 0:
        return False
    x = ma - c[ma] // 2
    # pair以外、すべてalone(crowdが0個)
    if x == c[mi]:
        return True
    # crowdを1つ以上作れるなら1以上c[mi]-2以下の好きな數を作れる
    return 1 <= x <= c[mi] - 2

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    print(YES if solve(N, a) else NO)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
M=max(a)
m=min(a)
if M>m+1:
  print("No")
elif M==m+1:
  cntm=a.count(m)
  cntM=n-cntm
  if cntm>m:
    print("No")
  elif cntm+cntM//2<M:
    print("No")
  else:
    print("Yes")
elif M==m:
  if m<=n//2:
    print("Yes")
  else if m==n-1:
    print("Yes")
  else:
    print("No")

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

YES = "Yes"  # type: str
NO = "No"  # type: str


def solve(N: int, a: "List[int]"):
    from collections import Counter
    ma, mi = max(a), min(a)
    if ma >= N or mi < 1:
        raise
    if ma - mi > 1:
        return False
    if ma == mi:
        return ma == N - 1 or  2 * ma <= N
    c = Counter(a)
    if c[ma] % 2 != 0:
        return False
    return mi != c[ma] // 2 + c[mi]

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    print(YES if solve(N, a) else NO)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = []
C = []
for num in A:
    if num in B: 
        C[B.index(num)] += 1
    else:
        B.append(num)
        C.append(1)

if len(B) > 2:
    print('No')
elif len(B) == 1:
    if N == B[0] +1:
        print('Yes')
    elif N//B[0] >= 2:
        print('Yes')
    else:
        print('No')
elif B[0]  == (B[1]+1):
    k = C[1]
    m = B[1]
    if m-k+1 <= 0:
        print('No')
    elif (n-k) % (m-k+1) == 0:
        print('Yes')
    else:
        print('No')
elif B[1] == (B[0]+1):
    k = C[0]
    m = B[0]
    if m-k+1 <= 0:
        print('No')
    elif (n-k) % (m-k+1) == 0:
        print('Yes')
    else:
        print('No')
else:
    print('No')

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = []
C = []
for num in A:
    if num in B: 
        C[B.index(num)] += 1
    else:
        B.append(num)
        C.append(1)

if len(B) > 2:
    print('No')
elif len(B) == 1:
    if N/B[0] >= 2:
        print('Yes')
    else:
        print('No')
elif B[0]  == (B[1]+1):
    k = C[1]
    m = B[1]
    if m-k+1 <= 0:
        print('No')
    elif (n-k) % (m-k+1) == 0:
        print('Yes')
    else:
        print('No')
elif B[1] == (B[0]+1):
    k = C[0]
    m = B[0]
    if m-k+1 <= 0:
        print('No')
    elif (n-k) % (m-k+1) == 0:
        print('Yes')
    else:
        print('No')
else:
    print('No')

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = []
C = []
for num in A:
    if num in B: 
        C[B.index(num)] += 1
    else:
        B.append(num)
        C.append(1)

if len(B) > 2:
    print('No')
elif len(B) == 1:
    if N/B[0] >= 2:
        print('Yes')
    else:
        print('No')
elif B[0]  == (B[1]+1):
    k = C[1]
    m = B[1]
    if (n-k) % (m-k+1) == 0:
        print('Yes')
    else:
        print('No')
elif B[1] == (B[0]+1):
    k = C[0]
    m = B[0]
    if (n-k) % (m-k+1) == 0:
        print('Yes')
    else:
        print('No')
else:
    print('No')

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(a) for a in input().split()]

A.sort()
flag = True
for a in A:
    if a > A[0]+1:
        flag = False

if flag:
    cnt = A.count(A[0])
    if cnt == N and (A[0] == N-1 or A[0]*2 <= N):
        ans = "Yes"
    elif cnt == A[0] and cnt < N-1:
        ans = "Yes"
    else:
        ans = "No"
print(ans)

Submission status: Runtime Error

Solution code:
N = input()
a = map(int, raw_input().split())

if max(a)-min(a)>=2:
  print "No"
elif max(a)-min(a)==0:
  if N>=2*a[0] or N==a[0]+1:
    print "Yes"
  else:
    print "No"
else:
  if a.count(max(a))>=2 and a.count(min(a))==min(a):
    print "Yes"
  else:
    exit(-1)
    print "No"

Submission status: Runtime Error

Solution code:
N = input()
a = map(int, raw_input().split())

if max(a)-min(a)>=2:
  exit(-1)
  print "No"
elif max(a)-min(a)==0:
  exit(-1)
  if N>=2*a[0] or N==a[0]+1:
    print "Yes"
  else:
    print "No"
else:
  if a.count(max(a))>=2 and a.count(min(a))==min(a):
    print "Yes"
  else:
    print "No"