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

n=int(input())
p=list(map(int,input().split()))

ans=0

for i in range(n-1):
  li=p[i:i+2]
  a1=max(li)
  a2=min(li)
  for j in range(i+1,n):
    if a2<=p[j]<a1:
      a2=p[j]
    elif a1<p[j]:
      a2=a1
      a1=p[j]
    ans+=a2

print(ans)

### FIXED CODE ###

import sys

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


# rstrip().decode('utf-8')
# INF=float("inf")
#MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
#import numpy as np
# import operator
import bisect
# from heapq import heapify,heappop,heappush
#from math import gcd
# from fractions import gcd
#from collections import deque
#from collections import defaultdict
# from collections import Counter
#from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())


def main():
	n,*p=map(int,read().split())
	H=[0,0,n+1,n+1]
	pidx=[0]*(n+1)
	for i in range(n):
		pidx[p[i]]=i+1
	
	pidx.reverse()
	
	
	ans=0
	for i in pidx:
		I=bisect.bisect_left(H,i)
		ans+=p[i-1]*((H[I-1]-H[I-2])*(H[I]-i)+(H[I+1]-H[I])*(i-H[I-1]))
		H.insert(I,i)
	print(ans)
	
	
if __name__ == "__main__":
		main()
### BUGGY CODE ###

# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import combinations
#import bisect# lower_bound etc
import numpy as np
def run():
    N, K, *A = list(map(int, read().split()))
    B = [a for a in A if a>0]
    C = [a for a in A if a<0]
    C_abs = [-c for c in C]
    O = A.count(0)
    B = sorted(B)
    C = sorted(C)
    C_abs = sorted(C_abs)
    n_neg = len(B) * len(C)
    n_0 = O * (O - 1) // 2 + O * (len(B) + len(C))

    def find(x):
        ret = 0
        if x < 0:
            for b in B:
                ret += np.searchsorted(C, x//b, side='right')

        elif x == 0:
            ret += n_0 + n_neg
        else:
            ret += n_neg + n_0
            for i, b in enumerate(B):
                ret += np.searchsorted(B[i+1:], x//b, side='right')

            for i, c in enumerate(C_abs):
                ret += np.searchsorted(C_abs[i+1:], x//c, side='right')
        return ret





    high = 10 ** 18
    low = -high
    current = 0
    while high - low > 1:
        tmp = find(current)
        if tmp < K:
            low = current
        else:
            high = current

        current = (low + high) // 2

    print(high)

if __name__ == "__main__":
    run()

### FIXED CODE ###

# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import combinations
#import bisect# lower_bound etc
import numpy as np
def run():
    N, K, *A = list(map(int, read().split()))
    B = [a for a in A if a>0]
    C = [a for a in A if a<0]
    C_abs = [-c for c in C]
    O = A.count(0)
    B = np.array(sorted(B), dtype= np.int64)
    C = np.array(sorted(C), dtype = np.int64)
    C_abs = np.array(sorted(C_abs), dtype = np.int64)
    n_neg = len(B) * len(C)
    n_0 = O * (O - 1) // 2 + O * (len(B) + len(C))

    def find(x):
        ret = 0
        if x < 0:
            ret += np.searchsorted(C, x//B, side='right').sum()

        elif x == 0:
            ret += n_0 + n_neg
        else:
            ret += n_neg + n_0
            b_count = (B ** 2 <= x).sum()
            c_count = (C ** 2 <= x).sum()
            ret += (np.searchsorted(B, x//B, side='right').sum() - b_count) // 2

            ret += (np.searchsorted(C_abs, x//C_abs, side='right').sum() - c_count) // 2
        return ret

    high = 10 ** 18
    low = -high
    current = 0
    while high - low > 1:
        tmp = find(current)
        if tmp < K:
            low = current
        else:
            high = current

        current = (low + high) // 2

    print(high)

if __name__ == "__main__":
    run()
### BUGGY CODE ###

def main():
    MOD = 998244353

    N, S = map(int, input().split())
    *A, = map(int, input().split())

    dp = [[[0] * 3 for _ in range(S + 1)] for _ in range(N + 1)]
    dp[0][0][0] = 1

    for i, x in enumerate(A):
        for s in range(S + 1):
            dp[i + 1][s][0] = (dp[i + 1][s][0] + dp[i][s][0]) % MOD
            dp[i + 1][s][1] = (dp[i + 1][s][1] + dp[i][s][1] + dp[i][s][0]) % MOD
            dp[i + 1][s][2] = (dp[i + 1][s][2] + dp[i][s][2] + dp[i][s][1] + dp[i][s][0]) % MOD

            if s - x >= 0:
                dp[i + 1][s][1] = (dp[i + 1][s][1] + dp[i][s - x][1] + dp[i][s - x][0]) % MOD
                dp[i + 1][s][2] = (dp[i + 1][s][2] + dp[i][s - x][1] + dp[i][s - x][0]) % MOD
        
    print(dp[N][S][2])


if __name__ == '__main__':
    main()

#             dp[i + 1][s][0] = (dp[i + 1][s][0] + dp[i][s][0]) % MOD
#             dp[i + 1][s][1] = (dp[i + 1][s][1]
#                                + dp[i][s][1] + (dp[i][s - x][1] if s - x >= 0 else 0)
#                                + dp[i][s][0] + (dp[i][s - x][0] if s - x >= 0 else 0)) % MOD
#             dp[i + 1][s][2] = (dp[i + 1][s][2]
#                                + dp[i][s][2] +
#                                dp[i][s][1] + (dp[i][s - x][1] if s - x >= 0 else 0)
#                                + dp[i][s][0] + (dp[i][s - x][0] if s - x >= 0 else 0)) % MOD


### FIXED CODE ###

# https://atcoder.jp/contests/abc159/submissions/11139734

def main():
    MOD = 998244353

    N, S = map(int, input().split())
    *a, = map(int, input().split())

    dp = [0] * (S + 1)

    ret = 0
    for i, x in enumerate(a):
        dp[0] += 1
        if x > S: continue
        ret = (ret + dp[S - x] * (N - i)) % MOD
        for k in range(S - 1, x - 1, -1):
            dp[k] = (dp[k] + dp[k - x]) % MOD

    print(ret)


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

from collections import deque

h,w = map(int,input().split())
maze=[]

for _ in range(h):
    maze.append(input())

ans = 0

move = [[1,0],[-1,0],[0,1],[0,-1]]

for i in range(h):
    for j in range(w):
        if(maze[i][j]=="."):
            dist = [[999999]*w for _ in range(h)]
            
            dq = deque()
            y=i
            x=j
            d=0
            
            dq.append((y,x,d))
            
            while(len(dq)):
                
                y,x,d = dq.popleft()
                dist[y][x]=d
                for m in move:
                    if((0<=x+m[0]<w) and (0<=y+m[1]<h) and
                        (dist[y+m[1]][x+m[0]] > d+1) and
                            (maze[y+m[1]][x+m[0]] == ".")):
                                dq.append((y+m[1],x+m[0],d+1))
                                
            ans = max(ans,d)
            # print(d,i,j)
print(ans)


### FIXED CODE ###

from collections import deque

h,w = map(int,input().split())
maze = []

for _ in range(h):
    maze.append(input())

ans = 0

move = [[1,0],[-1,0],[0,1],[0,-1]]

for i in range(h):
    for j in range(w):
        if(maze[i][j]=="."):
            dist = [ [99999999]*w for _ in range(h) ]
            
            dq = deque()
            y=i
            x=j
            d=0
            dist[y][x]=0
            dq.append((y,x,d))
            
            while(len(dq)):
                
                y,x,d = dq.popleft()
                
                for m in move:
                    if((0<=x+m[0]<w) and (0<=y+m[1]<h) and
                        (dist[y+m[1]][x+m[0]] > d+1) and
                            (maze[y+m[1]][x+m[0]] == ".")):
                                dist[y+m[1]][x+m[0]]=d+1
                                dq.append((y+m[1],x+m[0],d+1))
                                
            ans = max(ans,d)
            # print(d,i,j)
print(ans)
### BUGGY CODE ###

s=input()
n=len(s)
List=[0]*2019
ans=0

for i in range(0,n):
  List[(int(s[i:])%2019)]+=1
  #print(s[i:],int(s[i:])%2019)
  
#print(List)
ans=List[0]
for i in List:
  if (i>=2):
    ans+=(i*(i-1))//2
  
print(ans)

### FIXED CODE ###

S=input()[::-1]
List=[0]*2019
ans=0
a=1
d=0

for s in S:
  d=d+int(s)*a
  List[d%2019]+=1
  a=a*10
  a=a%2019
  #print(s[i:],int(s[i:])%2019)
  
#print(List)
ans=List[0]
for i in List:
  if (i>=2):
    ans+=(i*(i-1))//2
  
print(ans)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
n, k = map(int, input().split())
s = input()

hsc = []
pre = s[0]
cnt = 0
for c in s:
    if c == pre:
        cnt += 1
    else:
        if pre == '0':
            hsc.append(-cnt)
        else:
            hsc.append(cnt)
        pre = c
        cnt = 1
if pre == '0':
    hsc.append(-cnt)
else:
    hsc.append(cnt)

max_hsc = []

for i, c in enumerate(hsc):
    cnt = 0
    _k = 0
    for _c in hsc[i:]:
        if _c < 0:
            _k += 1
        if _k > k:
            break
        cnt += abs(_c)
    max_hsc.append(cnt)

print(max(max_hsc))


### FIXED CODE ###

# -*- coding: utf-8 -*-
n, k = map(int, input().split())
s = input()

hsc = []
if s[0] == '0':
    hsc.append(0)
pre = s[0]
cnt = 0
for c in s:
    if c == pre:
        cnt += 1
    else:
        hsc.append(cnt)
        pre = c
        cnt = 1
hsc.append(cnt)

_max = sum(hsc[0:2*k+1])
tmp = _max
r = 2 * k + 1
for i in range(2, n, 2):
    l_sum = sum(hsc[i-2:i])
    r_sum = sum(hsc[r:r+2])
    r += 2
    tmp -= l_sum
    tmp += r_sum
    _max = max(_max, tmp)

print(_max)
### BUGGY CODE ###

def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def solve(N, A, L=60, MOD=10**9+7):
    # j桁目のビットが1であるものの数を記憶
    count_zeros = [0 for j in range(L+1)]
    count_ones = [0 for j in range(L+1)]
    for i in range(N):
        for j in range(L):
            count_ones[j] = count_ones[j] + ((A[i] >> j) & 1)
            count_zeros[j] = N - count_ones[j]
    # A[i] と A[j] (i<j) のxorをとる
    modsum = 0
    for j in range(L):
        base = count_zeros[j] * count_ones[j]
        # 64bit以下に収まるように分割。base<<jとしない。
        for k in range(j):
            base = (base<<1) % MOD
        modsum = (base + modsum) % MOD
    return modsum


if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

### FIXED CODE ###

def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def solve(N, A, L=60, MOD=10**9+7):
    modsum = 0
    for j in range(L):
        count_ones = sum([1 for a in A if (a >> j) & 1])
        # count_ones = sum([(A[i] >> j) & 1 for i in range(N)])
        count_zeros = N - count_ones
        # MODは一度にとる
        modsum +=  (1<<j) * count_zeros * count_ones
        modsum %= MOD
    return modsum


if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
sum = 0
t = n-1
for i in a:
    for j in range(t):
        sum += (i * a[-(j+1)]) % (10**9 + 7)
    t -= 1
ans = sum % (10**9 + 7)
print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 0
s = sum(a)
for i in a:
    s -= i
    ans = (ans + (i * s))
ans = ans % mod
print(ans)
### BUGGY CODE ###

import sys
a = sys.stdin.read().split('\n')[:-1]
s = a[0]
q = int(a[1])
f = 1
#for i in range(0, q):
for i in range(2, q + 2):
    if a[i][0] == '1':
        f = 1 - f
    if a[i][0] == '2':
        if a[i][2] == '21'[f]:
            s = a[i][4] + s
        else:
            s += a[i][4]
if f == 1:
    print(s)
else:
    b = ''
    for c in reversed(s):
        b += c
    print(b)

### FIXED CODE ###

import sys
a = sys.stdin.read().split('\n')[:-1]
q = int(a[1])
f = 1
l = [[], []]
for i in range(2, q + 2):
    if a[i][0] == '1':
        f = 1 - f
    else:
        l[0 if a[i][2] == '21'[f] else 1].append(a[i][4])
if f != 1:
    l[1].reverse()
    print(''.join(l[1]) + ''.join(reversed(list(a[0]))) + ''.join(l[0]))
else:
    l[0].reverse()
    print(''.join(l[0]) + a[0] + ''.join(l[1]))
### BUGGY CODE ###

from pprint import pprint
import sys
 
n = int(sys.stdin.readline().strip())
a = map(int, sys.stdin.readline().strip().split(" "))

zero_count = [0] * 60
for a_i in a:
    binary = format(a_i, '060b')
    # print(format(a_i, '030b'))
    for i, bit in enumerate(binary):
        zero_count[i] += int(bit)

# pprint(zero_count)

ans = 0
for i, count in enumerate(zero_count[::-1]):
    # print(i, count)
    ans += ((2 ** i) % (10**9 + 7)) * (count * (n - count))
    ans %= 10**9 + 7 
    # print(ans)

print(ans)

### FIXED CODE ###

import sys
from pprint import pprint

mod = 10**9 + 7

def pow_r(x, n):
    """
    O(log n)
    """
    if n == 0:  # exit case
        return 1
    if n % 2 == 0:
        return pow_r(x ** 2, n // 2) % mod
    else:
        return (x * pow_r(x ** 2, (n - 1) // 2)) % mod


def solve(n, a):

    zero_count = [0] * 60
    for a_i in a:
        # binary = format(a_i, '060b')
        # for j, b_j in enumerate(binary):
        #    zero_count[j] += int(b_j)
        for j in range(61):
            if a_i >> j & 1:
                zero_count[j] += 1

    ans = 0
    # for i, count in enumerate(zero_count[::-1]):
    for i, count in enumerate(zero_count):
        # ans += (2**i) * (count * (n - count)) 
        ans += pow_r(2, i) * (count % mod) * ((n - count) % mod) 
        ans %= mod
    print(ans)

if __name__ == '__main__':
    n = int(sys.stdin.readline().strip())
    a = map(int, sys.stdin.readline().strip().split(" "))
    
    solve(n, a)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
    c = []
    for j in range(60):
        if a[i] % 2**(j+1) == 0:
            c.append(0)
        else:
            c.append(1)
            a[i] -= 2**j
    b.append(c)

ans = 0

for i in range(60):
    c0 = 0
    c1 = 0
    for j in range(n):
        if b[j][i] == 0:
            c0 += 1
        else:
            c1 += 1
    ans += 2 ** i * c0 * c1
    ans %= 10**9 + 7

print(ans)

### FIXED CODE ###

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

ans = 0
for j in range(60):
    c0 = 0
    c1 = 0
    for i in range(n):
        if a[i] % 2**(j+1) == 0:
            c0 += 1
        else:
            a[i] -= 2**j
            c1 += 1
    ans += 2 ** j * c0 * c1
    ans %= 10**9 + 7

print(ans)
### BUGGY CODE ###

import sys
from math import factorial as fac

N, M = map(int, input().split())

factor = {}
tmp = 2
while(M // tmp >= 1):
    if(M % tmp != 0):
        if(tmp == 2):
            tmp += 1
        else:
            tmp += 2
        continue

    M = M // tmp
    factor[tmp] = factor.get(tmp, 0) + 1

if(factor == {}):
    print(1)
    sys.exit()

ans = 1
max_a = 7 + 10 ** 9
tt = fac(N - 1)
# print(factor)
for i in factor.values():
    # print(i)
    tmp = fac(N + i - 1) // (fac(i) * tt)
    ans = (ans * tmp) % max_a
print(ans)

### FIXED CODE ###

import sys

N, M = map(int, input().split())

factor = {}
tmp = 2
while(M // tmp >= 1):
    if(M % tmp != 0):
        if(tmp == 2):
            tmp += 1
        elif(M // tmp < tmp):
            tmp = M
        else:
            tmp += 2
        continue

    M = M // tmp
    factor[tmp] = factor.get(tmp, 0) + 1

if(factor == {}):
    print(1)
    sys.exit()

ans = 1
max_a = 7 + 10 ** 9
max_value = max(list(factor.values()))
aho = {0: 1}
fact = {0: 1}
abc = {}
for i in range(1, max_value + 1):
    # print(i)
    aho[i] = ((aho[i - 1] * (i + N - 1)))
    fact[i] = (fact[i - 1] * i)
    abc[i] = (aho[i] // fact[i]) % max_a


# print(factor)
# print(aho, fact)
for i in factor.values():
    # print(i)
    ans = (ans * abc[i]) % max_a
print(ans)
### BUGGY CODE ###

X,Y,Z,K=map(int,input().split())
A=[int(x) for x in input().split()]
B=[int(x) for x in input().split()]
C=[int(x) for x in input().split()]
A.sort(reverse =True)
B.sort(reverse =True)
C.sort(reverse =True)
answer=[]
for p in range(min(X,K)):
  for q in range(min(Y,K)):
    for r in range(min(Z,K)):
      answer +=[A[p]+B[q]+C[r]]
answer.sort()
for i in range(K):
  print(answer[-i-1])

### FIXED CODE ###

X,Y,Z,K=map(int,input().split())
A=[int(x) for x in input().split()]
B=[int(x) for x in input().split()]
C=[int(x) for x in input().split()]
A.sort(reverse =True)
B.sort(reverse =True)
C.sort(reverse =True)
answer=[]
for p in range(min(X,K)):
  for q in range(min(Y,K)):
    if p*q>K:
      break
    for r in range(min(Z,K)):
      if p*q*r>K:
        break
      answer +=[A[p]+B[q]+C[r]]
answer.sort()
for i in range(K):
  print(answer[-i-1])
### BUGGY CODE ###

def main():
    [N, M] = [int(number) for number in input().split()]
    part_times = []
    for i in range(N):
        job= [int(number) for number in input().split()]
        part_times.append(job)
    part_times = [part_time for part_time in sorted(part_times, key = lambda x: x[1], reverse=True) if part_time[0] <= M]
    money = 0
    #day = 0
    #earns = []
    for day in range(M):
        try:
            todays_part = list(filter(lambda x: x[0] <= day + 1, part_times))[0]
            money += todays_part[1]
            part_times.remove(todays_part)
        except:
            continue

    """
    for [income_day, earn] in part_times:
        remain_day = M - day
        if remain_day >= income_day:
            money += earn
        else:
            min_earn = min(earns)
            if earn > min_earn:
                money += earn
                money -= min_earn
                earns.remove(min_earn)
        earns.append(earn)
        day += 1
    """
    print(money)

if __name__ == '__main__':
    main()


### FIXED CODE ###

import heapq

def main():
    [N, M] = [int(number) for number in input().split()]
    part_times = []
    for i in range(N):
        job= [int(number) for number in input().split()]
        part_times.append(job)
    part_times = [part_time for part_time in sorted(part_times, key = lambda x: x[0])] #if part_time[0] <= M]
    money = 0
    hp = []
    for day in range(M):
        while part_times != [] and part_times[0][0] <= day + 1:
            part_time = part_times[0]
            heapq.heappush(hp, - part_time[1])
            part_times.pop(0)
        if hp != []:
            money -= heapq.heappop(hp)

    print(money)

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

from statistics import median
n=int(input())
l=list(map(int,input().split()))

for i in range(n):
    x=l[:]
    x.pop(i)
    print(median(x))

### FIXED CODE ###

from statistics import median, median_low, median_high

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

m_l=median_low(l)
m_h=median_high(l)

for i in range(n):
    if l[i] <= m_l:
        print(m_h)
    elif l[i] >= m_h:
        print(m_l)
### BUGGY CODE ###

n=int(input())
l=list(set([int(input()) for d in range(n)]))
def gcd(a,s):
  for i in range(min(a,s)+1)[:0:-1]:
    if (a%i,s%i)==(0,0):
      return i

    
a=l[0]
for i in l[1:]:
  g=gcd(a,i)
  a=a*i//g
print(a)

### FIXED CODE ###

n=int(input())
l=list(set([int(input()) for d in range(n)]))  
a=l[0]
def gcd(a,s):
  a,s=min(a,s),max(a,s)
  if a==0:return s
  else:return gcd(s%a,a)

for i in l[1:]:
  a=a*i//gcd(a,i)
print(a)
### BUGGY CODE ###

k = int(input())
i = 1
if k%2 ==1:
    while True:
        s = 0
        for j in range(i):
            s += (10**j)*7
        if s % k == 0:
            print(i)
            break
        i += 1
else:
    print(-1)

### FIXED CODE ###

import sys
k = int(input())
d = 0
for i in range(k+1):
  d = (10*d) + 7
  d %= k
  if d == 0:
    print(i+1)
    sys.exit()
print(-1)
### BUGGY CODE ###

n = int(input())
x = list(map(int,input().split()))
a = sorted(x)
#print(a)
ans1 = a[int(n/2-1)]
ans2 = a[int(n/2)]
#print(ans1,ans2)
if ans1 == ans2:
    for i in range(n):
        print(ans1)
else:
    for i in range(n):
        if a.index(x[i]) < n/2:
            print(ans2)
        else:
            print(ans1)

### FIXED CODE ###

n = int(input())
x = list(map(int,input().split()))
a = sorted(x)
#print(a)
ans1 = a[int(n/2-1)]
ans2 = a[int(n/2)]
#print(ans1,ans2)
if ans1 == ans2:
    for i in range(n):
        print(ans1)
else:
    for i in range(n):
        if x[i] < ans2:
            print(ans2)
        else:
            print(ans1)
### BUGGY CODE ###

import itertools
n = int(input())
a = list(map(int, input().split()))
b = list(enumerate(a))

cnt = 0
for i,j in itertools.combinations(b,2):
	if abs(i[0]-j[0]) == i[1] + j[1]:
		cnt += 1
print (cnt)

### FIXED CODE ###

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

dicplus = {}
dicmins = {}
for i in range(n):
	b = i + a[i]
	c = i - a[i]
	if b not in dicplus:
		dicplus[b] = 1
	else:
		dicplus[b] += 1
	if c not in dicmins:
		dicmins[c] = 1
	else:
		dicmins[c] += 1

ans = 0
for i in dicplus:
	if i in dicmins:
		ans += dicplus[i] * dicmins[i]
print (ans)
### BUGGY CODE ###

import sys
input=sys.stdin.readline
n=int(input())
x=input().rstrip()
p=x.count("1")
numx=0
for i in range(n):
    if x[-i-1]=="1":
        numx+=2**i
if p!=1:
    minus=numx%(p-1)
plus=numx%(p+1)

def popcnt(x):
    cnt=str(bin(x)).count("1")
    x%=cnt
    return x

for i in range(n):
    if x[i]=="0":
        amari=(plus+2**(n-i-1)%(p+1))%(p+1)
        ans=1
        while amari>=1:
            amari=popcnt(amari)
            ans+=1
        print(ans)
    else:
        if p==1:
            print(0)
            continue
        amari=(minus-2**(n-i-1)%(p-1))%(p-1)
        ans=1
        while amari>=1:
            amari=popcnt(amari)
            ans+=1
        print(ans)

### FIXED CODE ###

import sys
input=sys.stdin.readline
n=int(input())
x=input().rstrip()
x10=int(x,2)
pcx=x.count("1")

plus=x10%(pcx+1)
if pcx>=2:
    minus=x10%(pcx-1)

def popcnt(x):
    cnt=1
    while x>=1:
        x%=(bin(x).count("1"))
        cnt+=1
    return cnt

for i in range(n):
    if x[i]=="0":
        amari=(plus+pow(2,n-i-1,pcx+1))%(pcx+1)
        print(popcnt(amari))
    else:
        if pcx==1:
            print(0)
        else:
            amari=(minus-pow(2,n-i-1,pcx-1))%(pcx-1)
            print(popcnt(amari))
### BUGGY CODE ###

N = int(input())

employee = input().split(" ")

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

### FIXED CODE ###

N = int(input())

employees = list(map(int, input().split(" ")))

subordinates = [0] * N

for employee in employees:
    subordinates[employee-1] += 1
    
for i in range(N):
    print(subordinates[i])
### BUGGY CODE ###

import sys
H, W, *A = sys.stdin.read().split()
H = int(H); W = int(W)

INF = 8000
dist = [[INF]*(W) for i in range(H)]
for i in range(H):
    di = dist[i]
    Ai = A[i]
    for j, a in enumerate(Ai):
        if a == '#':
            di[j] = 0
for i in range(H):
    cur = INF
    di = dist[i]
    for j in range(W):
        if di[j] == 0:
            cur = 0
        else:
            di[j] = cur = cur+1
    cur = INF
    for j in range(W-1, -1, -1):
        if di[j] == 0:
            cur = 0
        else:
            di[j] = cur = min(di[j], cur+1)
for j in range(W):
    cur = INF
    for di in dist:
        if cur+1 < di[j]:
            di[j] = cur = cur+1
        else:
            cur = di[j]
    cur = INF
    for di in reversed(dist):
        if cur+1 < di[j]:
            di[j] = cur = cur+1
        else:
            cur = di[j]
sys.stdout.write("%d\n" % max(max(di) for di in dist))

### FIXED CODE ###

from collections import deque

H, W = map(int, input().split())
que = deque()
A = [input() for i in range(H)]

dist = [[-1]*W for i in range(H)]
dd = ((-1, 0), (0, -1), (1, 0), (0, 1))
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            dist[i][j] = 0
            que.append((i, j))

d = 0
while que:
    y, x = que.popleft()
    d = dist[y][x]
    for dx, dy in dd:
        nx = x + dx; ny = y + dy
        if not 0 <= nx < W or not 0 <= ny < H or dist[ny][nx] != -1:
            continue
        dist[ny][nx] = d+1
        que.append((ny, nx))
print(d)