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

from collections import deque
import copy
H,W = map(int, input().split())
field = [[j == "#" for j in input()] for _ in range(H)]
dy = [1,-1,0,0]
dx = [0,0,1,-1]

queue = deque()
# sy,sx = 0,0
# gy,gx = H-1,W-1
queue = deque([(i, j) for i in range(H) for j in range(W) if field[i][j]])
# for i in range(H):
#     for j in range(W):
#         if field[i][j] == True:
#             queue.append([i,j])
# queue = [[sy,sx]]
flag = False
count = 0
while True:
    count += 1
    nqueue = deque()
    while queue:
        h,w = queue.popleft()
        if w >0 and not field[h][w-1]:
            field[h][w-1] = True
            nqueue.append([h,w-1])
        if w <W-1 and not field[h][w+1]:
            field[h][w+1] = True
            nqueue.append([h,w+1])
        if h >0 and not field[h-1][w]:
            field[h-1][w] = True
            nqueue.append([h-1,w])
        if h < H-1 and not field[h+1][w]:
            field[h+1][w] = True
            nqueue.append([h+1,w])
        # for i in range(4):
        #     nh = h + dy[i]
        #     nw = w + dx[i]
        #     if (0<=nh<H) and (0<=nw<W):
        #         if field[nh][nw] == False:
        #             field[nh][nw] = True
        #             nqueue.append((nh,nw))
    if nqueue:
        queue = copy.copy(nqueue)
    else:
        break
print(count-1)
    

### FIXED CODE ###

from collections import deque
import copy

def main():
    h, w = map(int, input().split())
    a = [[j == "#" for j in input()] for _ in range(h)]


    # スタート地点をqueueに追加
    que = deque([(i, j) for i in range(h) for j in range(w) if a[i][j]])

    count = 0
    while True:
        count += 1
        nque = deque()
        while que:
            y, x = que.popleft()
            if x > 0 and not a[y][x-1]:
                a[y][x-1] = True
                nque.append((y,x-1))
            if x < w-1 and not a[y][x+1]:
                a[y][x+1] = True
                nque.append((y,x+1))
            if y > 0 and not a[y-1][x]:
                a[y-1][x] = True
                nque.append((y-1,x))
            if y < h-1 and not a[y+1][x]:
                a[y+1][x] = True
                nque.append((y+1,x))

        if nque:
            que = copy.copy(nque)
        else:
            break

    print(count - 1)

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

import collections
N = int(input())
s = [collections.Counter(input()) for i in range(N)]
ans = 0
for i in range(N-1):
    for j in range(i+1,N):
        c = 1
        for k in s[i].items():
            if k not in s[j].items():
                c = 0
                break
        if c == 1:
            ans += 1
print(ans)

### FIXED CODE ###

N = int(input())
s = [sorted(input()) for i in range(N)]
s.sort()
ans = 0
for i in range(N-1):
    if s[i] == s[i+1]:
        start = i
        end = N-1
        while start != end:
            center = (start + end) // 2
            if s[center] == s[i]:
                if s[center+1] == s[i]:
                    start = center+1
                else:
                    start = end = center
            else:
                end = center
        ans += start - i
print(ans)
### BUGGY CODE ###

INF = 10**10 - 1


#指定数分の３つの整数を順番に受けてリストに入力
# スペース区切りの整数入力
N,Wmax = map(int, input().split())
w = [0]*(N+1)
v = [0]*(N+1)
for i in range(1,N+1):
    w[i],v[i] = map(int, input().split())

#dpの初期化
dp = [[-INF] * (Wmax+1) for i in range(N+1)]
for i in range(0,N+1):
    dp[i][0] = 0
#print(dp[0])

for i in range(1,N+1):
    for ww in range(1,Wmax+1):
        if w[i] > ww:
            dp[i][ww] = dp[i-1][ww]        
        else:
            dp[i][ww] = max(dp[i-1][ww],dp[i-1][ww-w[i]]+v[i])
    #print(dp[i])

Vmax = 0
for ww in range(1,Wmax+1):
    Vmax = max(Vmax,dp[N][ww])
print(Vmax)


### FIXED CODE ###

INF = 10**10 - 1


#指定数分の３つの整数を順番に受けてリストに入力
# スペース区切りの整数入力
N,Wmax = map(int, input().split())
w = [0]*(N+1)
v = [0]*(N+1)
for i in range(1,N+1):
    w[i],v[i] = map(int, input().split())

#dpの初期化
dp = [[-INF] * (Wmax+1) for i in range(N+1)]
for i in range(0,N+1):
    dp[i][0] = 0
#print(dp[0])

for i in range(1,N+1):
    for ww in range(1,Wmax+1):
        dp[i][ww] = dp[i-1][ww]        
        if w[i] > ww:
            continue
        else:
            dp[i][ww] = max(dp[i][ww],dp[i-1][ww-w[i]]+v[i])
    #print(dp[i])

Vmax = 0
for ww in range(1,Wmax+1):
    Vmax = max(Vmax,dp[N][ww])
print(Vmax)
### BUGGY CODE ###

import sys
import math
import heapq
from collections import deque
from itertools import combinations
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res
def gcd(x, y):
    if x < y: x, y = y, x
    div = x % y
    while div != 0:
        x, y = y, div
        div = x % y
    return y

H,W=LI()
MP=[input() for _ in range(H)]
CORD=[(i,j) for i in range(H) for j in range(W)]

max_dist = 0
for st, en in combinations(CORD, 2):
    if MP[st[0]][st[1]] == "." and MP[en[0]][en[1]] == ".":
        QUE=deque()
        visited={ st }
        QUE.append((st[0], st[1], 0))
        res = 0
        while QUE:
            ii, jj, dist = QUE.popleft()
            if (ii, jj) == en:
                res = dist
                # print("st:{} en:{} dist:{}".format(st, en, dist))
                break
            for di, dj in [(-1,0),(1,0),(0,-1),(0,1)]:
                iii,jjj = ii+di, jj+dj
                if iii >= 0 and jjj >= 0 and iii < H and jjj < W and MP[iii][jjj] != '#' and not (iii, jjj) in visited:
                    QUE.append((iii, jjj, dist+1))
                    visited.add((iii, jjj))

        max_dist = max(res, max_dist)

print(max_dist)


### FIXED CODE ###

import sys
import math
import heapq
from collections import deque
from itertools import combinations
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res
def gcd(x, y):
    if x < y: x, y = y, x
    div = x % y
    while div != 0:
        x, y = y, div
        div = x % y
    return y

H,W=LI()
MP=[input() for _ in range(H)]
CORD=[(i,j) for i in range(H) for j in range(W)]

max_dist = 0
for i in range(len(CORD)):
    st = CORD[i]
    if MP[st[0]][st[1]] == ".":
        QUE=deque()
        visited = [[0]*W for i in range(H)]
        visited[st[0]][st[1]] = 1
        QUE.append((st[0], st[1], 0))
        res = 0
        while QUE:
            ii, jj, dist = QUE.popleft()
            max_dist = max(max_dist, dist)
            for iii, jjj in [(ii-1,jj),(ii+1,jj),(ii,jj-1),(ii,jj+1)]:
                if iii >= 0 and jjj >= 0 and iii < H and jjj < W and MP[iii][jjj] != '#' and not visited[iii][jjj]:
                    QUE.append((iii, jjj, dist+1))
                    visited[iii][jjj] = 1


print(max_dist)
### BUGGY CODE ###

N = int(input())
S = input()

total = 0
for i in range(N):
    for j in range(i,N):
        for k in range(j,N):
            chars = set()
            chars.add(S[i])
            chars.add(S[j])
            chars.add(S[k])
            if len(chars) == 3:
                if j - i != k - j:
                    total += 1

print(total)


### FIXED CODE ###

N = int(input())
S = input()

r = 0
g = 0
b = 0

for i in range(N):
    if S[i] == 'R':
        r += 1
    if S[i] == 'G':
        g += 1
    if S[i] == 'B':
        b += 1

rgb_tot = r*g*b
#print(rgb_tot)

for i in range(N):
    first = S[i]
    for j in range(i,N):
        if 2*j - i < N:
            if first != S[j] and first != S[2*j-i] and S[j] != S[2*j-i]:
                #print("%d %d %d" % (i,j,2*j-i))
                #print(rgb_tot)
                rgb_tot -= 1

print(rgb_tot)
### BUGGY CODE ###

n = int(input())
s = input()


nr = 0
ng = 0
nb = 0

for i in range(0, n):
    if s[i] == 'R':
        nr += 1
    elif s[i] == 'G':
        ng += 1
    else:
        nb += 1

c = 0
for i in range(0, n-2):
    for j in range(i+1, n-1):
        k = 2*j - i
        if j < k and k < n:
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                c += 1

print(nr*ng*nb - c)


### FIXED CODE ###

from collections import defaultdict

n = int(input())
s = input()

rgb = {'R': 0, 'G': 0, 'B': 0}
for i in s:
    rgb[i] += 1

c = 0
for i in range(0, n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        k = 2*j - i
        if j < k and k < n and s[i] != s[k] and s[j] != s[k]:
                c += 1

print(rgb['R']*rgb['G']*rgb['B'] - c)
### BUGGY CODE ###

N=int(input())
A=[int(x) for x in input().split()]
for i in range(N):
  print(A.count(i+1))
  A = A[1:]

### FIXED CODE ###

import collections
N=int(input())
A=[x for x in input().split()]
c = collections.Counter(A)
for i in range(N):
	print(c["{}".format(str(i+1))])
### BUGGY CODE ###

import sys
input=sys.stdin.readline
N,M=map(int,input().split())
from collections import defaultdict
OUT=defaultdict(dict)
IN=defaultdict(set)
start={i for i in range(N)}
for i in range(N+M-1):
    a,b=map(int,input().split())
    start-={b-1}
    OUT[a-1][i]=b-1
    IN[b-1]|={i}
P=[0]*N
while len(start)>0:
    now=start.pop()
    for b,nex in OUT[now].items():
        IN[nex].remove(b)
        if not IN[nex]:
            start|={nex}
            P[nex]=now+1
for p in P:
    print(p)

### FIXED CODE ###

import sys
input=sys.stdin.readline
N,M=map(int,input().split())
from collections import defaultdict
OUT=defaultdict(set)
IN=defaultdict(int)
for i in range(N+M-1):
    a,b=map(int,input().split())
    OUT[a-1]|={b-1}
    IN[b-1]+=1
P=[0]*N
start={i for i in range(N) if IN[i]==0}
while len(start)>0:
    now=start.pop()
    for nex in OUT[now]:
        IN[nex]-=1
        if IN[nex]==0:
            start|={nex}
            P[nex]=now+1
[print(p) for p in P]
### BUGGY CODE ###

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))

    for i in range(M):
        b, c = map(int, input().split())
        for j in range(b):
            A.append(c)

    A = sorted(A, reverse=True)
    print(sum(A[:N]))


if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    N, M = map(int, input().split())
    A = []
    for i in list(map(int, input().split())):
        A.append((i, 1))
    for i in range(M):
        b, c = map(int, input().split())
        A.append((c, b))

    A = sorted(A)
    # print(A)
    cnt = 1
    ans = 0
    while cnt <= N:
        b, c = A.pop()
        ans += b
        if c > 1:
            c -= 1
            A.append((b, c))
        cnt += 1
    print(ans)


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

N ,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
inf = 10 **18 +1
ok = inf
ng = -inf

def is_ok(arg):
    ans = 0
    for i in range(N):
        if A[i] * A[i] < arg :
            ans -= 1
        ok = N
        ng = -1
        if A[i] > 0:
            while (abs(ok - ng) > 1):
                mid = (ok + ng) // 2
                if A[i] * A[mid] < arg:
                    ng = mid
                else:
                    ok = mid
            ans += ok
        else: 
            while (abs(ok - ng) > 1):
                mid = (ok + ng) // 2
                if A[i] * A[mid] < arg:
                    ok = mid
                else:
                    ng = mid       
            ans += N-ok   
    ans /= 2
    return K > ans

while (abs(ok - ng) > 1):
    mid = (ok + ng) // 2
    if is_ok(mid):
        ng = mid
    else:
        ok = mid
print(ng)


### FIXED CODE ###

N ,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
inf = max(A[0]**2,A[-1]**2)
ok1 = inf
ng1 = -inf
while (abs(ok1 - ng1) > 1):
    arg = (ok1 + ng1) // 2
    ans = 0
    for i in range(N):
        if A[i] * A[i] < arg :
            ans -= 1
        ok = N
        ng = -1
        if A[i] == 0:
            if arg > 0:
                ans += N
        elif A[i] > 0:
            while (abs(ok - ng) > 1):
                mid = (ok + ng) // 2
                if A[i] * A[mid] < arg:
                    ng = mid
                else:
                    ok = mid
            ans += ok
        else: 
            while (abs(ok - ng) > 1):
                mid = (ok + ng) // 2
                if A[i] * A[mid] < arg:
                    ok = mid
                else:
                    ng = mid       
            ans += N-ok   
    ans /= 2
    if K>ans:
        ng1 = arg
    else:
        ok1 = arg
print(ng1)
### BUGGY CODE ###

#A=int(input())
#A=list(map(int,(input().split())))
N,M=list(map(int,(input().split())))
A=[]
B=[]
C=[]
for x in range(M):
	te,mp=list(map(int,(input().split())))
	A.append(te)
	B.append(mp)
	temp=list(map(int,(input().split())))
	c=0
	for x in temp:
		c+=2**(x-1)
	C.append(c)
dp=[]
for x in range(M+1):
	dp.append([-1]*(2**N))
dp[0][0]=0

for i in range(M):
	for j in range(2**N):
		if (dp[i][j]>=0):
			#買わなかった場合
			if (dp[i+1][j]==-1):
				dp[i+1][j]=dp[i][j]
			else:
				dp[i+1][j]=min(dp[i+1][j],dp[i][j])
			#買った場合
			if (dp[i+1][j|C[i]]==-1):
				dp[i+1][j|C[i]]=dp[i][j]+A[i]
			else:
				dp[i+1][j|C[i]]=min(dp[i+1][j|C[i]],dp[i][j]+A[i])
"""
for x in dp:
	print(x)
print("\n",C)"""
print(dp[M][2**N-1])

### FIXED CODE ###

#A=int(input())
#A=list(map(int,(input().split())))
N,M=list(map(int,(input().split())))
A=[]
B=[]
C=[]
for x in range(M):
	te,mp=list(map(int,(input().split())))
	A.append(te)
	B.append(mp)
	temp=list(map(int,(input().split())))
	c=0
	for x in temp:
		c+=2**(x-1)
	C.append(c)
dp=[]
for x in range(M+1):
	dp.append([-1]*(2**N))
dp[0][0]=0

for i in range(M):
	for j in range(2**N):
		if (dp[i][j]>=0):
			#買わなかった場合
			if (dp[i+1][j]==-1):
				dp[i+1][j]=dp[i][j]
			else:
				dp[i+1][j]=min(dp[i+1][j],dp[i][j])
			#買った場合
			if (dp[i+1][j|C[i]]==-1):
				dp[i+1][j|C[i]]=dp[i][j]+A[i]
			else:
				dp[i+1][j|C[i]]=min(dp[i+1][j|C[i]],dp[i][j]+A[i])
"""
for x in dp:
	print(x)
print("\n",C)"""
print(dp[M][2**N-1])
### BUGGY CODE ###

def euqlid(x,y):
    if x < y:
        tmp = x
        x  = y
        y = tmp
    while x % y != 0:
        tmp = x % y
        x = y
        y = tmp
    return y
a, b = map(int, input().split())
k = euqlid(a, b)
data = []
i = 2
while k > 1:
    while k % i == 0:
        k = k // i
        data.append(i)
    i += 1
data = set(data)
print(len(data) + 1)

### FIXED CODE ###

import math
def euqlid(x,y):
    while x % y != 0:
        tmp = x % y
        x = y
        y = tmp
    return y
a, b = map(int, input().split())
if a < b:
    tmp = a
    a = b
    b = tmp
k = euqlid(a, b)
sum = 0
i = 2
if k == 1:
    print(1)
    exit()
while i <= math.sqrt(k):
    flag = False
    while k % i == 0:
        k = k // i
        flag = True
    if flag:
        sum += 1
    i += 1
if i <= k:
    sum += 1
print(sum + 1)
### BUGGY CODE ###

n,m = map(int,input().split())
data = [tuple(map(int,input().split())) for i in range(n)]
data.sort(key = lambda tup:tup[0])
salary =[]
ans=0
prev=0
memo=0
for day in range(m):
	for i in range(memo,n):
		if data[memo][0]<=(day+1):
			salary.append(data[memo][1])
			memo+=1
		else:
			break

	# ans+=salary.pop(max)

	ans+=max(salary ,default=0)
	if max(salary ,default=0) !=0:
		del salary[salary.index(max(salary))]
print(ans)	

### FIXED CODE ###

import heapq
n,m = map(int,input().split())
AB =[tuple(map(int,input().split())) for i in range(n)]
ans,memo=0,0
hq=[]
AB.sort(key=lambda tup:tup[0])
for day in range(m):
	for i in range(memo,n):
		if AB[i][0]<=(day+1):
			heapq.heappush(hq,-AB[i][1])
			memo+=1
		else:
			break
	if len(hq):	
		ans += -heapq.heappop(hq)
print(ans)
### BUGGY CODE ###

h,w = map(int,input().split())
u = [list(input()) for _ in range(h)]
from collections import deque
q = deque([])
dist = [[-1 for i in range(w)] for j in range(h)]
for i in range(h):
    for j in range(w):
        if u[i][j]=='#':
            q.append((i,j))
            dist[i][j]=0

while q:
    (i,j) = q.popleft()
    if 0<=i-1<h:
        if u[i-1][j]=='.' and dist[i-1][j]==-1:
            u[i-1][j]='#'
            q.append((i-1,j))
            dist[i-1][j]=dist[i][j]+1
    if 0<=i+1<h:
        if u[i+1][j]=='.' and dist[i+1][j]==-1:
            u[i+1][j]='#'
            q.append((i+1,j))
            dist[i+1][j]=dist[i][j]+1
    if 0<=j-1<w:
        if u[i][j-1]=='.' and dist[i][j-1]==-1:
            u[i][j-1]='#'
            q.append((i,j-1))
            dist[i][j-1]=dist[i][j]+1
    if 0<=j+1<w:
        if u[i][j+1]=='.' and dist[i][j+1]==-1:
            u[i][j+1]='#'
            q.append((i,j+1))
            dist[i][j+1]=dist[i][j]+1

z=[]
for b in dist:
    z.append(max(b))

print(max(z))

### FIXED CODE ###

h,w = map(int,input().split())
u = [list(input()) for _ in range(h)]
from collections import deque
q = deque([])
dist = [[-1 for i in range(w)] for j in range(h)]
for i in range(h):
    for j in range(w):
        if u[i][j]=='#':
            q.append((i,j))
            dist[i][j]=0

while q:
    (i,j) = q.popleft()
    if 0<=i-1<h:
        if u[i-1][j]=='.' and dist[i-1][j]==-1:
            u[i-1][j]='#'
            q.append((i-1,j))
            dist[i-1][j]=dist[i][j]+1
    if 0<=i+1<h:
        if u[i+1][j]=='.' and dist[i+1][j]==-1:
            u[i+1][j]='#'
            q.append((i+1,j))
            dist[i+1][j]=dist[i][j]+1
    if 0<=j-1<w:
        if u[i][j-1]=='.' and dist[i][j-1]==-1:
            u[i][j-1]='#'
            q.append((i,j-1))
            dist[i][j-1]=dist[i][j]+1
    if 0<=j+1<w:
        if u[i][j+1]=='.' and dist[i][j+1]==-1:
            u[i][j+1]='#'
            q.append((i,j+1))
            dist[i][j+1]=dist[i][j]+1

z=[]
for b in dist:
    z.append(max(b))

print(max(z))
### BUGGY CODE ###

import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter
# a.sort(key=itemgetter(i)) # i番目要素でsort
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7


def inp():
    '''
    一つの整数
    '''
    return int(input())


def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


def str_inp():
    '''
    文字列をリストとして読み込む
    '''
    return list(input()[:-1])


n = inp()
a_origin = sorted(inpl())
ans = 0
count = collections.defaultdict(int)
a = []
for i in range(n):
    count[a_origin[i]] += 1
    if count[a_origin[i]] < 3:
        a.append(a_origin[i])
n = len(a)


class Cell:
    def __init__(self, value):
        self.value = value
        self.prev = None
        self.next = None


prev = Cell(a[0])
now = prev
for i in range(1, n):
    new = Cell(a[i])
    prev.next = new
    new.prev = prev
    prev = prev.next
while now:
    if not now.next:
        ans += 1
        break
    target = now.next
    while target:
        if target.value == now.value:
            ans -= 1
            if target.next:
                target.next.prev = target.prev
                target.prev.next = target.next
            else:
                target.prev.next = None
        elif target.value % now.value == 0:
            if target.next:
                target.next.prev = target.prev
                target.prev.next = target.next
            else:
                target.prev.next = None
        target = target.next
    ans += 1
    now = now.next
print(ans)


### FIXED CODE ###

import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter
# a.sort(key=itemgetter(i)) # i番目要素でsort
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7


def inp():
    '''
    一つの整数
    '''
    return int(input())


def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


def str_inp():
    '''
    文字列をリストとして読み込む
    '''
    return list(input()[:-1])


n = inp()
a = sorted(inpl())
cnt = collections.Counter(a)
ans = 0
dp = [0] * (10**6 + 1)
for i in a:
    if dp[i] == 0:
        for j in range(1, 10**7):
            if i * j > 10**6:
                break
            dp[i * j] = 1
        if cnt[i] == 1:
            ans += 1
print(ans)
### BUGGY CODE ###

from math import ceil,log
N=int(input())
A=[int(i) for i in input().split()]
def f(A):
    dp=[[0]*16 for i in range(N)]
    for i in range(16):
        dp[N-1][i]=i
    for n in range(N-2,-1,-1):
        nx=ceil(log(A[n]/A[n+1])/log(4)) #A
        for x in range(16):
            dx=nx+x
            if dx <=15:
                #print(max(0,dx))
                dp[n][x]=dp[n+1][max(0,dx)]+x
            else:
                dp[n][x]=dp[n+1][15]+x+(N-1-n)*(dx-15)
    L=[0]*N
    for i in range(N):
        L[i]=dp[i][0]
    return L

K=f(A)+[0]
L=[0]+f(A[::-1])[::-1]
ans=10**9
for i in range(N):
    ans=min(ans,i+2*L[i]+2*K[i])
print(ans)

### FIXED CODE ###

from math import ceil,log
N=int(input())
A=[int(i) for i in input().split()]
def f(A):
    dp=[0]*16
    L=[0]*N
    for i in range(16):
        dp[i]=i
    for n in range(N-2,-1,-1):
        ndp=[0]*16
        nx=ceil(log(A[n]/A[n+1])/log(4)) #A
        for x in range(16):
            dx=nx+x
            if dx <=15:
                #print(max(0,dx))
                ndp[x]=dp[max(0,dx)]+x
            else:
                ndp[x]=dp[15]+x+(N-1-n)*(dx-15)
        dp=ndp
        L[n]=dp[0]
    return L

K=f(A)+[0]
L=[0]+f(A[::-1])[::-1]
ans=10**22
for i in range(N+1):
    ans=min(ans,i+2*L[i]+2*K[i])
print(ans)
### BUGGY CODE ###

N = int(input())
A = [int(input()) for _ in range(N)]
for i in range(N):
  a = A[:i] + A[i+1:]
  print(max(a))

### FIXED CODE ###

N = int(input())
A = [int(input()) for _ in range(N)]
a = sorted(set(A))
max_count = A.count(a[-1])

for i in range(N):
  if len(a) > 1:
    if A[i] != a[-1]:
      print(a[-1])
    else:
      if max_count > 1:
        print(a[-1])
      else:
        print(a[-2])
  else:
    print(a[-1])
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
ans = []
for i in a:
    ans = ans + [i]
    ans = ans[::-1]
print(*ans,sep=" ")

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
if n % 2 == 0:
    a = a[::-2]+a[0::2]
else: a = a[::-2] +a[1::2]
print(*a,sep=" ")
### BUGGY CODE ###

N = int(input())
s = input()
l = set()
for i1 in range(N):
  for i2 in range(i1+1, N):
    for i3 in range(i2+1, N):
      l.add(s[i1] + s[i2] + s[i3])
print(len(l))

### FIXED CODE ###

N = int(input())
s = input()
l = {}
a = [{} for i in range(N)]
a[-1] = {s[-1]: N-1}

for i in range(N-2, -1 , -1):
  a[i] = a[i+1].copy()
  a[i].update({s[i]: i})

r = 0
for k1, v1 in a[0].items():
  if v1 >= N - 2: continue
  for k2, v2 in a[v1+1].items():
    if v2 >= N - 1 : continue
    r += len(a[v2+1].keys())

print(r)
### BUGGY CODE ###

n, x = map(int, input().split())
a = list(map(int, input().split()))
count = 0
for i in range(n-1):
    while a[i] + a[i+1] > x:
        a[i+1] -= 1
        count += 1
print(count)

### FIXED CODE ###

n, x = map(int, input().split())
a = list(map(int, input().split()))
count = 0
for i in range(n-1):
    if a[i] + a[i+1] > x:
        y = a[i] + a[i+1] - x
        count += y
        a[i+1] = max(0, a[i+1]-y)
print(count)