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

n = int(input())
a = [int(i) for i in input().split()]

mod = 10**9+7

#sum_a = sum(a)

#bin_num = format(2, 'b')
#print(bin_num,len(bin_num))

max_a = max(a)
bin_max_a = format(max_a, 'b')

#print(bin_max_a)
ans = 0

for i in range(len(bin_max_a)):
    tmp = 0
    #print(i)
    kazu = 0
    for j in range(n):
        #print(j,kazu)
        kazu += int(format(a[j], 'b').zfill(len(bin_max_a))[i])
        #print(a[j],int(format(a[j], 'b').zfill(len(bin_max_a))[i]))
        #print(format(a[j], 'b').zfill(len(bin_max_a))[i])
        #print(kazu)
    if kazu == 0:
        continue
    tmp = kazu*(n-kazu) % mod
    tmp1 = 1
    for i in range(len(bin_max_a)-1-i):
        tmp1 *= 2
        tmp1 %= mod
    #tmp1 = 2**(len(bin_max_a)-1-i) % mod
    tmp = tmp*tmp1%mod
    ans += tmp
    ans %= mod
    #print(ans,kazu)
    
print(ans)

### FIXED CODE ###

n = int(input())
a = [int(i) for i in input().split()]

mod = 10**9+7

bin_a = []

for i in range(n):
    bin_a.append(format(a[i],'060b'))

ans = 0

for i in range(60):
    one = 0
    zero = 0
    for j in range(n):
        #print(bin_a[j])
        if bin_a[j][i] == '1':
            one += 1
        else:
            zero += 1
    #print(i,zero,one)
    ans += zero * one * (2**(59-i))
    
print(ans%mod)
### BUGGY CODE ###

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

import bisect, math
def comb(n,r):
  return math.factorial(n) / (math.factorial(r) * math.factorial(n-r))
  
arg_max_i = bisect.bisect_left(a, a[n-1] // 2)

if comb(a[n-1], a[arg_max_i]) < comb(a[n-1], a[arg_max_i-1]):
  arg_max_i -= 1
if arg_max_i == n-1:
  arg_max_i -= 1

print(a[n-1], a[arg_max_i])


### FIXED CODE ###

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

import bisect

arg_max_i = bisect.bisect_left(a, a[n-1] // 2)

if abs(a[n-1] / 2 - a[arg_max_i]) > abs(a[n-1] / 2 - a[arg_max_i-1]):
  arg_max_i -= 1
if arg_max_i == n-1:
  arg_max_i -= 1

print(a[n-1], a[arg_max_i])
### BUGGY CODE ###

N=int(input())
s = [input() for i in range(N)]
cnt=0
def verify_anagrams(w1, w2):
    f = lambda y: "".join(sorted(map(lambda x: x.replace(' ', '').lower(), y)))
    return f(w1) == f(w2)

for i in range(N-1):
    for j in range(i+1,N):
        if verify_anagrams(s[i], s[j]):
            cnt+=1

print(cnt)


### FIXED CODE ###

from collections import Counter
from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

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

count_by_kw=Counter()

for i in s:
    count_by_kw[''.join(sorted(i))]+=1

values, counts = zip(*count_by_kw.most_common())
cnt=0
for i in counts:
    if i>1:
        cnt+=combinations_count(i,2)

print(cnt)
### BUGGY CODE ###

N = int(input())
ans = []
for i in range(N):
    num = int(input())
    check = ans.count(num)
    if check == 0:
        ans.append(num)
    else:
        ans.remove(num)
print(len(ans))

### FIXED CODE ###

N = int(input())
ans = set()
for i in range(N):
    num = int(input())
    if num in ans:
        ans.remove(num)
    else:
        ans.add(num)
print(len(ans))
### BUGGY CODE ###

MOD=10**9+7
x , y = map(int, input().split())
a=(2*x-y)//3
b=(2*y-x)//3
if 2*a+b!=x:
    print(0)
    exit()

factorial = [1]
inverse = [1]

n=a+b
r=a

for i in range(1, n+2):
    factorial.append(factorial[-1] * i % MOD)
    inverse.append(pow(factorial[-1], MOD - 2, MOD))

def combi(n, r):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % MOD

ans = combi(n,r)
print(ans)


### FIXED CODE ###

MOD=10**9+7
x , y = map(int, input().split())
a=(2*x-y)//3
b=(2*y-x)//3
if 2*a+b!=x:
    print(0)
    exit()

factorial = [1]
inverse = [1]

n=a+b
r=a

for i in range(1, n+2):
    factorial.append(factorial[-1] * i % MOD)
    inverse.append(pow(factorial[-1], MOD - 2, MOD))

def combi(n, r):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % MOD

ans = combi(n,r)
print(ans)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10 ** 6)


MOD = 10 ** 9 + 7


def modFact(n):
    global MOD
    if n == 1:
        return 1
    else:
        return (n * modFact(n - 1)) % MOD


def modInvFact(n):
    global MOD
    ans = 1
    for i in range(1, n + 1):
        ans *= modPow(i, MOD - 2)
        ans %= MOD
    return ans


def modCombi(n, r):
    global MOD
    if r == 0:
        return 1
    else:
        N = modFact(n)
        NR = modInvFact(n - r)
        R = modInvFact(r)
    return (N * R * NR) % MOD


def modPow(n, m):
    global MOD
    if m == 0:
        return 1
    elif m % 2 == 1:
        return (n * modPow(n, m - 1)) % MOD
    else:
        d = modPow(n, m // 2)
        return (d * d) % MOD


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

for i in range(1, K + 1):
    A = modCombi(K - 1, i - 1)
    if N - K < i - 1:
        B = 0
    else:
        B = modCombi(N - K + 1, i)
    print((A * B) % MOD)


### FIXED CODE ###

MOD = 10 ** 9 + 7


def prepare(n):
    global MOD
    factorials = [0] * (n + 1)
    factorials[0] = 1
    for i in range(n):
        factorials[i + 1] = (factorials[i] * (i + 1)) % MOD

    invs = [1] * (n + 1)
    invs[n] = pow(factorials[n], MOD - 2, MOD)
    for i in range(n, 1, -1):
        invs[i - 1] = (invs[i] * i) % MOD

    return factorials, invs


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

factorials, invs = prepare(N)

for i in range(1, K + 1):
    A = factorials[K - 1] * invs[i - 1] * invs[K - i] % MOD
    if N - K < i - 1:
        B = 0
    else:
        B = factorials[N - K + 1] * invs[i] * invs[N - K + 1 - i] % MOD
    print((A * B) % MOD)
### BUGGY CODE ###

a,b,k=map(int,input().split())
s=set()
for i in range(a,b+1):
  if i-a<k:
    s.add(i)
  elif b-i<k:
    s.add(i)
arr=list(s)
arr=sorted(arr)
for i in arr:
  print(i)

### FIXED CODE ###

a,b,k=map(int,input().split())
s=set()
if k>(b-a):
  for i in range(a,b+1):
    print(i)
else:
  for i in range(min(k,b-a)):
    s.add(a+i)
    s.add(b-i)
  arr=list(s)
  arr=sorted(arr)
  for i in arr:
    print(i)
### BUGGY CODE ###

I = list(map(int, input().split()))
N = I[0]
K = I[1]
H = list(map(int, input().split()))

dp = [10**10 for _ in range(N+1)]

dp[N] = 0
dp[N-1] = abs(H[N-1]-H[N-2])
for i in range(N-2,0,-1):
	for k in range(1,K+1):
		if i+k<=N:
			dp[i] = min(abs(H[i+k-1]-H[i-1])+dp[i+k], dp[i])

print(dp[1])

### FIXED CODE ###

I = list(map(int, input().split()))
N = I[0]
K = I[1]
H = list(map(int, input().split()))

dp = [float('inf') for _ in range(N+1)]

dp[N] = 0
for i in range(N-1,N-K-1,-1):
	if i>0:
		dp[i] = abs(H[i-1]-H[N-1])

for i in range(N-K-1,0,-1):
	for k in range(1,K+1):
		if i+k<=N:
			dp[i] = min(abs(H[i+k-1]-H[i-1])+dp[i+k], dp[i])

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

k = int(input())

li=[]

for i in range(1,10):
    li.append(i)
for i in range(10,100):
    if abs(int(str(i)[0])-int(str(i)[1]))<2:
           li.append(i)
for i in range(100,1000):
    if abs(int(str(i)[1])-int(str(i)[2]))<2 and abs(int(str(i)[0])-int(str(i)[1]))<2:
           li.append(i)
for i in range(1000,10000):
    if abs(int(str(i)[2])-int(str(i)[3]))<2 and abs(int(str(i)[1])-int(str(i)[2]))<2 and abs(int(str(i)[0])-int(str(i)[1]))<2:
           li.append(i)
for i in range(10000,100000):
    if abs(int(str(i)[3])-int(str(i)[4]))<2 and abs(int(str(i)[2])-int(str(i)[3]))<2 and abs(int(str(i)[1])-int(str(i)[2]))<2 and abs(int(str(i)[0])-int(str(i)[1]))<2:
           li.append(i)
for i in range(100000,1000000):
    if abs(int(str(i)[4])-int(str(i)[5]))<2 and abs(int(str(i)[3])-int(str(i)[4]))<2 and abs(int(str(i)[2])-int(str(i)[3]))<2 and abs(int(str(i)[1])-int(str(i)[2]))<2 and abs(int(str(i)[0])-int(str(i)[1]))<2:
           li.append(i)
for i in range(1000000,10000000):
    if abs(int(str(i)[5])-int(str(i)[6]))<2 and abs(int(str(i)[4])-int(str(i)[5]))<2 and abs(int(str(i)[3])-int(str(i)[4]))<2 and abs(int(str(i)[2])-int(str(i)[3]))<2 and abs(int(str(i)[1])-int(str(i)[2]))<2 and abs(int(str(i)[0])-int(str(i)[1]))<2:
           li.append(i)
for i in range(10000000,100000000):
    if abs(int(str(i)[6])-int(str(i)[7]))<2 and abs(int(str(i)[5])-int(str(i)[6]))<2 and abs(int(str(i)[4])-int(str(i)[5]))<2 and abs(int(str(i)[3])-int(str(i)[4]))<2 and abs(int(str(i)[2])-int(str(i)[3]))<2 and abs(int(str(i)[1])-int(str(i)[2]))<2 and abs(int(str(i)[0])-int(str(i)[1]))<2:
           li.append(i)
for i in range(100000000,1000000000):
    if abs(int(str(i)[7])-int(str(i)[8]))<2 and abs(int(str(i)[6])-int(str(i)[7]))<2 and abs(int(str(i)[5])-int(str(i)[6]))<2 and abs(int(str(i)[4])-int(str(i)[5]))<2 and abs(int(str(i)[3])-int(str(i)[4]))<2 and abs(int(str(i)[2])-int(str(i)[3]))<2 and abs(int(str(i)[1])-int(str(i)[2]))<2 and abs(int(str(i)[0])-int(str(i)[1]))<2:
           li.append(i)
                    
print(li[k-1])

### FIXED CODE ###

k = int(input())
li =[i+1 for i in range(9)]

c = 0
while len(li) < k:
    if li[c]%10 != 0:
        li.append(li[c]*10+li[c]%10-1)
    li.append(li[c]*10+li[c]%10)
    if li[c]%10 != 9:
        li.append(li[c]*10+li[c]%10+1)
    c +=1
    
print(li[k-1])
### BUGGY CODE ###

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

print(c)

### FIXED CODE ###

from collections import*
n=int(input())
s=input()
c=0
r=s.count('R')
g=s.count('G')
b=s.count('B')
rgb=r*g*b

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

print(rgb)
### BUGGY CODE ###

inp = input()

s = 0

for i in range(len(inp)-3):
	num = int(inp[i:])
	while num > 2019:
		if num % 2019 == 0:
			s += 1
		num = num // 10

print(s)

### FIXED CODE ###

from collections import defaultdict

s = input()

rests = [0]
n = 0
a = 0
m = 1
c = defaultdict(int)
c[0] += 1
for i in list(s)[::-1]:
    n = n + int(i)*m

    c[n % 2019] += 1
    m = m * 10 % 2019

for v in c.values():
    a += v * (v-1) // 2
print(a)
### BUGGY CODE ###

#入力（後でいじる）
def pin(type=int):
    return map(type,input().split())
#どっかで最悪計算量の入力データを用意する関数を作ろう？
def worstdata():
    a=0
    return a
#solution:
def comp(x,y):
    dp=[0,max(act[0])]*(N+1) #[i]日目のActivity で得られる最大幸福量
    
    return 0

#input
n,=(pin())
S=list(pin())

c=[]
cond=0
for i in range(len(S)):
    if S[i] in c:
        cond=1
        break
    c.append(S[i])
#print(solution(H,N))
print(["NO","YES"][1-cond])



### FIXED CODE ###

#入力（後でいじる）
def pin(type=int):
    return map(type,input().split())
#どっかで最悪計算量の入力データを用意する関数を作ろう？
def worstdata():
    a=0
    return a
#solution:
def comp(p):#期待値
    return ((p+1)/2)



#input
n,=(pin())
S=sorted(list(pin()))
cond=1
for i in range(1,n):
    if S[i]-S[i-1]==0:
        cond=0
        break

print(["NO","YES"][cond])
### BUGGY CODE ###

X = int(input())
A = 1
K = 1
while K < X:
    A += 1
    K = A ** 5
S = A - 4
w = 0
while w != 1:
    Q = S ** 5 - X
    if Q < 0:
        B = 0
        while (Q < B**5):
            B -= 1
        if (Q == B**5):
            w += 1
        else:
            S += 1
        
    elif Q > 0:
        B = 0
        while (Q > B**5):
            B += 1
        if (Q == B**5):
            w += 1
        else:
            S += 1
    else:
        B = 0
print(S,B)

### FIXED CODE ###

X = int(input())
for i in range (240):
    for j in range (240):
        s = (i -120)**5 - (j -120)**5
        if s == X:
            A = i-120
            B = j -120
print(A,B)
### BUGGY CODE ###

n = int(input())
a = [int(input()) for i in range(n)]
tmp = [a[0]]
for i in range(1,n):
  if a[i] <= min(tmp):
    tmp.append(a[i])
  else:
    for j in range(len(tmp)):
      if a[i]>tmp[j]:
        tmp[j]=a[i]
        break
print(len(tmp))

### FIXED CODE ###

import bisect
n = int(input())
a = [- int(input()) for i in range(n)]
tmp = [a[0]]
for i in range(1,n):
  idx=bisect.bisect_right(tmp,a[i])
  if idx==len(tmp):
    tmp.append(a[i])
  else:
    tmp[idx] =a[i]
print(len(tmp))
### BUGGY CODE ###

K, S = [int(i) for i in input().split()]
cnt = 0

for x in range(K+1):
  for y in range(K+1):
    candi_z = S - x - y 
    for z in range(candi_z + 1):
      if x + y + z == S and x <= K and y <= K and z <= K:
        cnt += 1
print(cnt)

### FIXED CODE ###

K, S = [int(i) for i in input().split()]
cnt = 0

for x in range(K+1):
  for y in range(K+1):
    candi_z = S - x - y 
    if 0<= candi_z <= K:
      cnt += 1
      

print(cnt)
### BUGGY CODE ###

N, K = map(int, input().split())
p = list(map(int, input().split()))
m = K
for i in range(N-K+1):
    m = max(sum(p[i:i+K]),m)
print((m+K)/2)


### FIXED CODE ###

N, K = map(int, input().split())
p = list(map(int, input().split()))
s = sum(p[:K])
m = s
for i in range(1, N-K+1):
    s = s - p[i-1] + p[i+K-1]
    m = max(m, s)
print((m+K)/2)
### BUGGY CODE ###

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

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

dp = {}

dp[1] = 0
dp[2] = abs(H[1]-H[0])

for i in range(3, N+1):
    li = []
    for j in range(1,K+1):
        if (i - j) > 0:
            li.append(dp[i-j]+abs(H[i-1]-H[i-(j+1)]))
    dp[i] = min(li)

print(dp[N])

### FIXED CODE ###

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

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

dp = {}

dp[1] = 0
dp[2] = abs(H[1]-H[0])

for i in range(3, N+1):
    li = []
    for j in range(1,min(K+1,i)):
        li.append(dp[i-j]+abs(H[i-1]-H[i-(j+1)]))
    dp[i] = min(li)

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

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

one = []
zero = []
cnt_one = 0
cnt_zero = 0

if s[0] == "#":
    zero.append(0)

for i in s:
    if i == "#":
        cnt_one += + 1
        if cnt_zero != 0:
            zero.append(cnt_zero)
            cnt_zero = 0
    else:
        cnt_zero += 1
        if cnt_one != 0:
            one.append(cnt_one)
            cnt_one = 0

if cnt_one != 0:
    one.append(cnt_one)
else:
    zero.append(cnt_zero)

if s[-1] == "#":
    zero.append(0)

print(min(sum(one[:i]) + sum(zero[i+1:]) for i in range(len(one)+1)))

### FIXED CODE ###

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

one = []
zero = []
cnt_one = 0
cnt_zero = 0

if s[0] == "#":
    zero.append(0)

for i in s:
    if i == "#":
        cnt_one += + 1
        if cnt_zero != 0:
            zero.append(cnt_zero)
            cnt_zero = 0
    else:
        cnt_zero += 1
        if cnt_one != 0:
            one.append(cnt_one)
            cnt_one = 0

if cnt_one != 0:
    one.append(cnt_one)
else:
    zero.append(cnt_zero)

if s[-1] == "#":
    zero.append(0)

sum = 0
sum_zero = [0]
for i in range(len(one)):
    sum += zero[len(one)-i]
    sum_zero.append(sum)

sum = 0
sum_one = [0]
for i in range(len(one)):
    sum += one[i]
    sum_one.append(sum)


print(min(sum_one[i] + sum_zero[len(one)-i] for i in range(len(one)+1)))
### BUGGY CODE ###

class BinaryIndexedTree(object):
    def __init__(self, N):
        self.N = N
        self.bit = [0] * (N + 1)

    def bit_add(self, index, num):
        while (index <= self.N):
            self.bit[index] += num
            index += index & -index
        return

    def bit_sum(self,index):
        result = 0
        while (index > 0):
            result += self.bit[index]
            index -= index & -index
        return result

def main():
    N, Q = list(map(int, input().split()))
    c = list(map(int, input().split()))
    query = [tuple(list(map(int, input().split())) + [i]) for i in range(Q)]
    query_result = [0 for i in range(Q)]
    collar_pos = [-1] * (N + 1)
    bit = BinaryIndexedTree(N)

    sorted_query = sorted(query, key=lambda query: query[1])

    index = 1
    for query in sorted_query:
        while (index <= query[1]):
            # update BIT
            if (collar_pos[c[index-1]] != -1):
                bit.bit_add(collar_pos[c[index-1]], -1)
            bit.bit_add(index, 1)
            # update collar_pos
            collar_pos[c[index-1]] = index
            index += 1
        query_result[query[2]] = bit.bit_sum(query[1]) - bit.bit_sum(query[0]-1)

    print(*query_result, sep='\n')

if __name__ == '__main__':
    main()

### FIXED CODE ###

class BinaryIndexedTree(object):
    def __init__(self, N):
        self.N = N
        self.bit = [0] * (N + 1)

    def bit_add(self, index, num):
        while (index <= self.N):
            self.bit[index] += num
            index += index & -index
        return

    def bit_sum(self,index):
        result = 0
        while (index > 0):
            result += self.bit[index]
            index -= index & -index
        return result

    def bit_sum_range(self, l, r):
        if (l == r):
            return 1
        else:
            return self.bit_sum(r) - self.bit_sum(l - 1)

def main():
    from sys import stdin
    N, Q = list(map(int, input().split()))
    c = list(map(int, input().split()))
    readline = stdin.readline
    query = [list(map(int, readline().split())) + [i] for i in range(Q)]
    query_result = [0 for i in range(Q)]
    collar_pos = [-1] * (N + 1)
    bit = BinaryIndexedTree(N)

    sorted_query = sorted(query, key=lambda query: query[1])

    index = 1
    for query in sorted_query:
        l, r, q = query
        while (index <= r):
            # update BIT
            if (collar_pos[c[index-1]] != -1):
                bit.bit_add(collar_pos[c[index-1]], -1)
            bit.bit_add(index, 1)
            # update collar_pos
            collar_pos[c[index-1]] = index
            index += 1
        query_result[q] = bit.bit_sum_range(l, r)

    print(*query_result, sep='\n')

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

from math import log

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

bits = [[] for x in range(20)]
queue = []
ans = 0
for i in range(N):
    temp = set()
    while A[i] > 0:
        bits[int(log(A[i] & -A[i],2))].append(i)
        temp.add(A[i] & -A[i])
        A[i] -= A[i] & -A[i]
    popAt = -1
    for j in range(len(queue)):
        if len(queue[j][1] | temp) < len(queue[j][1]) + len(temp):
            ans += i - queue[j][0]
            popAt = j
        else:
            queue[j][1] |= temp
    queue = queue[popAt+1:] + [[i,temp]]

z = (len(queue) * (len(queue) + 1)) // 2
print(ans+z)


### FIXED CODE ###

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

cur = A[0]
ans = 0
rp = 1
for lp in range(N):
    while rp < N and cur & A[rp] == 0:
        cur += A[rp]
        rp += 1
    ans += rp - lp
    cur -= A[lp]
    
print(ans)
### BUGGY CODE ###

N, K = map(int,input().split())
A = list(map(int,input().split()))
c_list = []
count = 0

A_list = list(set(A))
l = len(A_list)

for a in A_list:
    c = A.count(a)
    c_list.append(c)

c_list.sort()

if K >= l:
    print(0)
else:
    for i in range(l - K):
        count += c_list[i]
    print(count) 

### FIXED CODE ###

import collections

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

A_cou = collections.Counter(A)
B = A_cou.most_common()
l = len(B)
count = 0

if K >= l:
    print(0)
else:
    for i in range(l - K):
        count += B[-i-1][1]
    print(count)
### BUGGY CODE ###

from functools import reduce
import functools
import fractions
 
@functools.lru_cache(maxsize=12800)
def gcd(*numbers):
  return reduce(gcd_, numbers)
  # return reduce(fractions.gcd, numbers)

@functools.lru_cache(maxsize=12800)
def gcd_(a, b):
  if (b == 0):
    return a
  return gcd(b, a % b)
 
sum = 0
K = int(input())
 
for i in range(1,K+1):
  for j in range(1,K+1):
    for k in range(1,K+1):
      sum += gcd(i,j,k)
      
print(sum)


### FIXED CODE ###

from functools import reduce
import functools
import fractions
 
@functools.lru_cache(maxsize=None)
def gcd(*numbers):
  return reduce(gcd_, numbers)

@functools.lru_cache(maxsize=None)
def gcd_(a, b):
  if (b == 0):
    return a
  return gcd(b, a % b)
 
sum = 0
K = int(input())
 
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      if (i == j) & (j == k):
        sum += gcd(i,j,k)
      elif (i == j) & (j != k):
        sum += gcd(i,j,k)*3
      elif (i != j) & (j == k):
        sum += gcd(i,j,k)*3
      elif (i != j) & (j != k):
        sum += gcd(i,j,k)*6

print(sum)
### BUGGY CODE ###

n, k = list(map(int, input().split()))
hs = list(map(int, input().split()))

# 足場 i にたどり着くまでのコストの総和の最小値
dp = [1e14] * n

dp[0] = 0
for i in range(1, min(k, n)):
    dp[i] = abs(hs[0] - hs[i])

if n > k:
    for i in range(k, n):
        costs = [abs(hs[i] - hs[i - j]) + dp[i - j] for j in range(1, k + 1)]
        dp[i] = min(costs)

print(dp[n - 1])

### FIXED CODE ###

n, k = map(int, input().strip().split())
hs = list(map(int, input().strip().split()))

dp = [0] * n

for i in range(1, n):
    costs = []
    for j in range(1, k + 1):
        if i - j >= 0:
            cost = dp[i - j] + abs(hs[i] - hs[i - j])
            costs.append(cost)
    dp[i] = min(costs)

print(dp[n - 1])
### BUGGY CODE ###

N, M = map(int, input().split())
ans = 1
if N == 1:
    print(M)
else:
    for i in range(2, M//N+1):
        if M % i == 0:
            ans = i
    print(ans)


### FIXED CODE ###

import math
N, M = map(int, input().split())
tmp = [1]
if N == 1:
    print(M)
else:
    for i in range(2, int(math.ceil(M**0.5))):
        if M % i == 0:
            tmp.append(i)
            tmp.append(int(M/i))
    ans = 1
    for x in tmp:
        if M / x >= N:
            ans = max(ans, x)
    print(ans)
### BUGGY CODE ###

s = list(input())
q = int(input())
mode = 0
for i in range(q):
  read = list(map(str,input().split()))
  if read[0]=='1':
    mode = (mode+1)%2
  else:
    if (read[1]=='1' and mode == 0) or (read[1]=='2' and mode == 1):
      s = [read[2]]+ s
    else:
      s = s + [read[2]]
if mode == 0:
  print(''.join(s))
else:
  print(''.join(s[::-1]))

### FIXED CODE ###

s = list(input())
q = int(input())

head = []
tail = []

mode = 0
for i in range(q):
  read = list(map(str,input().split()))
  if read[0]=='1':
    mode = (mode+1)%2
  else:
    if (read[1]=='1' and mode == 0) or (read[1]=='2' and mode == 1):
      head.append(read[2])
    else:
      tail.append(read[2])

s = head[::-1] + s
s = s + tail
if mode == 0:
  print(''.join(s))
else:
  print(''.join(s[::-1]))
### BUGGY CODE ###

n,k = map(int,input().split())
ls = list(map(int,input().split()))
a = []
b = []
for i in range(n):
    p = (ls[i]+1)/2
    a.append(p)
for j in range(n+1-k):
    q = sum(a[j:j+k])
    b.append(q)
print(max(b))

### FIXED CODE ###

n,k = map(int,input().split())
ls = list(map(int,input().split()))
l,p = 0,0
for i in range(n):
    if l < k:
        p += (1+ls[i])/2
        l += 1
        continue
    if i == k:
        now = p
    now = now-((1+ls[i-k])/2)+((1+ls[i])/2)
    p = max(p,now)
print(p)
### BUGGY CODE ###

X = int(input())
dp = set([0])

ans = 0
while X not in dp:
    ans += 1
    for i in list(dp):
        if i + ans not in dp:
            dp.add(i + ans)
        if i - ans not in dp:
            dp.add(i - ans)
print(ans)


### FIXED CODE ###

X = int(input())

ans = 0
sums = 0
while sums < X:
    ans += 1
    sums += ans
print(ans)
### BUGGY CODE ###

N = int(input())
A_list = [int(i) for i in input().split(" ")]

result = "YES"
for i in range(0, N-1):
  for j in range(i+1, N):
    if A_list[i] == A_list[j]:
      result = "NO"
      break
  if result == "NO":
    break

print(result)


### FIXED CODE ###

N = int(input())
A_list = [int(i) for i in input().split(" ")]
B_list = list(set(A_list))

if len(A_list) == len(B_list):
  print("YES")
else:
  print("NO")
### BUGGY CODE ###

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

dp = [float('inf')]*n
dp[0] = 0
for i in range(1,k+1):
    dp[i] = abs(l[0]-l[i])
    
    if i == n-1:
        break

for i in range(k+1,n):
    dp_min = float('inf')
    for j in range(1,k+1):
        tmp = dp[i-j] + abs(l[i] - l[i-j])
        dp_min = min(dp_min, tmp)
    
    dp[i] = dp_min
    
print(dp[n-1])

### FIXED CODE ###

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

dp = [float('inf')]*n
dp[0] = 0
for i in range(1,k+1):
    dp[i] = abs(l[0]-l[i])
    
    if i == n-1:
        break

for i in range(k+1,n):
    dp_min = float('inf')
    for j in range(1,k+1):
        tmp = dp[i-j] + abs(l[i] - l[i-j])
        dp_min = min(dp_min, tmp)
    
    dp[i] = dp_min
    
print(dp[n-1])