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

n = int(input())
s = str(input())
r = []
g = []
b = []
ans = 0
for i in range(n):
    if s[i] == "R":
        r.append(i+1)
    if s[i] == "G":
        g.append(i+1)
    if s[i] == "B":
        b.append(i+1)
for i in range(len(r)):
    for j in range(len(g)):
        ans += len(b)
        if max(r[i],g[j])+(abs(r[i]-g[j])) in b:
            ans -= 1
        if min(r[i],g[j])-(abs(r[i]-g[j])) in b:
            ans -= 1
        if abs(r[i]-g[j])%2 == 0 and (r[i]+g[j])//2 in b:
            ans -= 1
print(ans)

### FIXED CODE ###

n = int(input())
s = str(input())
r = []
g = []
def right(r,g):
    return (max(r,g)+(abs(r-g)))
def left(r,g):
    return (min(r,g)-(abs(r-g)))
def half(r,g):
    return ((r+g)//2)
for i in range(n):
    if s[i] == "R":
        r.append(i)
    elif s[i] == "G":
        g.append(i)
ans = len(r)*len(g)*(n-(len(r)+len(g)))
for i in range(len(r)):
    for j in range(len(g)):
        t = left(r[i],g[j])
        f = right(r[i],g[j])
        h = half(r[i],g[j])
        if f < n:
            if s[f] == "B":
                ans -= 1
        if 0 <= t:
            if s[t] == "B":
                ans -= 1
        if abs(r[i]-g[j])%2 == 0:
            if s[h] == "B":
                ans -= 1
print(ans)
### BUGGY CODE ###

N = int(input())

def luca(N):
  if N>=2:
    return luca(N-2)+luca(N-1)
  if N==1:
    return 1
  if N==0:
    return 2
  
print(luca(N))


### FIXED CODE ###

from functools import lru_cache

N = int(input())

@lru_cache(maxsize=None)
def luca(N):
  if N>=2:
    return luca(N-2)+luca(N-1)
  if N==1:
    return 1
  if N==0:
    return 2
  
print(luca(N))
### BUGGY CODE ###

A, B = map(int, input().split())
arr = []
#for i in range(A, B+1):
#    arr.append(int(format(i, 'b')))
for i in range(A,B+1):
    arr.append(i)
for i in range(len(arr)):
    if i==0:
        ans = arr[i]
    else:
        ans = ans ^ arr[i]
print(ans)

### FIXED CODE ###

A, B = map(int, input().split())
def f(x):
    if x % 2 == 1:
        tmp = (x + 1) / 2
        if tmp % 2 == 0:
            return 0
        else:
            return 1
    else:
        return f(x-1) ^ x
print(f(A-1)^f(B))
### BUGGY CODE ###

N = int(input())
A_list = list(map(int, input().split()))
ans = 1
for i in range(N):
    ans *= A_list[i]
if ans > 10**18:
    ans=-1

print(ans)

### FIXED CODE ###

N = int(input())
A_list = list(map(int, input().split()))
ans = 1
if 0 in A_list:
    ans = 0
else:
    for A in A_list:
        ans *= A
        if ans > 10**18:
            ans=-1
            break

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

#!/usr/bin/env python3
s = input()
t = input()
ls = len(s)
lt = len(t)
dp = [[0]*(ls+1) for _ in range(lt+1)]

for i,T in enumerate(t):
    for j,S in enumerate(s):
        if T == S:
            dp[i+1][j+1] = dp[i][j] + 1
        else:
            dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j])

ans = ""
ti = lt
sj = ls
while ti > 0 and sj > 0:
    if dp[ti][sj] == dp[ti-1][sj]:
        ti -= 1
    elif dp[ti][sj] == dp[ti][sj-1]:
        sj -= 1
    elif dp[ti][sj] == dp[ti-1][sj-1] + 1:
        ans += t[ti-1]
        ti -= 1
        sj -= 1
print(ans[::-1])


### FIXED CODE ###

#!/usr/bin/env python3
s = input()
t = input()
ls = len(s)
lt = len(t)
dp = [[0 for _ in range(ls+1)] for _ in range(lt+1)]

for i in range(lt):
    for j in range(ls):
        if t[i] == s[j]:
            dp[i+1][j+1] = dp[i][j] + 1
        else:
            dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j])

ans = ""
ti = lt
sj = ls
while ti > 0 and sj > 0:
    if dp[ti][sj] == dp[ti-1][sj]:
        ti -= 1
    elif dp[ti][sj] == dp[ti][sj-1]:
        sj -= 1
    elif dp[ti][sj] == dp[ti-1][sj-1] + 1:
        ans += t[ti-1]
        ti -= 1
        sj -= 1
print(ans[::-1])
### BUGGY CODE ###

import bisect
from collections import deque
class UnionFind(): #Union-Find木の実装
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
same = set()
xseq = []
yseq = []
edge = []
uf = UnionFind(N)
ans = 0

x,y = (int(x) for x in input().split())
same.add((x,y))
xseq.append((x,0))
yseq.append((y,0))
for i in range(1,N):
    x,y = (int(x) for x in input().split())
    if (x,y) not in same:
        same.add((x,y))
        xb = bisect.bisect_left(xseq,(x,i))
        xseq.insert(xb,(x,i))
        if xb == 0:
            edge.append((xseq[1][0]-x,(xseq[1][1],i)))
        elif xb == len(xseq)-1:
            edge.append((x-xseq[-2][0],(xseq[-2][1],i)))
        else:
            edge.append((xseq[xb+1][0]-x,(xseq[xb+1][1],i)))
            edge.append((x-xseq[xb-1][0],(xseq[xb-1][1],i)))
        yb = bisect.bisect_left(yseq,(y,i))
        yseq.insert(yb,(y,i))
        if yb == 0:
            edge.append((yseq[1][0]-y,(yseq[1][1],i)))
        elif yb == len(yseq)-1:
            edge.append((y-yseq[-2][0],(yseq[-2][1],i)))
        else:
            edge.append((yseq[yb+1][0]-y,(yseq[yb+1][1],i)))
            edge.append((y-yseq[yb-1][0],(yseq[yb-1][1],i)))
edge.sort()
d = deque(edge)
for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
    if uf.parents.count(-1*N) == 1:
        break
print(ans)

### FIXED CODE ###

from collections import deque
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return 0
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N = int(input())
xseq = []
yseq = []
uf = UnionFind(N)
ans = 0

for i in range(N):
    x,y = (int(x) for x in input().split())
    xseq.append((x,i))
    yseq.append((y,i))
xseq.sort()
yseq.sort()
xdiff = [(xseq[i][0]-xseq[i-1][0],(xseq[i][1],xseq[i-1][1])) for i in range(1,N)]
ydiff = [(yseq[i][0]-yseq[i-1][0],(yseq[i][1],yseq[i-1][1])) for i in range(1,N)]
diff = xdiff + ydiff
diff.sort()
d = deque(diff)

for i in range(len(d)):
    temp = d.popleft()
    if uf.find(temp[1][0]) != uf.find(temp[1][1]):
        uf.union(temp[1][0],temp[1][1])
        ans += temp[0]
print(ans)
### BUGGY CODE ###

n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]

MOD = 10**9 +7
dp = [0] * (1<<n)

pair_dic = {}
for i in range(n+1):
    pair_dic[i] = []

for i in range(1<<n):
    pair_dic[ bin(i).count('1') ].append(i)

dp[0] = 1
# for i in range(n):
#     for j in pair_dic[i]:
#         for k in range(n):
for i in range(n):
    for k in range(n):
        for j in pair_dic[i]:
            if( (a[i][k] == 1) & ( ((j&(1<<k)) >>k == 0 ) )):
                dp[j + (1<<k)] += dp[j]
                dp[j + (1<<k)] = dp[j + (1<<k)] % MOD


print(dp[-1])

### FIXED CODE ###

def main():

    n = int(input())
    a = [list(map(int, input().split())) for _ in range(n)]

    MOD = 10**9 +7
    dp = [0] * (1<<n)

    pair_dic = {}
    for i in range(n+1):
        pair_dic[i] = []

    for i in range(1<<n):
        pair_dic[ bin(i).count('1') ].append(i)

    dp[0] = 1

    for i in range(n):
        for k in range(n):
            if(a[i-1][k] == 0):
                continue
            for j in pair_dic[i]:
                if(  ((j&(1<<k)) >>k == 0 ) ):
                    dp[j + (1<<k)] += dp[j]
                    dp[j + (1<<k)] = dp[j + (1<<k)] % MOD

    print(dp[-1])

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

import sys
input = sys.stdin.readline
N,Q = map(int,input().split())
C = list(map(int,input().split()))
LR = [tuple(map(int,input().split())) for i in range(Q)]

class SegTree:
    def __init__(self,arr,func,unit):
        self.func = func
        self.unit = unit
        n = 1
        while n < len(arr):
            n *= 2
        self.n = n
        nodes = [0 for _ in range(2*n-1)]
        nodes[n-1:n-1+len(arr)] = [1<<a for a in arr]
        for i in range(n-2,-1,-1):
            nodes[i] = func(nodes[2*i+1],nodes[2*i+2])
        self.nodes = nodes

    def update(self,i,val):
        i += self.n-1
        self.nodes[i] = val
        while i >= 0:
            i = (i - 1) // 2
            self.nodes[i] = self.func(self.nodes[2*i+1], self.nodes[2*i+2])

    def query(self,l,r):
        l += self.n
        r += self.n
        s = self.unit
        while l < r:
            if r & 1:
                r -= 1
                s = self.func(self.nodes[r-1], s)
            if l & 1:
                s = self.func(s, self.nodes[l-1])
                l += 1
            l >>= 1
            r >>= 1
        return s

#https://qiita.com/zawawahoge/items/8bbd4c2319e7f7746266
def popcount(x):
    '''xの立っているビット数をカウントする関数
    (xは64bit整数)'''

    # 2bitごとの組に分け、立っているビット数を2bitで表現する
    x = x - ((x >> 1) & 0x5555555555555555)

    # 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
    x = x + (x >> 8) # 16bitごと
    x = x + (x >> 16) # 32bitごと
    x = x + (x >> 32) # 64bitごと = 全部の合計
    return x & 0x0000007f


segt = SegTree(C, lambda x,y:x|y, 0)
ans = []
for l,r in LR:
    ans.append(popcount(segt.query(l-1,r)))
print(*ans, sep='\n')

### FIXED CODE ###

import sys
input = sys.stdin.readline
N,Q = map(int,input().split())
C = list(map(int,input().split()))
LR = [tuple(map(int,input().split())) for i in range(Q)]

qs = [[] for _ in range(N)]
for i,(l,r) in enumerate(LR):
    qs[l-1].append((i,r-1))

from collections import defaultdict
hist = defaultdict(lambda: -1)
nx = [-1] * N
for i in range(N-1,-1,-1):
    nx[i] = hist[C[i]]
    hist[C[i]] = i

class BinaryIndexedTree:
    def __init__(self,size):
        self.N = size
        self.bit = [0]*(size+1)
    def add(self,x,w): # 0-indexed
        x += 1
        while x <= self.N:
            self.bit[x] += w
            x += (x & -x)
    def _sum(self,x): # 1-indexed
        ret = 0
        while x > 0:
            ret += self.bit[x]
            x -= (x & -x)
        return ret
    def sum(self,l,r): # [l,r)
        return self._sum(r) - self._sum(l)
    def __str__(self): # for debug
        arr = [self.sum(i,i+1) for i in range(self.N)]
        return str(arr)
bit = BinaryIndexedTree(N)

ans = [None] * Q
for l in range(N-1,-1,-1):
    if nx[l] != -1:
        bit.add(nx[l], -1)
    for i,r in qs[l]:
        ans[i] = r-l+1 + bit.sum(l,r+1)

print(*ans, sep='\n')
### BUGGY CODE ###

from operator import mul
from functools import reduce

N = int(input())
S = input()
MOD = 998244353

# make A, B, C oredesr
r, g, b = 0, 0, 0
T = {'R': 0, 'G': 0, 'B': 0}
ABC = []  # means, A: 0, B: 1, C: 2
for s in S:
    T[s] += 1
    ge = [t >= T[s] for t in T.values()].count(True)
    lt = [t < T[s] for t in T.values()].count(True)
    if ge == 1:
        ABC.append(0)
    elif lt == 0:
        ABC.append(2)
    else:
        ABC.append(1)

la, rc = 0, 0  # selectable num of left A and right C
ans = 1

# from left for A
for d in ABC:
    if d == 0:
        la += 1
    elif d == 1:
        ans *= la
        ans %= MOD
        la -= 1

# from right for C
for d in ABC[::-1]:
    if d == 2:
        rc += 1
    elif d == 1:
        ans *= rc
        ans %= MOD
        rc -= 1

# human permutation
ans *= reduce(mul, range(1, N+1))
ans %= MOD
print(ans)


### FIXED CODE ###

N = int(input())
S = input()
MOD = 998244353

# make A, B, C oredesr
r, g, b = 0, 0, 0
T = {'R': 0, 'G': 0, 'B': 0}
ABC = []  # means, A: 0, B: 1, C: 2
for s in S:
    T[s] += 1
    ge = [t >= T[s] for t in T.values()].count(True)
    lt = [t < T[s] for t in T.values()].count(True)
    if ge == 1:
        ABC.append(0)
    elif lt == 0:
        ABC.append(2)
    else:
        ABC.append(1)

la, rc = 0, 0  # selectable num of left A and right C
ans = 1

# from left for A
for d in ABC:
    if d == 0:
        la += 1
    elif d == 1:
        ans *= la
        ans %= MOD
        la -= 1

# # from right for C
for d in ABC[::-1]:
    if d == 2:
        rc += 1
    elif d == 1:
        ans *= rc
        ans %= MOD
        rc -= 1

# human permutation
for i in range(1, N+1):
    ans *= i
    ans %= MOD
print(ans)
### BUGGY CODE ###

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

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


### FIXED CODE ###

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

L = A[:]
m1 = max(A)
m1i = A.index(m1)
A.pop(m1i)
m2 = max(A)
m2i = A.index(m2)
A.pop(m2i)

for i in range(N):
    if (L[i] == m1):
        print(m2)
    else:
        print(m1)
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
n = len(A)
z = 10 ** 9 + 7
ans = 0
x = 0
y = 0
for i in range(n - 1):
    B = A[i + 1:n]
    x = sum(B) % z
    y = (A[i] * x) % z
    ans = (ans + y) % z

print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
n = len(A)
z = 10 ** 9 + 7
ans = 0

x = sum(A)
x = x ** 2
for i in A:
    y = i * i
    x -= y

x = x // 2
x = x % z
print(x)
### BUGGY CODE ###

# -*- coding: utf-8 -*-

S = input()

MOD =[1,0,0,0,0,0,0,0,0,0,0,0,0]
NMOD =[0,0,0,0,0,0,0,0,0,0,0,0,0]

R10 = range(10)
R13 = range(13)

for i,D in enumerate(S):
    C = -1
    if D == '?':
        for N in R10:
            for M in R13:
                NM = (M * 10 + N) % 13
                NMOD[NM] += MOD[M]
            #end for
        #end for
    else:
        C = int(D)
        for M in R13:
            NM = (M * 10 + C) % 13
            NMOD[NM] += MOD[M]
        #end for
    #end if
    for M in R13:
        MOD[M] = NMOD[M]%1000000007
        NMOD[M] = 0
    #end for
#end for

print(MOD[5])


### FIXED CODE ###

# -*- coding: utf-8 -*-

R10 = range(10)
R13 = range(13)

S = input()
R = [[(M-N)*4 % 13 for N in R10] for M in R13]
AM = 10**9+7

MOD =[1]+[0]*12

for D in S:
    if D == '?':
        MOD = [sum([MOD[N] for N in M])%AM for M in R]
    else:
        C = int(D)
        MOD = [MOD[M[C]] for M in R]
    #end if
#end for

print(MOD[5])
### BUGGY CODE ###

n = int(input())

if n % 2 == 1 or n==0:
  print(0)
else:
  total = 1
  cnt = 0
  for i in range(n, 0, -2):
    total *= i
    if str(total)[-1] == '0':
      cnt += 1
      total = total // 10    
  print(cnt)

### FIXED CODE ###

n = int(input())

if n % 2 == 1 or n==0:
  print(0)
else:
  total = 0
  div = 5
  while True:
    total += n // (div * 2)
    div *= 5
    if div > n:
      break
  print(total)
### BUGGY CODE ###


from bisect import bisect_right
N = int(input())
L = sorted(list(map(int,input().split())))
count = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        ma = bisect_right(L[j+1:], L[i] + L[j] -1)
        if ma >= 0:
            count += ma
print(count)


### FIXED CODE ###


import bisect
N = int(input())
L = list(map(int,input().split()))
count = 0
L.sort()
for i in range(N-2):
    for j in range(i+1,N-1):
        count += bisect.bisect(L,L[i]+L[j]-1)-j-1
print(count)
### BUGGY CODE ###

h, w, d = [int(i) for i in input().split()]
a = [[int(j)-1 for j in input().split()] for i in range(h)]

q = int(input())#moving num
l = [0 for i in range(q)]#start number
r = [0 for i in range(q)]#finish number

for i in range(q):
    tmp = input().split()
    l[i]=int(tmp[0])-1
    r[i]=int(tmp[1])-1
loc = [[0, 0] for i in range(h*w)]
for i in range(h):
    for j in range(w):
        loc[a[i][j]][0] = i
        loc[a[i][j]][1] = j
#print(loc)
for i in range(q):
    cost = 0
    for j in range(int((r[i]-l[i])/d)):
        #print("{}->{}:{}+{}".format(l[i]+j*d,l[i]+(j+1)*d,
            #abs(loc[l[i]+(j+1)*d][0]-loc[l[i]+j*d][0]),
            #abs(loc[l[i]+(j+1)*d][1]-loc[l[i]+j*d][1])))
        cost += abs(loc[l[i]+(j+1)*d][0]-loc[l[i]+j*d][0])
        cost += abs(loc[l[i]+(j+1)*d][1]-loc[l[i]+j*d][1])
    print(cost)



### FIXED CODE ###

import math
def func(h, w, d, a, query):
    imos = [0 for _ in range(h*w)]
    pos = [[0, 0] for _ in range(h*w)]
    for i in range(h):
        for j in range(w):
            pos[a[i][j]] = [i, j]
    for i in range(d):
        for j in range(math.ceil((h*w-i)/d)):
            if j == 0:
                continue
            else:
                imos[i + d * j] = imos[i + d * (j-1)]\
                                  + abs(pos[i + d * j][0] - pos[i + d * (j-1)][0])\
                                  + abs(pos[i + d * j][1] - pos[i + d * (j-1)][1])
    for i in range(len(query)):
        print(imos[query[i][1]] - imos[query[i][0]])
    return
h, w, d = [int(i) for i in input().split()]
a = [[0 for _ in range(w)] for _ in range(h)]
for i in range(h):
    a[i] = [int(i) - 1 for i in input().split()]
q = int(input())
query = [[0, 0] for _ in range(q)]
for i in range(q):
    query[i] = [int(i) - 1 for i in input().split()]
func(h, w, d, a, query)
"""
func(3, 3, 2, [[0, 3, 2], [1, 4, 6], [7, 8, 5]], [[3, 7]])
print("ans: 5\n")
func(4, 2, 3, [[2, 6], [0, 3], [4, 1], [5, 7]], [[1, 1], [1, 1]])
print("ans: 0, 0\n")
func(5, 5, 4, [[12, 24, 6, 14, 16], [15, 21, 19, 1, 8], [13, 10, 11, 0, 18], [9, 5, 22, 7, 17], [2, 20, 4, 23, 3]], [[12, 12], [1, 9], [12, 12]])
print("ans: 0, 5, 0")
"""
# print("func({}, {}, {}, {}, {})".format(h, w, d, a, query))
### BUGGY 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)

### 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)
### BUGGY CODE ###

import sys
max_num = 1000000007
input = sys.stdin.readline
N, M = map(int, input().split())
A = list([int(input()) for i in range(M)])

current_step, prev_step, two_prev_step = 0, 1, 0
for i in sorted(range(N), reverse=True):
    if i in A:
        prev_step, two_prev_step = 0, prev_step
        continue

    current_step = prev_step + two_prev_step
    prev_step, two_prev_step = current_step, prev_step

print(current_step % max_num)

### FIXED CODE ###

import sys
max_num = 1000000007
input = sys.stdin.readline
N, M = map(int, input().split())
A = [int(input()) for i in range(M)]

stairs = [1] * (N+1)
for i in A:
    stairs[i] = 0

for i in range(1, N):
    if stairs[i+1] != 0:
        stairs[i+1] = stairs[i] + stairs[i-1]
print(stairs[N] % max_num)
### BUGGY CODE ###

import functools
import operator

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

answer = functools.reduce(operator.mul, A)

if answer > 1000000000000000000:
    print('-1') 
else:
    print(answer)

### FIXED CODE ###

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

if A.count(0) >= 1:
    print(0)
    exit()

answer = 1
for _ in A:
    answer *= _
    if answer > 1000000000000000000:
        print('-1') 
        exit()

print(answer)
### BUGGY CODE ###

N, K = map(int, input().split())
h_list = list(map(int, input().split()))

dp = [float("inf")] * N
dp[0] = 0

for i in range(N):
    for k in range(1, min(K+1, N-i)):
        dp[i + k] = min(dp[i + k], dp[i] + abs(h_list[i + k] - h_list[i]))
print(dp[N - 1])


### FIXED CODE ###

N, K = map(int, input().split())
h_list = list(map(int, input().split()))

dp = [float("inf") for _ in range(N + 1)]
dp[1] = 0
for i in range(1, N+1):
    for k in range(1, K+1):
        if i + k <= N:
            dp[i+k] = min(dp[i + k], dp[i] + abs(h_list[i-1] - h_list[i-1+k]))

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

n, Y = map(int,input().split())
x , y , z , e = '-1' , '-1' , '-1' , 0

for i in range(n+1):
  if e == 1:
    break
  for j in range(n+1):
    if n-i < 0 or e == 1:
      break
    for k in range(n+1):
      if n-i-j < 0 or e == 1:
        break
      if (i + j + k == n) and (10000 * i + 5000 * j + 1000 * k == Y):
        x , y , z , e = str(i) , str(j) , str(k) , 1
        break

print(x + ' ' + y + ' ' + z)

### FIXED CODE ###

# -*- coding: utf-8 -*-

n, Y = map(int,input().split())
x , y , z = '-1' , '-1' , '-1'

def check(num,total):
  for i in range(num+1):
    for j in range(num+1):
      k = num - i - j
      if (k >= 0) and (i + j + k == num) and (10000 * i + 5000 * j + 1000 * k == total):
        x , y , z = str(i) , str(j) , str(k)
        return x + ' ' + y + ' ' + z
res = check(n,Y)
if res is None:
  res = x + ' ' + y + ' ' + z
print(res)
### BUGGY CODE ###

a1=list(input().split( ))
a=list(map(int, a1))
b= a[1] - a[0]
d=2018

for j in range(b):
 for k in range(b-j):
  c=((a[0] + j ) * (a[0] + j  + k + 1)) % 2019
  if c<=d:
   d=c
  if d==0:
   break

print(d)

### FIXED CODE ###

a1=list(input().split( ))
a=list(map(int, a1))
b= a[1] - a[0]
d=2018

if b > 2019:
 print(0)
else:
 for j in range(b):
  for k in range(b-j):
    c= ((a[0] + j )* (a[0] + j  + k + 1)) % 2019
    if c<=d:
     d=c
 print(d)
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split(' ')))
for i in range(N-1):
    k=0
    for j in range(N-1):
        if A[j]==i+1:
            k=k+1
    print(k)
print(0)

### FIXED CODE ###

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

ans = [0] * n
for i in a:
    ans[i] += 1

for i in range(n-1):
    print(ans[i+1])
print(0)
### BUGGY CODE ###

import bisect
inf=10**12

a,b,q=map(int,input().split())
s=[-inf]+[int(input()) for _ in range(a)]+[inf];s.sort()
t=[-inf]+[int(input()) for _ in range(b)]+[inf];t.sort()

for i in range(q):
    x=int(input())
    p=bisect.bisect_left(s,x)
    q=bisect.bisect_left(t,x)

    sl,sr,tl,tr=s[p-1],s[p],t[q-1],t[q]

    sltl=x-min(sl,tl)
    srtr=max(sr,tr)-x
    sltr=min(x-sl*2+tr,-x-sl+tr*2)
    srtl=min(x-tl*2+sr,-x-tl+sr*2)
    print(min(sltl,srtr,sltr,srtl))

### FIXED CODE ###

import math
import bisect
inf=10**12
a,b,q=map(int,input().split())
s=[-inf]+[int(input()) for _ in range(a)]+[inf];s.sort()
t=[-inf]+[int(input()) for _ in range(b)]+[inf];t.sort()

for i in range(q):
    x=int(input())
    p=bisect.bisect_left(s,x)
    q=bisect.bisect_left(t,x)

    sl,sr,tl,tr=s[p-1],s[p],t[q-1],t[q]

    sltl=x-min(sl,tl)
    srtr=max(sr,tr)-x
    sltr=min(x-sl*2+tr,-x-sl+tr*2)
    srtl=min(x-tl*2+sr,-x-tl+sr*2)
    print(min(sltl,srtr,sltr,srtl))