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

def main():
    """
    1 <= i <= N
    p_1 ... p_n
    p_i != p_j
    2 <= N <= 10^5

    p_i != i にする
    最悪、sort(reverse) + αすれば終わりそう
    -> N^2
    """
    N = int(input())
    p = list(map(int, input().split()))
    n_swap = 0

    for i in range(N - 1):
        for j in range(i, N - 1):
            if p[j] == j + 1 or p[j + 1] == j + 2:
                p[j + 1], p[j] = p[j], p[j + 1]
                n_swap += 1

    print(n_swap)

if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    """
    1 <= i <= N
    p_1 ... p_n
    p_i != p_j
    2 <= N <= 10^5

    p_i != i にする
    最悪、sort(reverse) + αすれば終わりそう
    -> N^2(10^5^2 -> 10^10 NG)

    xx: 12(xx) -> 21(oo)
    xo: 132(xoo) -> 312(ooo)
    ox:
      oは飛ばしてxから始める
    """
    N = int(input())
    p = map(int, input().split())
    n_swap = 0

    p_same = [i == _p for i, _p in enumerate(p, 1)]
    # enumerateでは複製になるのでrangeで回しながら更新
    for i in range(N - 1):
        same = p_same[i]
        if same:
            n_swap += 1
            p_same[i] = p_same[i + 1] = False

    if p_same[-1]:
        n_swap += 1
    print(n_swap)

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

N = int(input())
T = [[] for i in range(N+1)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    T[a].append([b,c])
    T[b].append([a,c])

Q, K = map(int, input().split())
shortest_path = [0 for _ in range(N+1)]
V, queue = set([K]), T[K]
while len(queue) > 0:
    v = queue.pop(queue.index(min(queue, key = lambda x: x[1])))
    shortest_path[v[0]] = v[1]
    for u in T[v[0]]:
        if u[0] not in V:
            queue.append([u[0], u[1] + v[1]])
    V.add(v[0])

for _ in range(Q):
    x, y = map(int, input().split())
    print(shortest_path[x] + shortest_path[y])

### FIXED CODE ###

from collections import deque

N = int(input())
T = [{} for i in range(N+1)]
for _ in range(N-1):
    a, b, c = map(int, input().split())
    T[a][b] = c
    T[b][a] = c

Q, K = map(int, input().split())
shortest_path = [0 for _ in range(N+1)]
queue = deque([[v, T[K][v], K] for v in T[K].keys()])
for _ in range(N-1):
    v = queue.popleft()
    shortest_path[v[0]] = v[1]
    queue.extend([[u, T[v[0]][u] + v[1], v[0]] for u in T[v[0]].keys() if u != v[2]])

for _ in range(Q):
    x, y = map(int, input().split())
    print(shortest_path[x] + shortest_path[y])
### BUGGY CODE ###

import sys
input = sys.stdin.readline

def main():
    n = int(input())
    ans = 0
    cnt = [[0]*45 for _ in range(45)]
    for _ in range(n):
        a = list(map(str, input().rstrip().split(".")))
        x = 0
        if len(a) == 1:
            x = int(a[0]) * 10**9
        else:
            x = int(a[0]) * 10**9 + int(a[1]) * pow(10, 9-len(a[1]))
        cnt_2, cnt_5 = 0, 0
        while x % 2 == 0:
            cnt_2 += 1
            x //= 2
        while x % 5 == 0:
            cnt_5 += 1
            x //= 5
        for i in range(45):
            for j in range(45):
                if cnt_2 + i >= 18 and cnt_5 + j >= 18:
                    ans += cnt[i][j]
        cnt[cnt_2][cnt_5] += 1
    print(ans)

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
input = sys.stdin.readline

n = int(input())
ans = 0
cnt = [[0]*20 for _ in range(45)]
for _ in range(n):
    a = list(map(str, input().rstrip().split(".")))
    x = 0
    if len(a) == 1:
        x = int(a[0]) * 10**9
    else:
        x = int(a[0]) * 10**9 + int(a[1]) * pow(10, 9-len(a[1]))
    cnt_2, cnt_5 = 0, 0
    while x % 2 == 0:
        cnt_2 += 1
        x //= 2
    while x % 5 == 0:
        cnt_5 += 1
        x //= 5
    for i in range(45):
        for j in range(20):
            if cnt_2 + i >= 18 and cnt_5 + j >= 18:
                ans += cnt[i][j]
    cnt[cnt_2][cnt_5] += 1
print(ans)
### BUGGY CODE ###

from math import factorial
n=int(input())
a=list(map(int,input().split()))
b=max(a)
a.remove(b)
l=[]
for i in range(n-1):
  l.append([factorial(b)//(factorial(b-a[i])*factorial(a[i])),a[i]])
l.sort(reverse=True)
print(b,l[0][1])

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
b=max(a)
a.remove(b)
l=[]
for i in range(n-1):
  l.append([abs(b/2-a[i]),a[i]])
l.sort()
print(b,l[0][1])
### BUGGY CODE ###

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

i = 1
result = a[0]
for i in range(len(a)-1):
    result = result * a[i+1]

if result > 10**18:
    print(-1)
else:
    print(result)


### FIXED CODE ###

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

i = 1
result = a[0]
flag = False
if a[-1] == 0:
    result = 0
else:
    for i in range(len(a)-1):
        result = result * a[i+1]
        if result > 10 ** 18:
            flag = True
            break

if flag:
    print(-1)
else:
    print(result)
### BUGGY CODE ###

n, q = map(int, input().split())
s = input()
li = [i for i in s]
pos = []
for i in range(len(li)-1):
    if li[i]== "A" and li[i+1] == "C":
        pos.append(i)
for i in range(q):
    l, r = map(int, input().split())
    pos_copy=pos.copy()
    pos_copy+=[r-1, l-1]
    pos_copy.sort()
    print(pos_copy.index(r-1)  - pos_copy.index(l-1) - 1)


### FIXED CODE ###


n, q= map(int, input().split())
s = input()

li = [list(map(int, input().split())) for _ in range(q)]
pos = [0]*n
count=0
for i in range(1, n):   
    if s[i-1] == "A" and s[i]=="C":
        count+=1
    pos[i] = count

for i in li:
    print(pos[i[1]-1]-pos[i[0]-1])
### BUGGY CODE ###

N = int(input())
lst = [int(x) for x in input().split()]
ret = 1
for x in lst:
    ret *= x

if 10 ** 18 >= ret:
    print(ret)
else:
    print(-1)


### FIXED CODE ###

N = int(input())
lst = sorted([int(x) for x in input().split() if x != "1"], reverse=True)

if len(lst) == 0:
    print(1)
    exit()
if lst[-1] == 0:
    print(0)
    exit()

ret = 1
for x in lst:
    ret *= x
    if ret > 10 ** 18:
        print(-1)
        exit()

print(ret)
### BUGGY CODE ###

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

n = []
for i in range(K + 1):
  n.append(i)

import itertools
XYZ = list(itertools.product(n, repeat = 3))

count = 0
for j in range(len(XYZ)):
  if sum(XYZ[j]) == S:
    count += 1

print(count)

### FIXED CODE ###

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

ans = 0
for i in range(K + 1):
  for j in range(K + 1):
    if i + j == S:
      ans += 1
    elif 0 <= S - (i + j) <= K:
      ans += 1
print(ans)
### BUGGY CODE ###

def printt(x):
  print(x,flush=True)
  i=input()
  if i=="Vacant":exit()
  if i=="Male":return 0
  return 1
n=int(input())
ng=0
ok=n
f=printt(0)
while ng+1!=ok:
  mid=(ok+ng)//2
  if printt(mid)==f and (ng+mid)%2==0:ng=mid
  else:ok=mid

### FIXED CODE ###

n=int(input())
def out(i):
  print(i,flush=True)
  s=input()
  if s=="Male":return 0
  elif s=="Female":return 1
  else:exit()
f=out(0)
ng=0
ok=n
while ng+1!=ok:
  mid=(ng+ok)//2
  if (mid+f)%2==out(mid)%2:ng=mid
  else:ok=mid
### BUGGY CODE ###

import sys,queue,copy
sys.setrecursionlimit(10**8)
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = int(input())

S = list()

for i in range(N):
    S.append(list(input()))
    S[i].sort()

S.sort()
ans = 0
for j in range(N):
    k = 1
    while j+k < N and S[j][0] == S[j+k][0]:
        if "".join(S[j]) == "".join(S[j+k]):
            ans += 1
        k += 1

print(ans)

### FIXED CODE ###

import sys,queue,copy
sys.setrecursionlimit(10**8)
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = int(input())

S = list()
num = {}
ans = 0

for i in range(N):
    S.append(list(input()))
    S[i].sort()
    if "".join(S[i]) in num:
        num["".join(S[i])] += 1
        ans += num["".join(S[i])]
    else:
        num["".join(S[i])] = 0

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

import math
from collections import deque
n,m=[int(_) for _ in input().split()]
A=[[int(_) for _ in input().split()] for l in range(m)]
R=deque([] for _ in range(n))
for a in A:
    R[a[0]-1].append(a[1]-1)
    R[a[1]-1].append(a[0]-1)
print("Yes")
for h in range(1,n):
    dq=deque()
    vlist=deque([0]*n)
    vlist[h]=1
    dq.appendleft([h])
    while dq:
        x=dq.popleft()
        if 0 in R[x[-1]]:
            if len(x)==1:
                print(1)
            else:
                print(x[1]+1)
            break
        for a in R[x[-1]]:
            if vlist[a]==0:
                dq.append(x+[a])
                vlist[a]=1



### FIXED CODE ###

import math
from collections import deque
n,m=[int(_) for _ in input().split()]
A=[[int(_) for _ in input().split()] for l in range(m)]
R=deque([] for _ in range(n))
for a in A:
    R[a[0]-1].append(a[1]-1)
    R[a[1]-1].append(a[0]-1)
print("Yes")
Dis=deque([100000000]*n)
Dis[0]=0
PP=deque([0]*n)
l=deque()
l.append(0)
while l:
    x=l.popleft()
    for a in R[x]:
        if Dis[a]>Dis[x]+1:
            Dis[a]=Dis[x]+1
            l.append(a)
            PP[a]=x
for t in range(1,n):
    print(PP[t]+1)
### BUGGY CODE ###

# https://atcoder.jp/contests/tenka1-2017/tasks/tenka1_2017_c
# 式をこねくり回せ

N = int(input())

from itertools import product

for n, h in product(range(1, 3501), range(1, 3501)):
    bunbo = (4 * n * h - N * n - N * h)
    if bunbo <= 0:
        continue
    w = (N * h * n) / bunbo
    if w.is_integer():
        print(n, h, int(w))
        exit()


### FIXED CODE ###

# https://atcoder.jp/contests/tenka1-2017/tasks/tenka1_2017_c
# 式をこねくり回せ

N = int(input())

from itertools import product

for n, h in product(range(1, 3501), range(1, 3501)):
    bunbo = (4 * n * h - N * n - N * h)
    if bunbo <= 0:
        continue
    w = (N * h * n) / bunbo
    if w.is_integer():
        print(n, h, int(w))
        exit()
### BUGGY CODE ###

from math import ceil, floor

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

if n == 1:
  print(ceil(h/l[0][0]) * l[0][1])
  exit(0)

dp = [ceil(hp/l[0][0]) * l[0][1] for hp in range(h + 1)]

for i in range(1, n):
  for hp in range(h + 1):
    if hp >= l[i][0]:
      dp[hp] = min([dp[hp], dp[hp - l[i][0]] + l[i][1]])
    else:
      dp[hp] = min([dp[hp], l[i][1]])

print(dp[-1])


### FIXED CODE ###

from math import ceil, floor

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

if n == 1:
  print(ceil(h/l[0][0]) * l[0][1])
  exit(0)

dp = [ceil(hp/l[0][0]) * l[0][1] for hp in range(h + 1)]

for i in range(1, n):
  for hp in range(h + 1):
    if hp >= l[i][0]:
      dp[hp] = min([dp[hp], dp[hp - l[i][0]] + l[i][1]])
    else:
      dp[hp] = min([dp[hp], l[i][1]])

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

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

ans = 0
flag = True

while flag:
    for i in A:
        if i % 2 == 0 and i != 0:
            i %= 2
        else:
            flag = False
    if flag == True:
        ans += 1
print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
ans = 0
while 1 not in [a%2 for a in A]:
    A = [a//2 for a in A]
    ans += 1
print(ans)
### BUGGY CODE ###

n = int(input())
A = list(map(int, input().split()))
cnt = 0
for i in range(n):
    if A[i] <= 0:
        cnt += 1
        A[i] = A[i]*(-1)
if cnt % 2 == 0:
    for a in A:
        ans = sum(A)
else:
    ans = sum(A) - 2*min(A)
print(ans)

### FIXED CODE ###

n = int(input())
A = list(map(int, input().split()))
cnt = 0
s = 0
m = 10 ** 9
for ai in A:
    if ai <= 0:
        cnt = cnt + 1
        ai *= (-1)
    s += ai
    if m > ai:
        m = ai
if cnt % 2 == 0:
    for a in A:
        ans = s
else:
    ans = s - 2*m
print(ans)
### BUGGY CODE ###

n, q = map(int , input().split())
queue = []
for _ in range(n):
    line = input().split()
    queue.append([line[0], int(line[1])])

class my_queue:
    def __init__(self, queue):
        self.queue = queue
    def enqueue(self, item):
        self.queue = self.queue + [item]
        #self.queue.append(item)
    def dequeue(self):
        if len(self.queue) != 0:
            item = self.queue[0]
            self.queue = self.queue[1:]
        else :
            item = None
        return item

queue = my_queue(queue)

time = 0
finish_task = []

while(True):
    item = queue.dequeue()
    if item == None:
        break
    elif item[1] <= q:
        time += item[1]
        finish_task.append([item[0], time])
    else :
        time += q
        queue.enqueue([item[0], item[1] - q])

for item in finish_task:
    print(item[0], item[1])

### FIXED CODE ###

n, q = map(int , input().split())
queue = []
for _ in range(n):
    line = input().split()
    queue.append([line[0], int(line[1])])

class my_queue:
    def __init__(self, queue):
        self.queue = queue
    def enqueue(self, item):
        self.queue = self.queue + [item]
        self.queue.append(item)
    def dequeue(self):
        if len(self.queue) != 0:
            item = self.queue[0]
            self.queue = self.queue[1:]
        else :
            item = None
        return item


time = 0
finish_task = []

while(len(queue) != 0):
    item = queue.pop(0)
    if item == None:
        break
    elif item[1] <= q:
        time += item[1]
        finish_task.append([item[0], time])
    else :
        time += q
        queue.append([item[0], item[1] - q])

for item in finish_task:
    print(item[0], item[1])
### BUGGY CODE ###

import copy

n = int(input())
a_list = []

for i in range(n):
    a_list.append(int(input()))

is_max = False
#print(a_list)    
for i in range(n):
    if not is_max:
        a_copy = copy.copy(a_list)
        a_copy.pop(i)
        #print(a_copy)
        a_max = max(a_copy)
        print(a_max)
        if a_list[i] == a_max:
            is_max = True
    else:
        print(a_max)

### FIXED CODE ###


n = int(input())
a = []

for i in range(n):
    a.append(int(input()))
    
a_copy = list(a)
a.sort(reverse=True)

max_1 = a[0]
max_count = a.count(max_1)

for i in a:
    if i < max_1:
        max_2 = i
        break
        
for a in a_copy:
    if a == max_1 and max_count == 1:
        print(max_2)
    else:
        print(max_1)
### BUGGY CODE ###

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

MOD = 10**9 + 7
import itertools

ans = 0
Cmb = itertools.combinations(A, 2)
for cmb in Cmb:
    ans += cmb[0]*cmb[1]%MOD
print(ans%MOD)

### FIXED CODE ###

MOD = 10**9 + 7
n = int(input())
A = list(map(int, input().split()))

import numpy
A_CS = numpy.cumsum(sorted(A, reverse=True))
A_CS = A_CS%MOD

A = sorted(A)

#print(A_CS)
ans = 0
for i in range(n-1):
#    print(A[i], A_CS[-i-2])
#    A_CS -= A[i]
    ans = (ans+(A[i]*A_CS[-i-2])%MOD)%MOD
print(ans)
### BUGGY CODE ###

N, M, K = map(int, input().split())
ans = [0] * (N + 1)
child = {i:[] for i in range(1, N+1)}
group_id = {i:[] for i in range(1, N+1)}
group = {}

for _ in range(M):
    a, b = map(int, input().split())
    child[a].append(b)
    child[b].append(a)
    
X = set(range(1, N+1))
g = 0
while X:
    i = X.pop()
    group_id[i] = g
    x = {i}
    ch = set(child[i])
    while ch:
        c = ch.pop()
        group_id[c] = g
        ch = (ch | set(child[c])) - set(x)
        x = x | {c}
    group[g] = x
    X = X - set(x)
    g += 1

for i in range(1, N+1):
    ans[i] = len(group[group_id[i]]) - len(child[i]) - 1

for _ in range(K):
    c, d = map(int, input().split())
    if group_id[c] == group_id[d]:
        ans[c] -= 1
        ans[d] -= 1

print(*ans[1:])

### FIXED CODE ###

N, M, K = map(int, input().split())
ans = [0] * (N + 1)
child = {i:[] for i in range(1, N+1)}
isChecked = [0] * (N + 1)
isBlocked = [set() for _ in range(N+1)]

for _ in range(M):
    a, b = map(int, input().split())
    child[a].append(b)
    child[b].append(a)
    
for _ in range(K):
    c, d = map(int, input().split())
    isBlocked[c].add(d)
    isBlocked[d].add(c)
    
for i in range(1, N+1):
    if isChecked[i]:
        continue
    isChecked[i] = 1
    x = set([i])
    ch = child[i].copy()
    while ch:
        c = ch.pop()
        if isChecked[c]:
            continue
        isChecked[c] = 1
        x.add(c)
        ch.extend(child[c])
    for j in x:
        ans[j] = len(x) - 1 - len(child[j]) - len(x & isBlocked[j])
        
print(*ans[1:])
### BUGGY CODE ###

if __name__ == '__main__':
    import sys
    input = sys.stdin.readline


    nn = int(input())
    x = input()

    ans = 0
    def make_divisors(n):
        divisors = []
        for i in range(1, int(n**0.5)+1):
            if n % i == 0:
                if i % 2 != 0:
                    divisors.append(i)
                if i != n // i and (n//i)%2 != 0:
                    divisors.append(n//i)

        divisors.sort(reverse = True)
        return divisors

    fact_n = make_divisors(nn)

    used = {}

    ans = 0

    for i in fact_n:
        num_len = int(nn/i)
        base = x[:num_len]
        can_divide = int(base, 2)+1
        for key, value in used.items():
            if num_len%key == 0:
                can_divide -= value
        if int(x) < int((base + format(2**num_len-1-int(base,2), 'b'))*int((i-1)/2)+base):
            can_divide -= 1
        used[num_len] = can_divide
        ans += can_divide*num_len*2
        ans = ans%998244353


    print(ans%998244353)


### FIXED CODE ###

if __name__ == '__main__':
    import sys
    input = sys.stdin.readline


    nn = int(input())
    x = input()

    ans = 0
    def make_divisors(n):
        divisors = []
        for i in range(1, int(n**0.5)+1):
            if n % i == 0:
                if i % 2 != 0:
                    divisors.append(i)
                if i != n // i and (n//i)%2 != 0:
                    divisors.append(n//i)

        divisors.sort(reverse = True)
        return divisors

    fact_n = make_divisors(nn)

    used = {}

    ans = 0

    for i in fact_n:
        num_len = int(nn/i)
        base = x[:num_len]
        can_divide = int(base, 2)+1
        for key, value in used.items():
            if num_len%key == 0:
                can_divide -= value
        if x < (base + format(2**num_len-1-int(base,2), 'b').zfill(num_len))*int((i-1)/2)+base:
            can_divide -= 1
        used[num_len] = can_divide
        ans += can_divide*num_len*2
        ans = ans%998244353


    print(ans%998244353)
### BUGGY CODE ###

def factorize(x):
    f = {}
    if x%2==0: f[2]=0
    while x%2==0:
        f[2]+=1
        x//=2
    
    p = 3
    while x>=p*p:
        if x%p==0: f[p]=0
        while x%p==0:
            f[p]+=1
            x//=p
        p+=2
    if x>1: f[x]=1
    return f

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

F = {0:0}

for a in A:
    for f in set(factorize(a)):
        if f not in F: F[f] = 0
        F[f] += 1

m = max(F.values())
if m<=1:
    print('pairwise coprime')
elif m<N:
    print('setwise coprime')
else:
    print('not coprime')


### FIXED CODE ###

def factorize(x):
    f = set()
    if x%2==0: f.add(2)
    while x%2==0: x//=2
    
    p = 3
    while x>=p*p:
        if x%p==0: f.add(p)
        while x%p==0: x//=p
        p+=2
    if x>1: f.add(x)
    return f

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

F = [0]*(10**6+5)

for a in A:
    for f in factorize(a):
        F[f] += 1

m = max(F)
if m<=1:
    print('pairwise coprime')
elif m<N:
    print('setwise coprime')
else:
    print('not coprime')
### BUGGY CODE ###

import sys
readline = sys.stdin.readline

N = int(readline())
A = list(map(int,readline().split()))
DIV = 10 ** 9 + 7

# 各桁の1と0の数を数えて、((1の数) * (0の数)) * (2 ** i桁目(1桁目を0とする))を数える

K = max(A) # 最も大きい数が0になるまでループ
rate = 1
ans = 0
j = 0
while K:
  one = 0
  zero = 0
  for i in range(len(A)):
    if (A[i] >> j) & 1:
      one += 1
    else:
      zero += 1
  ans += one * zero * rate
  ans %= DIV
  rate *= 2
  j += 1
  K >>= 1
  
print(ans)

### FIXED CODE ###

n=int(input())
A=list(map(int,input().split()))
binA=[None]*n
for i in range(n):
  binA[i]=bin(A[i])[2:]
  
keta=[0]*60
for i in range(n):
  tar=binA[i]
  for j in range(len(tar)-1,-1,-1):
    if tar[j]=="1":
      keta[len(tar)-1-j]+=1

pair=[0]*60
for i in range(len(keta)):
  pair[i]=keta[i]*(len(A)-keta[i])
  
ans=0
mul=1
DIV=10**9+7
for i in range(len(pair)):
  ans+=pair[i]*mul
  ans%=DIV
  mul*=2
print(ans)
### BUGGY CODE ###

sentence_len, request_len = map(int, input().split())
sentence = input()
answer_table = [0 for i in range(sentence_len)]
for i in range(sentence_len-1):
    if sentence[i] == 'A' and sentence[i+1] == 'C':
        answer_table[i+1] = 1

for i in range(request_len):
    start, end = map(int, input().split())
    start, end = start-1, end
    answer = sum(answer_table[start:end])
    if sentence[start] == 'C':
        answer -= 1
    print(answer)


### FIXED CODE ###

from itertools import accumulate

N, Q = map(int, input().split())
S = input()

t = [0] * N

for i in range(1, N):
    s = S[i - 1:i + 1]
    if s == 'AC':
        t[i] += 1

T = list(accumulate(t))

for i in range(Q):
    l, r = map(int, input().split())
    print(T[r - 1] - T[l - 1])
### BUGGY CODE ###

n,k=map(int,input().split())
x=[int(i) for i in input().split()]
l,r=[],[]
for i in range(n):
    if x[i]<0 and i+1<n: l.append(abs(x[i]-min(x[i+1],0)))
    elif x[i]<0: l.append(-x[i])
    if x[i]>=0 and i>0: r.append(x[i]-max(0,x[i-1]))
    elif x[i]>=0: r.append(x[i])

l=l[::-1]
l=l[:k]
r=r[:k]
ll=len(l)
rr=len(r)

ans=sum(l)*2+sum(r)*2
for i in range(ll):
    if k-1==i: ans=min(ans,sum(l))
    elif i+rr+1>=k: ans=min(ans,sum(l[:i+1])*2+sum(r[:k-i-1]))

for i in range(rr):
    if k-1==i: ans=min(ans,sum(r))
    elif i+ll+1>=k: ans=min(ans,sum(r[:i+1])*2+sum(l[:k-i-1]))
print(ans)


### FIXED CODE ###

n,k=map(int,input().split())
x=[int(i) for i in input().split()]
l,r=[],[]
for i in range(n):
    if x[i]<0 and i+1<n: l.append(abs(x[i]-min(x[i+1],0)))
    elif x[i]<0: l.append(-x[i])
    if x[i]>=0 and i>0: r.append(x[i]-max(0,x[i-1]))
    elif x[i]>=0: r.append(x[i])

l=l[::-1]
l=l[:k]
r=r[:k]

ll=len(l)
rr=len(r)
lx=sum(l)
rx=sum(r)

if ll<k: lx=lx*2+sum(r[:(k-ll)])
if rr<k: rx=rx*2+sum(l[:(k-rr)])
ans=min(lx,rx)
o=[ll,[0,k-ll][ll<k]]
p=[rr,[0,k-rr][rr<k]]


for i in range(1,k):
    if o[1]==0: lx*=2
    if p[1]==0: rx*=2
    if o[1]+1<=rr and o[0]>0:
        lx-=l[o[0]-1]*2
        lx+=r[o[1]]
        o[0]-=1
        o[1]+=1
        ans=min(ans,lx)
    if p[1]+1<=ll and p[0]>0:
        rx-=r[p[0]-1]*2
        rx+=l[p[1]]
        p[0]-=1
        p[1]+=1
        ans=min(ans,rx)
print(ans)
### BUGGY CODE ###

n=int(input())
a=[]
ans=[]
for i in range(n):
    b=input()
    a+=[b]
for i in range(n):
    if a[i] in ans:
        ans.remove(a[i])
    else:
        ans.append(a[i])
print(len(ans))

### FIXED CODE ###

n=int(input())
ans=set([])
for i in range(n):
    a=int(input())
    if a in ans:
        ans.remove(a)
    else:
        ans.add(a)
print(len(ans))
### BUGGY CODE ###

import numpy as np
import math

n, k = map(int, input().split())
arr = np.array(list(map(int, input().split())))
cut_num_arr = [1]*n
cut_length_arr = [l for l in arr]

for i in range(k):
  max_idx = np.argmax(cut_length_arr)
  tmp = cut_num_arr[max_idx] + 1
  cut_num_arr[max_idx] = tmp
  cut_length_arr[max_idx] = arr[max_idx] / tmp

print(math.ceil(max(cut_length_arr)))


  

### FIXED CODE ###

import math
n, k = map(int, input().split())
arr = list(map(int, input().split()))

max_length = max(arr)
low = 1
high = max_length - 1

while low <= high:
  mid = (low + high) //2
  cnt = 0
  for i in range(n):
    cnt += math.ceil(arr[i]/mid) - 1
  if cnt > k:
    low = mid + 1
  else:
    high = mid -1
    max_length = mid

print(max_length)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
N = int(input())
S = input()

R = []
G = []
B = []

for i in range(N):
  if S[i] == 'R':
    R.append(i)
  elif S[i] == 'G':
    G.append(i)
  else:
    B.append(i)
    
count = 0

for r in R:
  for g in G:
    for b in B:
      S = sorted([r,g,b])
      if S[0]+S[2]!=2*S[1]:
        count += 1

print(count)

### FIXED CODE ###

# -*- coding: utf-8 -*-
N = int(input())
S = input()

R = 0
G = 0
B = 0

for i in range(N):
  if S[i] == 'R':
    R += 1
  elif S[i] == 'G':
    G += 1
  else:
    B += 1
    
count = 0
for i in range(N-1):
  for j in range(i+1,N):
    if 2*j-i>=N:
      break
    if S[i]!=S[j] and S[2*j-i]!=S[i] and S[2*j-i]!=S[j]:
      count += 1

print(R*G*B-count)