Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

class CumulativeSum2D:
  def __init__(self,a):
    self.h=len(a)
    self.w=len(a[0])
    self.s=[(self.w+1)*[0]for _ in range(self.h+1)]
    for i in range(self.h):
      for j in range(self.w):
        self.s[i+1][j+1]=self.s[i+1][j]+self.s[i][j+1]-self.s[i][j]+a[i][j]
  def su(self,i1,j1,i2,j2):
    return self.s[i2+1][j2+1]-self.s[i1][j2+1]-self.s[i2+1][j1]+self.s[i1][j1]
  def prin(self):
    for i in self.s:print(i)
h,w,kk=map(int,input().split())
b=[list(map(int,list(input())))for _ in range(h)]
h-=1
bs=CumulativeSum2D(b)
ans=h+w-1
for i in range(2**h):
  bi=[-1]+[j for j in range(h)if(1<<j)&i]+[h]
  anss=len(bi)-2
  flag=False
  cut=[0]
  for k in range(w):
    for j in range(len(bi)-1):
      up=bi[j]+1
      down=bi[j+1]
      s=bs.su(up,cut[-1],down,k)
      if s>kk:
        if k==cut[-1]:flag=True
        else:cut.append(k);anss+=1
        break
    if flag:break
  if flag:continue
  ans=min(ans,anss)
print(ans)

### FIXED CODE ###

class CumulativeSum2D:
  def __init__(self,a):
    self.h=len(a)
    self.w=len(a[0])
    self.s=[(self.w+1)*[0]for _ in range(self.h+1)]
    for i in range(self.h):
      for j in range(self.w):
        self.s[i+1][j+1]=self.s[i+1][j]+self.s[i][j+1]-self.s[i][j]+a[i][j]
  def su(self,i1,j1,i2,j2):
    return self.s[i2+1][j2+1]-self.s[i1][j2+1]-self.s[i2+1][j1]+self.s[i1][j1]
  def prin(self):
    for i in self.s:print(i)
h,w,kk=map(int,input().split())
b=[list(map(int,list(input())))for _ in range(h)]
h-=1
bs=CumulativeSum2D(b)
ans=h+w-1
for i in range(2**h):
  bi=[-1]+[j for j in range(h)if(1<<j)&i]+[h]
  anss=len(bi)-2
  flag=False
  cut=[0]
  for k in range(w):
    for j in range(len(bi)-1):
      up=bi[j]+1
      down=bi[j+1]
      s=bs.su(up,cut[-1],down,k)
      if s>kk:
        if k==cut[-1]:flag=True
        else:cut.append(k);anss+=1
        break
    if flag:break
  if flag:continue
  ans=min(ans,anss)
print(ans)
### BUGGY CODE ###

N,M = map(int, input().split())
A = [int(input()) for i in range(M)]

dp = [0]*(N+1)

dp[0] = 1
for now in range(N+1):
    next1 = now + 1
    next2 = now + 2
    if next1 <= N:
        if next1 not in A:
            dp[next1] += dp[now]
    if next2 <= N:
        if next2 not in A:
            dp[next2] += dp[now]
print(dp[N]%1000000007)

### FIXED CODE ###

N,M = map(int, input().split())
A = set([int(input()) for i in range(M)])

dp = [0]*(N+1)
dp[0] = 1

for next in range(1,N+1):
    if next in A:
        dp[next] = 0
    else:
        dp[next] = (dp[next-1] + dp[next-2])%1000000007

print(dp[N])
### BUGGY CODE ###

def solve():
    r1, c1, r2, c2 = map(int, input().split())
    
    # Combination calculation
    p = 10 ** 9 + 7
    N = r2 + c2 + 2
    fact = [1, 1]
    factinv = [1, 1]
    inv = [0, 1]
    
    for i in range(2, N + 1):
        fact.append((fact[-1] * i) % p)
        inv.append((-inv[p % i] * (p // i)) % p)
        factinv.append((factinv[-1] * inv[-1]) % p)
    
    def comb(n, r):
        return fact[n] * factinv[r] * factinv[n - r] % p
    
    def f(i, j):
        return comb(i + j, i)
    
    def g(i, j):
        return f(i + 1, j + 1) - 1
    
    ans = g(r2, c2) - g(r2, c1 - 1) - g(r1 - 1, c2) + g(r1 - 1, c1 - 1)
    
    print(ans % p)

solve()

### FIXED CODE ###

def solve():
    r1, c1, r2, c2 = map(int, input().split())
    
    p = 10 ** 9 + 7
    N = r2 + c2 + 2
    fact = [1, 1]
    for i in range(2, N + 1):
        fact.append((fact[-1] * i) % p)
    
    def comb(n, r):
        a = fact[n]
        b = pow(fact[r], p - 2, p)
        c = pow(fact[n-r], p - 2, p)
        return a * b * c % p
    
    def f(i, j):
        return comb(i + j, i)
    
    def g(i, j):
        return f(i + 1, j + 1) - 1
    
    ans = g(r2, c2) - g(r2, c1 - 1) - g(r1 - 1, c2) + g(r1 - 1, c1 - 1)
    
    print(ans % p)

solve()
### BUGGY CODE ###

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

for i in range(1, N+1):
    print(A.count(i))

### FIXED CODE ###

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

B = [0] * N

for i in A:
    B[i-1] = B[i-1] + 1

for j in B:
    print(str(j))
### BUGGY CODE ###

H, W, M = map(int, input().split())
hw = []
sum_h = [0] * W
sum_w = [0] * H
for _ in range(M):
    h, w = map(int, input().split())
    hw.append([h, w])
    h -= 1
    w -= 1
    sum_h[w] += 1
    sum_w[h] += 1


max_h = max(sum_h)
max_w = max(sum_w)

new_H = [h for h in range(H) if sum_w[h] == max_w]
new_W = [w for w in range(W) if sum_h[w] == max_h]

max_num = max_h + max_w - 1
for i in new_H:
    for j in new_W:
        if [i + 1, j + 1] not in hw:
            print(max_h + max_w)
            exit()
        else:
            continue
        break

print(max_num)


### FIXED CODE ###

H, W, M = map(int, input().split())
hw = set()
sum_h = [0] * W
sum_w = [0] * H
for _ in range(M):
    h, w = map(int, input().split())
    hw.add((h, w))
    h -= 1
    w -= 1
    sum_h[w] += 1
    sum_w[h] += 1


max_h = max(sum_h)
max_w = max(sum_w)

new_H = [h for h in range(H) if sum_w[h] == max_w]
new_W = [w for w in range(W) if sum_h[w] == max_h]

max_num = max_h + max_w - 1
for i in new_H:
    for j in new_W:
        if (i + 1, j + 1) not in hw:
            print(max_h + max_w)
            exit()
        else:
            continue
        break

print(max_num)
### BUGGY CODE ###


def ev(n):
    return 0.5*(n+1)


def main():
    n, k = map(int, input().split())
    p = list(map(int, input().split()))

    ans = []
    for i in range(n - k + 1):
        ans.append(sum(map(ev, p[i: i + k])))
    print(max(ans))


if __name__ == '__main__':
    main()


### FIXED CODE ###

# coding=UTF-8
from collections import deque
from operator import itemgetter
from bisect import bisect_left, bisect
import itertools
import sys
import math
import numpy as np
import time
sys.setrecursionlimit(10**6)
input = sys.stdin.readline


def ev(n):
    return 0.5*(n+1)


def main():
    n, k = map(int, input().split())
    p = list(map(int, input().split()))

    p_ev = list(map(ev, p))

    s = np.cumsum(p_ev)
    s = np.insert(s, 0, 0)

    ans = []

    for i in range(n - k+1):
        ans.append(s[i + k] - s[i])

    print(max(ans))


if __name__ == '__main__':
    main()
### BUGGY CODE ###

import math
s = input().split()
n=int(s[0])
m=int(s[1])
d={}
for i in range(1,n+1):d[i]=0
for i in range(m):
    s = list(map(int,input().split()))
    for j in range(s[0],s[1]+1):
        d[j]+=1
print(list(d.values()).count(m))


### FIXED CODE ###

import math
s = input().split()
n=int(s[0])
m=int(s[1])
l=0
r=float('inf')
for i in range(m):
    s = list(map(int,input().split()))
    l=max(l,s[0])
    r=min(r,s[1])
a=r-l
if a>=0:
    print(a+1)
else:
    print(0)
### BUGGY CODE ###

N, K = map(int, input().split())
H = list(map(int, input().split()))
P = [0] + [-1] * (len(H)-1)

def minv(i, v):
  if i < len(H):
    cost = v()
    if P[i] < 0:
      P[i] = cost
    else:
      P[i] = min(P[i], cost)

for i in range(len(H)-1):
  for k in range(1, K+1):
    minv(i + k, lambda : P[i] + abs(H[i] - H[i+k]))

print(P[-1])

### FIXED CODE ###

N, K = map(int, input().split())
H = list(map(int, input().split()))
P = [0] + [-1] * (len(H)-1)

def minv(i, v):
  if i < len(H):
    cost = v()
    if P[i] < 0:
      P[i] = cost
    else:
      P[i] = min(P[i], cost)

for i in range(len(H)-1):
  for k in range(1, K+1):
    minv(i + k, lambda : P[i] + abs(H[i] - H[i+k]))

print(P[-1])
### BUGGY CODE ###

h, w = map(int, input().split())
s = [list(map(str, input().rstrip())) for _ in range(h)]

score_h = [[0] * w for _ in range(h)]
score_w = [[0] * h for _ in range(w)]

for i in range(h):
    tmp = 0
    for j in range(w):
        if s[i][j] == "#":
            tmp = 0
            continue
        else:
            tmp += 1
            score_h[i][j] = tmp

for i in range(h):
    for j in range(w - 1, 0, -1):
        if score_h[i][j] == 0 or score_h[i][j - 1] == 0:
            continue
        else:
            score_h[i][j - 1] = score_h[i][j]

sT = list(zip(*s))
sT = [list(x) for x in sT]

for i in range(w):
    tmp = 0
    for j in range(h):
        if sT[i][j] == "#":
            tmp = 0
            continue
        else:
            tmp += 1
            score_w[i][j] = tmp

for i in range(w):
    for j in range(h - 1, 0, -1):
        if score_w[i][j] == 0 or score_w[i][j - 1] == 0:
            continue
        else:
            score_w[i][j - 1] = score_w[i][j]

score_w = list(zip(*score_w))
score_w = [list(x) for x in score_w]

ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, score_h[i][j] + score_w[i][j] - 1)

print(ans)

### FIXED CODE ###

import numpy as np
h, w = map(int, input().split())
arr = np.array([list(input()) for _ in range(h)]) == "."

l = np.zeros((h, w), dtype=int)
r = np.zeros((h, w), dtype=int)
u = np.zeros((h, w), dtype=int)
d = np.zeros((h, w), dtype=int)

for i in range(h):
    u[i] = (u[i - 1] + 1) * arr[i]
    d[- i - 1] = (d[- i] + 1) * arr[- i - 1]

for j in range(w):
    l[:, j] = (l[:, j-1] + 1) * arr[:, j]
    r[:, - j - 1] = (r[:, - j] + 1) * arr[:, - j - 1]

print(np.max(u + d + l + r) - 3)
### BUGGY CODE ###

import collections
import itertools
import functools
import math
import scipy.special

if __name__ == "__main__":
    k, s = map(int, input().split())
    res = 0
    mi = min(k, s)
    ma = max(k, s)
    for i in range(mi + 1):
        for j in range(mi + 1):
            res += int(0 <= s - i - j <= k)
    print(res)

### FIXED CODE ###

import collections
import itertools
import functools
import math
import scipy.special

if __name__ == "__main__":
    k, s = map(int, input().split())
    res = 0
    for i in range(k + 1):
        for j in range(k + 1):
            if 0 <= s - i - j <= k:
                res += 1
    print(res)
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
ans = 1

for i in a:
  ans = ans * i
    
if ans > 10**18:
  print(-1)
else:  
  print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
ans = 1

if 0 in a:
  print(0)
  exit()
  
for i in range(n):
  ans = ans*a[i]
  if ans > 10**18:
    print(-1)
    exit()
    
print(ans)
### BUGGY CODE ###

N, K = map(int, input().split())
H = list(map(int, input().split()))
H = [H[0]]*K+H
def solve(H):
    dp = [0]*(N+K)
    for i in range(K,N+K):
        dp[i]= 10**10
        for j in range(1,K+1):
            dp[i] = min(dp[i],dp[i-j]+abs(H[i]-H[i-j]))
    ans = dp[-1]
    return ans
print(solve(H))

### FIXED CODE ###

N, K = map(int, input().split())
H = list(map(int, input().split()))
H = [H[0]]*K+H
def solve(H):
    dp = [0]*(N+K)
    for i in range(K,N+K):
        dp[i]= 10**10
        for j in range(1,K+1):
            dp[i] = min(dp[i],dp[i-j]+abs(H[i]-H[i-j]))
    ans = dp[-1]
    return ans
print(solve(H))
### BUGGY CODE ###

#!/usr/bin/env python3
from fractions import gcd
from functools import reduce

N = int(input())

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

L = reduce((lambda x, y: x // gcd(x, y) * y), A)

res = 0

MOD = 10**9+7

for a in A:
  res = (res + L//a % MOD) % MOD

print(res)

### FIXED CODE ###

#!/usr/bin/env python3
from fractions import gcd
from functools import reduce

N = int(input())

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

MOD = 10**9+7

L = reduce((lambda x, y: x // gcd(x, y) * y), A) % MOD

res = 0

def modulo(x, y):
  tmp = x % y
  if tmp < 0: tmp += y
  return tmp

def extgcd(x, y):
  if x < y:
    a, b = extgcd(y, x)
    return b, a
  if y == 0 or x % y == 0: return 0, 1
  a, b = extgcd(y, modulo(x , y))
  return (b, -(x // y) * b + a)

def inv(x):
  a, _ = extgcd(x,MOD)
  return modulo(a, MOD)

for a in A:
  res = (res + L* inv(a) % MOD) % MOD

print(res)
### BUGGY CODE ###

# D - Gathering Children
import re

s = input()
sList = list(s)

# 同じ文字が連続していると端のマスに子供が集約される（Rなら右端、Lなら左端）
# 集約が完了したらあとは何回やってもRL間での位置の入れ替えのみ
# → 集約完了回数分は計算、残りの回数が奇数回なら追加で1回試行
retList = []
matchList = re.findall('R+L+', s)

for match in matchList:
    rCnt = match.count('R')
    integrateCnt = (rCnt if rCnt > len(match) - rCnt else len(match) - rCnt) - 1
    addCnt = (10**100 - integrateCnt) % 2

    n = [1] * len(match)
    ret = [1] * len(match)
    for i in range(integrateCnt + addCnt):
        for j, way in enumerate(match):
            ret[j] -= n[j]
            if way == 'R':
                ret[j + 1] += n[j]
            elif way == 'L':
                ret[j - 1] += n[j]
        n = ret[:]
    retList.extend(ret)
print(' '.join(map(str, retList)))

### FIXED CODE ###

# D - Gathering Children
import re

s = input()
sList = list(s)

# RR...RLL...Lは、「RL」の部分に子供が集約される
# RLそれぞれの連続する個数が
# ・同じ   → 同値
# ・異なる → 入れ替えごとに交互に余りを足していく
#          → 差が偶数なら同値
#             差が奇数ならいずれかを1大きくする
retList = []
matchList = re.findall('R+L+', s)

for match in matchList:
    rCnt = match.count('R')
    lCnt = len(match) - rCnt
    
    half = len(match) // 2
    dif = abs(rCnt - lCnt)

    # まずは均等配分する
    ret = [0] * len(match)
    ret[rCnt - 1] = half
    ret[-lCnt] = half
    
    # 個数が同じ         → 既に同値にしているので終了
    # 奇数だけど差が偶数 → 既に同値にしているので終了
    # 奇数かつ  差が奇数
    if dif % 2 != 0:

        # 集約完了までに動かした回数を計算
        changeCnt = (rCnt if rCnt < lCnt else lCnt) - 1 + dif
        
        # 集約完了時点で連続する個数が多かった側が1大きくなっている
        # → 残りの試行回数が奇数なら逆転、偶数ならそのまま
        if (rCnt < lCnt and changeCnt % 2 != 0) or (rCnt > lCnt and changeCnt % 2 == 0):
            ret[rCnt - 1] = half + 1
            ret[-lCnt] = half
        else:
            ret[rCnt - 1] = half
            ret[-lCnt] = half + 1
    retList.extend(ret)
print(' '.join(map(str, retList)))
### BUGGY CODE ###

import math

p = 0
n = int(input())

for i in range(n):
    a = int(input())
    
    if a == 2:
        p += 1
    elif a%2 == 0:
        pass
    else:
        d = int(math.sqrt(a))
        i = 3
        while i < d:
            if a%i == 0:
                break
        p += 1

print(p)

### FIXED CODE ###

import math

def prime(x):
    if x == 2:
        return True
    elif x%2 == 0:
        return False
    else:
        i = 3
        while i <= math.sqrt(x):
           if x%i == 0:
                return False
           else:
                i += 2
        return True

        
        

p = 0
n = int(input())

for i in range(n):
    if prime(int(input())) == True:
        p += 1
        

print(p)
### BUGGY CODE ###

N = int(input())
N_S = map(int, input().split())
n = 1
for i in N_S:
  if i == 0:
    print('0')
    break
  else:
    n *=i
else:
  if n > 10**18:
    print('-1')
  else:
    print(n)

### FIXED CODE ###

n = int(input())
a = [i for i in map(int, input().split())]
 
if a.count(0) != 0:
  print(0)
else:
  result = 1
  for i in range(n):
    result *= a[i]
    if result > (10**18):
      print(-1)
      break
    else:
      continue
  else:
    print(result)
### BUGGY CODE ###

import copy

N = int(input())
A = [input() for i in range(N)]

B = []
for i in range(N):
    B = copy.copy(A)
    B.pop(i)
    print(max(B))

### FIXED CODE ###

N = int(input())
A = [int(input()) for i in range(N)]

a = max(A)
b = sorted(A)[-2]
for i in A:
    print(b if i == a else a)
### BUGGY CODE ###

n, m = map(int, input().split())

l, r = map(int, input().split())
gate = set(range(l, r+1))
for i in range(m-1):
    l, r = map(int, input().split())
    gate = set(range(l, r+1)) & gate

print(len(gate))

### FIXED CODE ###

_, m = map(int, input().split())

mi, mx = map(int, input().split())
flag = False
for i in range(m-1):
    l, r = map(int, input().split())
    if mi < l <= mx:
        mi = l
    if mi <= r < mx:
        mx = r
    if mx < l or r < mi:
        flag = True
        break

if flag:
    print(0)
else:
    print(mx - mi + 1)
### BUGGY CODE ###

H, W, D = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
exam = [list(map(int, input().split())) for _ in range(Q)]

def search(x):
    j = 0
    for i, Ai in enumerate(A):
        if x in Ai:
            j = Ai.index(x)
            break
    return (i+1,j+1)

for L, R in exam:
    cost = 0
    nw = L
    nt = L + D
    for _ in range((R-L)//D):
        il, jl = search(nw)
        ir, jr = search(nt)
        nw = nt
        nt = nw + D
        cost += abs(il - ir) + abs(jl - jr)
    print(cost)



### FIXED CODE ###

from itertools import chain

H, W, D = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
exam = [list(map(int, input().split())) for _ in range(Q)]

x_a = [0] * (H*W)
y_a = [0] * (H*W)

for i, ax in enumerate(A):
    for j, ay in enumerate(ax):
        x_a[ay-1] = i
        y_a[ay-1] = j

S = [0] * (H*W)
for i in reversed(range(H*W-D)):
    S[i] = abs(x_a[i] - x_a[i+D]) + abs(y_a[i] - y_a[i+D]) + S[i + D]

for L, R in exam:
    print(S[L-1] - S[R-1])
### BUGGY CODE ###

h,w = map(int,input().split())
b = [[i == "#" for i in list(input())]+[True] for _ in range(h)]+[[True]*(w+1)]
#print(b)
check = []
for i in range(h):
  for j in range(w):
    if b[i][j]:
      check.append((i,j))
ans = 0
while check:
  new = []
  k = 0
  for i,j in check:
    if not b[i-1][j]:
      new.append((i-1,j))
      b[i-1][j] = True
      k = 1
    if not b[i+1][j]:
      new.append((i+1,j))
      b[i+1][j] = True
      k = 1
    if not b[i][j-1]:
      new.append((i,j-1))
      b[i][j-1] = True
      k = 1
    if not b[i][j+1]:
      new.append((i,j+1))
      b[i][j+1] = True
      k = 1
  check = new
  if k:
    ans += 1
  #print(b)
print(ans)

### FIXED CODE ###

h,w = map(int,input().split())
b = [[i == "#" for i in list(input())]+[True] for _ in range(h)]+[[True]*(w+1)]
check = []
for i in range(h):
  for j in range(w):
    if b[i][j]:
      check.append((i,j))
ans = 0
while check:
  new = []
  k = 0
  for i,j in check:
    if not b[i-1][j]:
      new.append((i-1,j))
      b[i-1][j] = True
      k = 1
    if not b[i+1][j]:
      new.append((i+1,j))
      b[i+1][j] = True
      k = 1
    if not b[i][j-1]:
      new.append((i,j-1))
      b[i][j-1] = True
      k = 1
    if not b[i][j+1]:
      new.append((i,j+1))
      b[i][j+1] = True
      k = 1
  check = new
  if k:
    ans += 1
print(ans)
### BUGGY CODE ###

mod = 10**9+7
def main():
  n = int(input())
  a = []
  for i in range(n):
    a.extend(list(map(int,input().split())))
  dp = [0]*(1<<n)
  dp[0] = 1
  for S in range((1<<n)-1):
    k = bin(S).count("1")*n
    t = dp[S]
    if t:
      for i in range(n):
        if a[k+i]:
          dp[S|1<<i] = (dp[S|1<<i]+t)%mod
  print(dp[(1<<n)-1])
main()

### FIXED CODE ###

def main():
  n = int(input())
  mod = 10**9+7
  a = []
  for i in range(n):
    a.extend(list(map(int,input().split())))
  dp = [0]*(1<<n)
  dp[0] = 1
  for S in range((1<<n)-1):
    k = bin(S).count("1")*n
    t = dp[S]
    for i in range(n):
      if a[k+i]:
        dp[S|1<<i] = (dp[S|1<<i]+t)%mod
  print(dp[(1<<n)-1])
main()
### BUGGY CODE ###

from collections import deque
from heapq import heappop, heappush

def main():
    h,w,k = map(int,input().split())
    x,y,gx,gy = map(int,input().split())
    x -= 1
    gx -= 1
    y -= 1
    gy -= 1
    c = [list(input()) for _ in range(h)]
    d = [[-1 for _ in range(w)] for _ in range(h)]
    dx = (1, 0, -1, 0)
    dy = (0, 1, 0, -1)
    que = []
    heappush(que,(0,(x,y)))
    d[x][y] = 0
    while que:
        [num,(qx,qy)] = heappop(que)
        if qx == gx and qy == gy:
            break
        for i in range(4):
            for K in range(1,k+1):
                nx = qx + dx[i]*K
                ny = qy + dy[i]*K
                if not(nx>=0 and h>nx and ny>=0 and w>ny) or c[nx][ny] == '@':
                    break
                if d[nx][ny] == -1 or d[nx][ny] > d[qx][qy]+1:
                    d[nx][ny] = d[qx][qy] + 1
                    heappush(que,[d[nx][ny],(nx,ny)])
    print(d[gx][gy])


if __name__ == "__main__":
    main()

### FIXED CODE ###

from collections import deque
def main():
    h,w,k = map(int,input().split())
    x,y,gx,gy = map(int,input().split())
    x -= 1
    gx -= 1
    y -= 1
    gy -= 1
    c = [list(input()) for _ in range(h)]
    d = [[-1 for _ in range(w)] for _ in range(h)]
    dx = (1, 0, -1, 0)
    dy = (0, 1, 0, -1)
    que = deque()
    que.append((x,y))
    d[x][y] = 0
    while que:
        (qx,qy) = que.popleft()
        if qx == gx and qy == gy:
            break
        for i in range(4):
            for K in range(1,k+1):
                nx = qx + dx[i]*K
                ny = qy + dy[i]*K
                if not(nx>=0 and h>nx and ny>=0 and w>ny) or c[nx][ny] == '@' or (d[nx][ny] != -1 and d[nx][ny] < d[qx][qy]+1):
                    break
                if d[nx][ny] == -1 or d[nx][ny] > d[qx][qy]+1:
                    d[nx][ny] = d[qx][qy] + 1
                    que.append((nx,ny))
    print(d[gx][gy])
 
 
if __name__ == "__main__":
    main()
### BUGGY CODE ###

n = int(raw_input())
A = map(int, raw_input().split())
q = int(raw_input())
m = map(int, raw_input().split())

def henkan(x):
    s = [0]*n
    i = 0
    while x != 0:
        s[i] = x%2
        x /= 2
        i += 1
    return s

for k in range(0, q):
    frag = 0
    for i in range(1, 2**n-1):
        sum = 0
        s = henkan(i)
        for j in range(0, len(s)):
            if s[j] == 1:
                sum += A[j]
        if sum == m[k]:
            print "yes"
            frag = 1
            break
    if frag == 0:
        print "no"

### FIXED CODE ###

n = int(raw_input())
A = map(int, raw_input().split())
q = int(raw_input())
m = map(int, raw_input().split())
 
def solve(i, m):
    if m == 0:
        return 1
    if i  >= n or m > sum(A):
        return 0
    res = solve(i + 1, m) or solve(i + 1, m - A[i])
    return res
         
for j in range(0, q):
    if solve(0, m[j]):
        print "yes"
    else:
        print "no"
### BUGGY CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
 
t = 0
now = 1
footprint = [1]
 

while t < K:
  if A[now-1] not in footprint:
    footprint.append(A[now-1])
    now = A[now-1]
    t += 1
  else:
    s = footprint.index(A[now-1])
    l = t - s + 1
    e = (K-s) % l
    now = footprint[s+e]
    break
    
print(now)

### FIXED CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
 
t = 0
now = 1
footprint = [1]
past = [0]*N
past[0] = 1

while t < K:
  if past[A[now-1]-1] == 0:
    now = A[now-1]
    footprint.append(now)
    past[now-1]=1
    t += 1
  else:
    s = footprint.index(A[now-1])
    l = t - s + 1
    e = (K-s) % l
    now = footprint[s+e]
    break
    
print(now)
### BUGGY CODE ###

k, s = map(int, input().split())
ans = 0

for x in range(0, k+1):
    for y in range(0, k+1):
          if ((x + y + (s-x-y)) == s) and (0 <= s-x-y <= k):
              ans+=1

print(ans)


### FIXED CODE ###

k, s = map(int, input().split())
ans = 0

for x in range(0, k+1):
    for y in range(0, k+1):
          if ((x + y + (s-x-y)) == s) and (0 <= s-x-y <= k):
              ans+=1

print(ans)