Problem:


Score : 300 points

Problem Statement

You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N).
Your objective is to remove some of the elements in a so that a will be a good sequence.

Here, an sequence b is a good sequence when the following condition holds true:

For each element x in b, the value x occurs exactly x times in b.

For example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.

Find the minimum number of elements that needs to be removed so that a will be a good sequence.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of elements that needs to be removed so that a will be a good sequence.

Sample Input 1

4
3 3 3 3

Sample Output 1

1

We can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence.

Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

We can, for example, remove two occurrences of 4. Then, (2, 1, 2) is a good sequence.

Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

Remove one occurrence of 10^9. Then, () is a good sequence.

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5



Sample input:
4
3 3 3 3


Sample output:
1

Submission status: Accepted

Solution code:
from collections import Counter

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

    counter = Counter(a)
    ans = 0
    for ele, count in counter.items():
        if ele < count:
            ans += count - ele
        elif count < ele:
            ans += count

    print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce
from operator import xor
from heapq import heappush, heappop
INF = float("inf")
sys.setrecursionlimit(10**7)

# 4近傍（右, 下, 左, 上）
dy4, dx4 = [0, -1, 0, 1], [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def ceil(a, b): return (a + b - 1) // b


def main():
    N = int(input())
    a = Counter(list(map(int, input().split())))

    ans = 0
    for k, v in a.items():
        if k == v:
            continue
        elif k < v:
            ans += v - k
        else:
            ans += v
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
length = raw_input()
arr = map(int, raw_input().split())

from collections import defaultdict
d = defaultdict(int)
count = 0
for a in arr:
	if d[a] == a:
		count += 1
		continue
	d[a] += 1
for k,x in d.items():
	if k != x:
		count += x

print count

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
text = input().split()

count = Counter()
for i in range(0, N):
    count[int(text[i])] += 1
remove = 0
for key, item in count.items():
    if key > item:
        remove = remove + item
    elif key < item:
        remove = remove + (item - key)
print(remove)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(raw_input())
arr = map(int, raw_input().split())

cnt = defaultdict(int)
for x in arr:
    cnt[x] += 1

ans = 0
for k in cnt:
    if cnt[k] >= k:
        ans += cnt[k] - k
    else:
        ans += cnt[k]

print ans

Submission status: Accepted

Solution code:
from collections import *
input()
a = Counter(map(int, raw_input().split()))
z = 0
for i in a:
	if a[i] >= i:
		z += a[i] - i
	else:
		z += a[i]
print z

Submission status: Accepted

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


v = {}
for q in x:
    if not q in v:
        v[q] = 0
    v[q] += 1


ans = 0
for key in v.keys():
    val = v[key]
    
    if val < key:
        ans += val
    elif val > key:
        ans += val - key
print ans

Submission status: Accepted

Solution code:
#!/usr/bin/env python

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

hash = {}
for elem in a:
    if not elem in hash:
        hash[elem] = 1
    else:
        hash[elem] += 1

ans = 0
for key, value in hash.items():
    if value > key:
        ans += value - key
    elif value < key:
        ans += value

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

d = defaultdict(int)

ans = 0

n = int(input())

for x in [int(y) for y in input().split()]:
    d[x] += 1

for k,v in d.items():
    if k > v:
        ans += v
    else:
        ans += v - k

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import collections

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

a_col = collections.Counter(a)
remove_count = 0

for k in a_col:
	if k > a_col[k]:
		remove_count += a_col[k]
	elif k < a_col[k]:
		remove_count += a_col[k] - k

print remove_count

Submission status: Accepted

Solution code:
from collections import *
input()
r = 0
for k, v in Counter(map(int, input().split())).items():
    if v < k:
        r += v
    else:
        r += v - k
print(r)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
tmp = 1
cnt = 0
S = 0
a.sort()
a.append(0)
for i in range(N):
    if a[i] == a[i + 1]:
        tmp += 1
    else:
        if tmp > a[i]:
            cnt += tmp - a[i]
        elif tmp < a[i]:
            cnt += tmp
        tmp = 1
print(cnt)

Submission status: Accepted

Solution code:
import collections
N = int(input())
a = list(map(int, input().split()))
nums = collections.defaultdict(int)
ans=0

for i in range(N):
    nums[a[i]] += 1

num_flag = list(nums.keys())
    
for i in range(len(num_flag)):
    if nums[num_flag[i]] >= num_flag[i]:
        ans += nums[num_flag[i]] - num_flag[i]
    else:
        ans += nums[num_flag[i]]

print(ans)

Submission status: Accepted

Solution code:
import sys,collections
sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

n = I()
As = Is()
d = collections.defaultdict(int)
for a in As:
    d[a] += 1 
lis = list(d.items())
ans = 0
for e in lis:
    if e[0] - e[1] > 0:
        ans += e[1]
    else:
        ans += e[1] - e[0]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort()
a.append(0)
answer = 0
i = 0
counter = 1
while i <= n-1:
	if a[i] == a[i+1]:
		counter += 1
		i += 1
	else:
		if counter != a[i]:
			if counter > a[i]:
				answer += (counter - a[i])
				i += 1
				counter = 1
			else:
				answer += counter
				i += 1
				counter = 1
		else:
			i += 1
			counter = 1
print(answer)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))

d = defaultdict(int)
for i in range(n):
    d[a[i]] += 1

res = 0
for k, v in d.items():
    if k <= v:
        res += (v - k)
    else:
        res += v
print(res)

Submission status: Accepted

Solution code:
import sys
N=int(input())
A=list(map(int,input().split()))
A.sort()
ans=0
current=A.pop()
num=1
if len(A)==0:
  if num<current: ans+=num
  else: ans+=num-current
while A:
  if A[-1]==current:
    num+=1
    current=A.pop()
    if len(A)==0:
      if num<current: ans+=num
      else: ans+=num-current
  else:
    if num<current: ans+=num
    else: ans+=num-current
    current=A.pop()
    num=1
    if len(A)==0:
      if num<current: ans+=num
      else: ans+=num-current
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt = Counter(As)
ans = 0
for A, num in cnt.items():
    if A <= num:
        ans += num - A
    else:
        ans += num

print(ans)

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 random
import re
import string

N = int(input())
c = Counter(map(int, input().split()))
ans = 0
for k, v in c.items():
    if k < v:
        ans += v - k
    elif k > v:
        ans += v
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 19 13:32:42 2017

@author: yamakoshi
"""

n = int(input())

seq = {}
a1n = map(int, input().split())
for a in a1n:
    if a not in seq:
        seq[a] = 1
    else:
        seq[a] += 1

ret = 0
for a in seq:
    if not seq[a] == a:
        if seq[a]-a > 0:
            ret += seq[a]-a
        else:
            ret += seq[a]

print(ret)

Submission status: Accepted

Solution code:
N = int(input())
A = map(int, (input().split()))
d = dict()
for a in A:
    if a not in d:
        d[a] = 1
    else:
        d[a] = d[a] + 1
ans = 0
for k, v in d.items():
    if k > v:
        ans = ans + v
    else:
        ans = ans + v - k
print(ans)

Submission status: Accepted

Solution code:

def main():
    N = int(input())
    a = list(map(int, input().split()))
    dic = {}
    for i in a:
        dic[i] = 1 + dic.get(i, 0)

    res = 0
    for k, v in dic.items():
        if k > 10**5:
            res += v
        elif k == v:
            continue
        elif k > v:
            res += v
        else:
            res += v- k

    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
src = list(map(int,input().split()))
ctr = Counter(src)
ans = 0
for k,v in ctr.items():
    if v >= k:
        ans += (v-k)
    else:
        ans += v
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = input().split()

from collections import Counter

b = Counter(a)
remove = 0

for x in b:
    if int(x) > b[x]:
        remove += b[x]
    else:
        remove += b[x]-int(x)

print(remove)

Submission status: Accepted

Solution code:
N=int(input())
a=[int(i) for i in input().split()]
data={}
for num in a:
        if num in data:
                data[num]+=1
        else:
                data[num]=1

ans=0
for key in data.keys():
        if data[key]<key:
                ans+=data[key]
        else:
                ans+=data[key]-key
print(ans)

Submission status: Accepted

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

b = [0] * (N + 1)
ans = 0
for ai in a:
    if ai <= N:
        b[ai] += 1
    else:
        ans += 1
for i in range(N + 1):
    if b[i] > i:
        ans += b[i] - i
    elif b[i] < i:
        ans += b[i]

print(ans)

Submission status: Accepted

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

from collections import defaultdict

def solve():
    n = int(input())
    ad = defaultdict(int)
    for i in input().split():
       ad[int(i)] += 1
    
    ans = 0
    for k, v in ad.items():
        if k > v:
            ans += v
        if v > k:
            ans += v - k
    print(ans)

if __name__=="__main__":
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter
def inpl(): return tuple(map(int, input().split()))

N = int(input())
A = inpl()
C = Counter(A)
res = 0

for k, v in C.items():
    if v < k:
        res += v
    elif v > k:
        res += (v - k)
print(res)

Submission status: Accepted

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

cnt = {}
for x in a:
    if x not in cnt:
        cnt[x] = 0
    cnt[x] += 1

# print(cnt)
ans = 0
for k, v in cnt.items():
    if k == v:
        continue
    if k < v:
        ans += v - k
    else:
        ans += v

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
d = dict()
ans = 0
for i in a:
    if i not in d.keys():
        d[i] = 0
    d[i] += 1
for x, c in d.items():
    if c < x:
        ans += c
    else:
        ans += c - x
print(ans)

Submission status: Accepted

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

cnt = {}
for ai in a:
    if ai in cnt:
        cnt[ai] += 1
    else:
        cnt[ai] = 1

ans = 0
for k, v in cnt.items():
    if k < v:
        ans += v - k
    elif v < k:
        ans += v
print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = collections.Counter(list(map(int, input().split())))
cnt = 0
for key in a.keys():
    if a[key] > key:
        cnt += a[key] - key
    elif a[key] < key:
        cnt += a[key]
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
c = Counter(list(map(int, input().split())))

ans = 0
for i in c:
  if c[i] < i:
    ans += c[i]
  elif c[i] > i:
    ans += c[i] - i

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = Counter(map(int, input().split()))
ans = 0
for k, v in A.items():
    if k <= v:
        ans += v - k
    else:
        ans += v
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in a:
    if i not in d:
        d[i] = 1
    else:
        d[i] += 1
ans = 0
for i in d.keys():
    if i > d[i]:
        ans += d[i]
    else:
        ans += d[i] - i
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

input()
ac = Counter(map(int, input().split()))
ans = 0

for a, c in ac.items():
    if a > c:
        ans += c
    else:
        ans += c - a

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = input()
A = map(int, raw_input().split())
C = Counter(A)
ans = 0

for k, v in C.items():
    if k > v:
        ans += v
    elif k < v:
        ans += v - k

print ans

Submission status: Runtime Error

Solution code:
s=input()
x,y=map(int,input().split())
p=8010
a,b=[],0
for c in s:
    if c=='T':
        a.append(b)
        b=0
    else:
        b+=1
a.append(b)
x-=a.pop(0)
yl,xl=a[::2],a[1::2]
mx,my=[0]*(p*2),[0]*(p*2)
mx[p],my[p]=1,1
for dx in xl:
    nx=[0]*(p*2)
    for i in range((p*2)):
        nx[i]=max(nx[i],mx[i])
        if i-dx>=0:
            nx[i-dx]=max(nx[i-dx],mx[i])
        if i+dx<2*p:
            nx[i+dx]=max(nx[i+dx],mx[i])
    mx=nx
for dy in yl:
    ny=[0]*(p*2)
    for i in range((p*2)):
        ny[i]=max(ny[i],my[i])
        if i-dy>=0:
            ny[i-dy]=max(ny[i-dy],my[i])
        if i+dy<2*p:
            ny[i+dy]=max(ny[i+dy],my[i])
    my=ny
print('Yes' if mx[x+p] and my[y+p] else 'No')

Submission status: Runtime Error

Solution code:
n = len(input())
mp, res = {}, 0
for el in list(map(int, input().split())): mp[el] = mp.get(el, 0) + 1
for key, val in mp:
  if key > val: res += val
  elif key < val: res += val - key
print(res)

Submission status: Runtime Error

Solution code:
def f(m,l):
    tmpm=set()
    for x in m:
        tmpm.add(x+l);tmpm.add(x-l)
    return tmpm

s=input()+"T"
x,y=map(int,input().split())

while s and s[0]=="F":
    x-=1
    s=s[1:]
ss=[{0},{0}]
mode=0
l=0
for i in range(len(s)):
    if s[i]=="F": l+=1
    else:
        ss[mode]=f(ss[mode],l)    
        mode^=1
        l=0

print("Yes" if x in ss[0] and y in ss[1] else "No")

Submission status: Runtime Error

Solution code:
#FT Robot_2
s = input()
x, y = map(int, input().split())

L = []
stack = 0
p = [0]
q = [0]

for i in range(len(s)):
    if s[i] == "F":
        stack = stack + 1
    else:
        L.append(stack)
        stack = 0

if stack != 0:
    L.append(stack)

for i in range(len(L)):
    if i == 0:
        p = [L[i]]
    elif i % 2 == 0:
        while len(p) < 2**(i//2):
            p.append(p[0]+L[i])
            p.append(p[0]-L[i])
            p.remove(p[0])
    elif i % 2 == 1:
        while len(q) < 2**(i//2 + 1):
            q.append(q[0]+L[i])
            q.append(q[0]-L[i])
            q.remove(q[0])

if x in p and y in q:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
# ARC087C - Good Sequence (ABC082C)
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    lst = list(map(int, input().rstrip().split()))
    cnt = [0] * (10 * 9 + 1)
    for i in lst:
        cnt[i] += 1
    ans = 0
    for i, j in enumerate(cnt):
        if i > j:
            ans += j
        elif i < j:
            ans += j - i
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

i = 0
j = 1
ans = 0

for i in range(N-1):
	if a[i] == a[i+1]:
		j += 1
	else:
		if j >= a[i]:
			ans += j - a[i]
		else:
			ans += j
if a[N-1] != A[N-2]:
	if a[N-1] != 1:
		ans += 1
else:
	if j >= a[i]:
		ans += j - a[i]
	else:
		ans += j

Submission status: Runtime Error

Solution code:
N=int(input())
num={}
for a in input().split():
  num[a]+=1
ans=0
for k,v in num:
  if k>v:
    ans+=v
  else:
    ans+=v-k
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
l = list(map(int, input().split()))
l.sort()
ans = 0
j = 0
for i in unique_l:
    cnt = 0
    
    while l[j] == i and j < len(l):
        cnt += 1
        j += 1
    j += 1
    if cnt == i:
        pass
    elif cnt > i:
        ans += cnt - 1
    elif cnt < i:
        ans += cnt
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
l = list(map(int, input().split()))
N_MAX =  int(1e+5 + 1)
cnt = [0 for _ in range(N_MAX)]
for i in l:
    cnt[i] += 1
ans = 0
for idx, val in enumerate(cnt):
    if val == 0 or val == idx:
        pass
    elif val < idx:
        ans += val
    elif idx < val:
        ans += val - idx
print(ans)

Submission status: Runtime Error

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
s=input()
[x,yy]=i2()
t=[]
y=[]
k=0
for i in range(len(s)):
  if s[i]=="T":
     if len(t)==len(y):
        y.append(k)
     else:
        t.append(k)
     k=0
  else:
     k+=1
if s[-1]=="F":
     if len(t)==len(y):
        y.append(k)
     else:
        t.append(k)  
dx=[0 for i in range(8001)]
dy=[0 for i in range(8001)]
dx[0]=1
dy[0]=1
t.sort()
y.sort()
t=t[::-1]
y=y[::-1]
for i in t:
 for j in range(8001):
   if j+i<=8000:dy[j+i]=dy[j]
   if j-i>=0:dy[j-i]=dy[j]

for i in y:
 for j in range(8001):
   if j+i<=8000:dx[j+i]=dx[j]
   if j-i>=0:dx[j-i]=dx[j]

if dy[abs(yy)]*dx[abs(x)]:
   print("Yes")
else:
   print("No")

Submission status: Runtime Error

Solution code:
# coding:utf-8

x=int(input())
member=list(map(int, input().split()))

table=(10**9)*[int(0)]
ans=0
for i in range(x):
    table[member[i]] += 1

    if (table[member[i]] != member[i]):
        ans += 1
    else:
        ans -= (member[i]-1);


print(ans)

'''
# {{{

# }}}
'''

Submission status: Runtime Error

Solution code:

length = raw_input()
nums = raw_input().split(" ")

numdict = {}
for i, num in enumerate(nums):
	if num not in dic:
		dic[num] = 1
	else:
		dic[num]++

ret = 0
for k,v in numdict.iteritems():
	if k > v:
		 += k - v 
	else:
		ret += v - k

print(ret)

Submission status: Runtime Error

Solution code:
s = input() + "T"
x, y = map(int, input().split())
N = len(s)



for i in range(N):
    if s[i] == "F":
        x -= 1
    else:
        cnt_Y = True
        index = i + 1
        break

steps_X = []
steps_Y = []

while index < N:
    cnt = 0
    for j in range(index, N):
        if s[j] == "F":
            cnt += 1
        else:
            if cnt_Y and cnt != 0:
                steps_Y.append(cnt)
            elif not cnt_Y and cnt != 0:
                steps_X.append(cnt)
            cnt_Y = not cnt_Y
            index = j
            break
    index += 1
    
K = 10000
dp = [set() for _ in range(K)]
dp[0].add(0)

for i, t in enumerate(steps_X):
    for tmp in dp[i]:
        dp[i+1].add(tmp+t)
        dp[i+1].add(tmp-t)

# print(dp[len(steps_X)])
if x not in dp[len(steps_X)]:
    print("No")
    import sys
    sys.exit()

dp = [set() for _ in range(K)]
dp[0].add(0)
for i, t in enumerate(steps_Y):
    for tmp in dp[i]:
        dp[i+1].add(tmp+t)
        dp[i+1].add(tmp-t)

if y not in dp[len(steps_Y)]:
    print("No")
    import sys
    sys.exit()


print("Yes")

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce
from operator import xor
from heapq import heappush, heappop
INF = float("inf")
sys.setrecursionlimit(10**7)

# 4近傍（右, 下, 左, 上）
dy4, dx4 = [0, -1, 0, 1], [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def ceil(a, b): return (a + b - 1) // b

YES = "Yes"
NO = "No"


def check(x, nums):
    x = abs(x)
    dp = [False] * (x + 1)
    dp[0] = True
    for y in nums:
        tmp = [False] * (x + 1)
        for i in range(len(dp)):
            if i + y < len(tmp):
                tmp[i + y] |= dp[i]
            if i - y >= 0:
                tmp[i - y] |= dp[i]
        dp = tmp
    return dp[x]


def solve(s, x, y):
    type_num = [(k, len(list(g))) for k, g in groupby(s)]
    if s[0] == "F":
        x -= type_num[0][1]
        type_num.pop(0)

    h, v = [], []
    now = 0
    for t, n in type_num:
        if t == "F":
            if now == 0:
                h.append(n)
            else:
                v.append(n)
        else:
            if n % 2 != 0:
                now = (now + 1) % 2
    return YES if check(x, h) and check(y, v) else NO


def main():
    s = input()
    x, y = map(int, input().split())

    print(solve(s, x, y))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N=int(input())
a=[int(i) for i in input().split()]
data={}
for num in a:
    data[num]+=1
 
ans=0
for key in data.keys():
        if data[key]<key:
                ans+=data[key]
        else:
                ans+=data[key]-key
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter
from itertools import product
def inpl(): return tuple(map(int, input().split()))
   

S = input()
x, y = inpl()
M = []

d = 0
for s in S:
    if s == "F":
        d += 1
    else:
        M.append(d)
        d = 0
M.append(d)

Cx = Counter(M[2::2])
Cy = Counter(M[1::2])

Lx = []
Ly = []

for k, v in Cx.items():
    if k == 0:
        pass
    else:
        Lx.append(list(range(-k*v, k*v+1, 2*k)))
        
for k, v in Cy.items():
    if k == 0:
        pass
    else:
        Ly.append(list(range(-k*v, k*v+1, 2*k)))

def bfss(Ls, t, f):
    N = set([f])
    for L in Ls:
        nN = set(([n+l for n, l in product(N, L)]))
        N = nN
    if t in N:
        return True
    else:
        return False

if bfss(Lx, x, M[0]) and bfss(Ly, y, 0):
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
N = input()
count = 0
if (N=0):
  count = 0
else:
  a = []
  for i in range(N):
    a.append(map(int, raw_input().split()))
  a.sort()
  a.reverse()
  i = 0
  while a[0] > N:
    del a[0]
    i++
  key = a[0]
  while key > 0
    b = a.count(key)
    if (b < key):
      i += b
    elif(b > key):
      i += b - key
  count = i
print count

Submission status: Runtime Error

Solution code:
s = str(input())
x, y = map(int, input().split())

n = 8000
fs = list(map(len, s.split('T')))
x -= fs[0]
dx = []
dy = []
for i in range(1, len(fs)):
    if fs[i] == 0:
        continue
    if i % 2 == 0:
        dx.append(fs[i])
    else:
        dy.append(fs[i])
dpx = [[False for _ in range(4 * n + 1)] for _ in range(2)]
dpy = [[False for _ in range(4 * n + 1)] for _ in range(2)]
dpx[0][2 * n] = True
dpy[0][2 * n] = True
for i in range(len(dx)):
    for j in range(4 * n + 1):
        if dpx[i % 2][j]:
            if 0 <= j - dx[i]:
                dpx[(i + 1) % 2][j - dx[i]] = True
            if j + dx[i] <= 4 * n:
                dpx[(i + 1) % 2][j + dx[i]] = True
for i in range(len(dy)):
    for j in range(4 * n + 1):
        if dpy[i % 2][j]:
            if 0 <= j - dy[i]:
                dpy[(i + 1) % 2][j - dy[i]] = True
            if j + dy[i] <= 4 * n:
                dpy[(i + 1) % 2][j + dy[i]] = True
ix = 2 * n + x
iy = 2 * n + y
if 0 <= ix <= 4 * n and dpx[len(dx) % 2][ix] and 0 <= iy <= 4 * n and dpy[len(dy) % 2][iy]:
    print("Yes")
else:
    print("No")