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

from collections import deque, defaultdict
from time import time

N = int(input())
A = [deque(map(lambda a: int(a) - 1, input().split())) for _ in range(N)]
S = time()

canBattle = defaultdict(lambda: False)
D = [0] * N

while True:
    isChanged = False
    for i, a in enumerate(A):
        if not a:
            continue
        j = a[0]
        canBattle[(i, j)] = True

        if canBattle[(j, i)]:
            isChanged = True
            A[i].popleft()
            A[j].popleft()
            d = max(D[i], D[j])
            D[i], D[j] = d + 1, d + 1
    if all(len(a) == 0 for a in A):
        print(max(D))
        exit()
    if time() - S >= 1.800:
        print(N * (N - 1) // 2)
        exit()
    if not isChanged:
        break

print(-1)


### FIXED CODE ###

import sys
from heapq import heappop, heappush
from operator import itemgetter
from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7
INF = float('inf')


def sol():
    N = int(input())
    A = [deque()] + [deque(map(int, input().split())) for _ in range(N)]

    days = [0] * (N + 1)
    now = [0] * (N + 1)

    que = deque([i + 1 for i in range(N)])

    while que:
        i = que.popleft()
        if not A[i]:
            continue

        a = A[i].popleft()
        if now[a] == i:
            days[a] = days[i] = max(days[a], days[i]) + 1
            que.append(i)
            que.append(a)
            now[a] = now[i] = 0
        else:
            now[i] = a

    for i in range(1, N + 1):
        if A[i]:
            print(-1)
            return
    print(max(days))


sol()
### BUGGY CODE ###

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

for i in range(1,n+1): #1~nについて
    b[i-1] = a.index(i) + 1

print(*b)

### FIXED CODE ###

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

for i in range(n):
    b[a[i]-1] = i+1

print(*b)
### BUGGY CODE ###

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

for i in range(n):
    if k-(q-a.count(i+1)) > 0:
        print("Yes")
    else:
        print("No")


### FIXED CODE ###

n,k,q=map(int, input().split())
a=[0]*n
for _ in range(q):
    a[int(input())-1]+=1

for i in range(n):
    if k-(q-a[i]) > 0:
        print("Yes")
    else:
        print("No")
### BUGGY CODE ###

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

ans = 0
for c in C:
    for b in B:
        if b >= c: break
            
        for a in A:
            if a >= b: break
            ans += 1
print(ans)

### FIXED CODE ###

import bisect
N = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

ans = 0
for b in B:
    i_a = bisect.bisect_left(A, b)
    i_c = bisect.bisect_right(C, b)
    ans += i_a * (N-i_c)
print(ans)
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
sum = 0
# temp1 = 0
# temp2 = 0
MOD = 1000000007
# for i in range(0, N-1):
#     A[i] = A[i] % MOD
#print(A)
for i in range(0, N-1):
    A[i] = A[i] % MOD
    for j in range(i+1, N):
        #print(i, j)
        # temp1 = 0
        # temp2 = 0
        temp3 = 0
        # temp1 = A[i] % MOD
        # temp2 = A[j] % MOD
        # temp3 = temp1 * temp2
        temp3 = (A[i] * A[j]) % MOD
        sum = (sum + temp3) % MOD
#sum = sum % MOD
print(sum)

### FIXED CODE ###

MOD = 1000000007
N = int(input())
A = list(map(int, input().split()))
ans = 0
B = [0]*(N-1)
sumA = sum(A)
for i in range(1, N):
    sumA -= A[i-1]
    B[i-1] = sumA
for i in range(0, N-1):
    ans += A[i] * B[i]
    ans %= MOD
print(ans)
### BUGGY CODE ###

h,w = map(int,input().split())
al = []
for _ in range(h):
    a = list(map(int,input().split()))
    al.append(a)

bl = []
for _ in range(h):
    b = list(map(int,input().split()))
    bl.append(b)

abl = [[0 for _ in range(w)] for _ in range(h)]
for i in range(h):
    for j in range(w):
        abl[i][j] = al[i][j] - bl[i][j]

from collections import defaultdict
dic = defaultdict(set)

dic[(0,0)].add(abl[0][0])
dic[(0,0)].add(-abl[0][0])
for i in range(h):
    for j in range(w):
        for v in dic[(i,j)]:
            if j + 1 < w:
                dic[(i,j+1)].add(v+abl[i][j+1])
                dic[(i,j+1)].add(v-abl[i][j+1])
            if i + 1 < h:
                dic[(i+1,j)].add(v+abl[i+1][j])
                dic[(i+1,j)].add(v-abl[i+1][j])

ans = float("inf")
for i in dic[(h-1, w-1)]:
    ans = min(abs(i), ans)
print(ans)




### FIXED CODE ###

h,w = map(int,input().split())

bitdp = [[0 for j in range(w)] for i in range(h)]
al = []
bl = []
for _ in range(h):
   a = list(map(int,input().split()))
   al.append(a)
for _ in range(h):
   b = list(map(int,input().split()))
   bl.append(b)
abl = [[] for i in range(h)]
for i in range(h):
   for j in range(w):
      abl[i].append(abs(al[i][j] - bl[i][j]))

bitdp[0][0] = 1 << h*w+abl[0][0]
for i in range(h):
   for j in range(w):
      if j != w-1:
         bitdp[i][j+1] |= bitdp[i][j] << abl[i][j+1] | bitdp[i][j] >> abl[i][j+1]
      if i != h-1:
         bitdp[i+1][j] |= bitdp[i][j] << abl[i+1][j] | bitdp[i][j] >> abl[i+1][j]

ans = []
for i in range(h*w*2+1):
   if (bitdp[-1][-1] >> i & 1):
      ans.append(abs(h*w-i))

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

s  = input()
q  = int(input())
inv = 1
sfb = [0,"",""]
for i in range(q):
    ls = list(input().split())
    if ls[0] == "1":
        inv *=-1
    else:
        f,c = ls[1:]
        if f == "1":
            sfb[inv] +=c
        else:
            sfb[-inv] +=c

ans = sfb[1][::-1] + s + sfb[-1]
print(ans[::inv])


### FIXED CODE ###

s  = input()
q  = int(input())
inv = 1
sfb = [0,"",""]
for i in range(q):
    ls = list(input().split())
    if ls[0] == "1":
        inv *=-1
    else:
        f,c = ls[1:]
        if f == "1":
            sfb[inv] +=c
        else:
            sfb[-inv] +=c

ans = sfb[1][::-1] + s + sfb[-1]
print(ans[::inv])
### BUGGY CODE ###

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

for i in range(K):
    if A==0:
        B-=1
    else:
        A-=1
    
print(A,B)

### FIXED CODE ###

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

if A+B <= K:
    print(0,0)
else:
    if A >= K:
        print(A-K,B)
    else:
        print(0,B-(K-A))
### BUGGY CODE ###

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

diff = N-K
for i in range(N-diff, N):
    mul = 1
    if 0 in A[i-K:i]:
        print("No")
        continue
    for j in range(i-K, i):
        mul*=A[j]
    if mul<mul*A[i]/A[i-K]:
        print("Yes")
    else:
        print("No")


### FIXED CODE ###

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

for i in range(K, N):
    if As[i]/As[i-K]>1:
        print("Yes")
    else:
        print("No")
### BUGGY CODE ###

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

for i in range(Q):
    l, r = map(int, input().split())
    print(S[l - 1:r].count("AC"))

### FIXED CODE ###

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

t = [0] * (N + 1)

for i in range(N):
    t[i + 1] = t[i] + (1 if S[i:i + 2] == "AC" else 0)

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

from math import gcd


def factorize(n):
    b = 2
    fct = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            fct.append(b)
        b = b + 1
    if n > 1:
        fct.append(n)
    return set(fct)


n = int(input())
a = list(map(int, input().split()))
b = a[0]
for i in range(1, n):
    b = gcd(b, a[i])
    if b == 1:
        break
else:
    print("not coprime")
    exit()

a = list(map(factorize, a))
wa = a[0]
cnt = 0
flag = 0
for i in range(1, n):
    if len(wa & a[i]) > 0 and flag == 0:
        flag = 1
    wa = wa | a[i]

if flag:
    print("setwise coprime")
else:
    print("pairwise coprime")



### FIXED CODE ###

import math


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return set(a)


n = int(input())
a = list(map(int, input().split()))
num = a[0]
for i in range(1, n):
    num = math.gcd(num, a[i])
if num != 1:
    print("not coprime")
    exit()
prime_memo = {}
for i in range(n):
    prime_number = prime_factorize(a[i])
    for num in prime_number:
        if num not in prime_memo.keys():
            prime_memo[num] = 1
        else:
            print("setwise coprime")
            exit()
print("pairwise coprime")
### BUGGY CODE ###

from collections import deque
H,W = map(int,input().split())
S = [input().strip() for _ in range(H)]
cmax = 0
for i in range(H):
    for j in range(W):
            for k in range(H):
                for l in range(W):
                    if (i,j)!=(k,l) and S[i][j]=="." and S[k][l]==".":
                        que = deque([(i,j,0)])
                        hist = {(n,m):0 for n in range(H) for m in range(W)}
                        hist[(i,j)] = 1
                        while que:
                            x = que.popleft()
                            n,m,c = x[0],x[1],x[2]
                            if n==k and m==l:
                                cmax = max(cmax,c)
                                break
                            if m+1<W and S[n][m+1]=="." and hist[(n,m+1)]==0:
                                que.append((n,m+1,c+1))
                                hist[(n,m+1)] = 1
                            if m-1>-1 and S[n][m-1]=="." and hist[(n,m-1)]==0:
                                que.append((n,m-1,c+1))
                                hist[(n,m-1)] = 1
                            if n+1<H and S[n+1][m]=="." and hist[(n+1,m)]==0:
                                que.append((n+1,m,c+1))
                                hist[(n+1,m)] = 1
                            if n-1>-1 and S[n-1][m]=="." and hist[(n-1,m)]==0:
                                que.append((n-1,m,c+1))
                                hist[(n-1,m)] = 1
print(cmax)

### FIXED CODE ###

import heapq
INFTY = 10**3
H,W = map(int,input().split())
S = [input().strip() for _ in range(H)]
dist = {(i,j):{(k,l):INFTY for k in range(H) for l in range(W)} for j in range(W) for i in range(H)}
for i in range(H):
    for j in range(W):
        if S[i][j]==".":
            hist = {(k,l):0 for k in range(H) for l in range(W)}
            heap = [(0,i,j)]
            dist[(i,j)][(i,j)]= 0
            hist[(i,j)] = 1
            while heap:
                d,k,l = heapq.heappop(heap)
                if d>dist[(i,j)][(k,l)]:continue
                hist[(k,l)] = 1
                if l+1<W and S[k][l+1]=="." and hist[(k,l+1)]==0:
                    if dist[(i,j)][(k,l+1)]>d+1:
                        dist[(i,j)][(k,l+1)] = d+1
                        heapq.heappush(heap,(d+1,k,l+1))
                if l-1>-1 and S[k][l-1]=="." and hist[(k,l-1)]==0:
                    if dist[(i,j)][(k,l-1)]>d+1:
                        dist[(i,j)][(k,l-1)]=d+1
                        heapq.heappush(heap,(d+1,k,l-1))
                if k+1<H and S[k+1][l]=="." and hist[(k+1,l)]==0:
                    if dist[(i,j)][(k+1,l)]>d+1:
                        dist[(i,j)][(k+1,l)]=d+1
                        heapq.heappush(heap,(d+1,k+1,l))
                if k-1>-1 and S[k-1][l]=="." and hist[(k-1,l)]==0:
                    if dist[(i,j)][(k-1,l)]>d+1:
                        dist[(i,j)][(k-1,l)]=d+1
                        heapq.heappush(heap,(d+1,k-1,l))
dmax = 0
for i in range(H):
    for j in range(W):
        for k in range(H):
            for l in range(W):
                if dist[(i,j)][(k,l)]<INFTY:
                    dmax = max(dmax,dist[(i,j)][(k,l)])
print(dmax)
### BUGGY CODE ###

#085 C
N,Y =map(int,input().split())
for i in range(N+1):
    for j in range(N+1):
        for k in range(N-(i+j)+1):
            if i * 10000 + j * 5000 + k *1000 ==Y and (i+j+k)==N:
                print(i,j,k)
                exit()
print(-1,-1,-1)

### FIXED CODE ###

#085 C

N,Y = map(int, input().split())
A =10000
B =5000
C =1000

for i in range((N)+1):
    for j in range(N+1-i):
        k = N-(i+j)
        if i * A + j * B + k * C == Y and k >= 0:
            print(str(i)+" "+str(j)+" "+str(k))
            exit()
                
print("-1 -1 -1")
### BUGGY CODE ###

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

ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N):
  a = S[i]
  for j in range(i+1,N):
    b = S[j]
    if (i+j)%2 :
      continue
    k = (i+j)//2
    if sorted([a,S[k],b]) == ['B','G','R']:
      ans -= 1
      
print(ans)

### FIXED CODE ###

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

ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N):
  for j in range(i+1,N):
    if (i+j)%2 :
      continue
    k = (i+j)//2
    if len(set([S[i],S[k],S[j]])) == 3:
      ans -= 1
      
print(ans)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(2000)
n, m = map(int, input().split())
bridge = list(map(int, sys.stdin.read().split()))
total = n*(n-1)//2
ans =[total]
u = [-1]*(n+1)
size = [1]*(n+1)
rank = [1]*(n+1)
def check(x, y):
    return find(x) != find(y)

def find(x):
    if u[x] < 0 :
        u[x] = x
        return x
    else:
        return u[x]

for x , y in zip(bridge[-2::-2],bridge[::-2]):
    if check(x, y):
        X = find(x)
        Y = find(y)
        total -= size[X] * size[Y]
        ans += [total]
        if rank[X] == rank[Y]:
            u = [Y if x == X else x for x in u]
            rank =[x+1 if u[i] == Y else x for i, x in enumerate(rank)]
        else:
            if rank[X] > rank[Y]:
                X, Y = Y, X
            rank =[x+1 if u[i] == X else x for i, x in enumerate(rank)]
            u = [Y if x == X else x for x in u]
        size[Y] += size[X]

    else:
        ans += [total]

print(*ans[-2::-1], sep="\n")

### FIXED CODE ###

import sys
n , m, *e = map(int,sys.stdin.read().split())
total = n*(n-1)//2
ans =[total]
u = [-1]*(n+1)

def find(x):
    if u[x] < 0 :
        return x
    else:
        return find(u[x])

for x , y in zip(e[-2:1:-2],e[:1:-2]):
    x = find(x)
    y = find(y)
    x, y = sorted((x, y))
    if x != y:
        total -= u[x] * u[y]
        u[x] += u[y]
        u[y] = x
    ans += [total]

print(*ans[::-1], sep="\n")
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
MOD = 1000000007
ans = 0

for i in range(N):
  A[i] = A[i] % MOD

for i in range(N):
  for j in range(N-i-1):
    ans = (ans + A[i] * A[j+i+1]) % MOD

print(ans)


### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
MOD = 1000000007
x = sum(A)
ans = 0
 
for i in range(N):
  x -= A[i]
  y = x % MOD
  z = (A[i] * y) % MOD
  ans += z
  ans %= MOD

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

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

result = 0
for i in range(int(N)-1):
    result+=As[i]*sum(As[i+1:])
print(result%(1*10**9+7))


### FIXED CODE ###

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

result = 0
before_sum = sum(As)
for i in range(N-1):
    before_sum-=As[i]
    result+=As[i]*(before_sum)
print(result%(1*10**9+7))
### BUGGY CODE ###

K=int(input())
if K%2==0:
	print(-1)
else:
	seven=0
	m=1
	count=0
	while m!=0:
		seven=seven*10+7
		m=seven % K
		count+=1

	print(count)

### FIXED CODE ###

K=int(input())
m=0
count=1
chk=0
for i in range(K):
	m=(m*10+7)%K
	if m==0:
		print(count)
		chk=1
		break
	count+=1

if chk==0:
	print(-1)
### BUGGY CODE ###

n=int(input())
s=input()
l=set()
for i in range(0,n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            m=s[i]+s[j]+s[k]
            l.add(m)
print(len(l))


### FIXED CODE ###

n=int(input())
s=input()
l=set()
r=set()

for i in range(0,n-2):
    a=set()
    if s[i] in r:
        continue
    else:
        r.add(s[i])
    for j in range(i+1,n-1):
        if s[j] in a:
            continue
        else:
            a.add(s[j])
        for k in range(j+1,n):
            if j==1 and k>=3 and s[k]==s[2]:
                continue
            m=s[i]+s[j]+s[k]
            l.add(m)
print(len(l))
### BUGGY CODE ###

n=int(input())
li=list(map(int,input().split()))
li.sort()
ans=1
for i in li:
    ans*=i
    if ans>1000000000000000000:
        exit
    else:
        pass
    
if ans>1000000000000000000:
    print(-1)
else:
    print(ans)

### FIXED CODE ###

import sys
n=int(input())
li=list(map(int,input().split()))
li.sort()
if li[0]==0:
  print(0)
  sys.exit()
else:
  ans=1
  for i in li:
    ans*=i
    if ans>1000000000000000000:
        print(-1)
        sys.exit()
    else:
        continue
    
  if ans>1000000000000000000:
      print(-1)
  else:
      print(ans)
### BUGGY CODE ###

def main():
  #1:初期化
  N, K = map(int,input().split())
  h = list(map(int, input().split()))
  #print(h,H)
  inf = float("inf")
  dp = [inf]*(N)

  #2:初期値
  dp[0] = 0

  #3:dp実行部(配るDP)
  for i in range(N):
      for k in range(1,min(N-i-1,K)+1):
          dp[i+k] = min(dp[i+k], dp[i] + abs(h[i] - h[i+k]))
          #print(dp,i,k)

  #4:出力
  print(dp[N-1])

if __name__ == '__main__':
    main()


### FIXED CODE ###

# -*- coding: utf-8 -*- 
def cost(h,N,K):
    dp=[0]*N
    dp[1]=abs(h[0]-h[1])
    for i in range(2,N):
        tmp=[abs(h[i]-h[j])+dp[j] for j in range(max(0,i-K),i)]
        dp[i]=min(tmp)
    return dp[-1]

N,K=map(int,input().split())
h=[int(num) for num in input().split()]

print(cost(h,N,K))
### BUGGY CODE ###

n = int(input())
p = list(map(int, input().split()))
b =0
for i in range(n):
  if p[i] ==min(p[0:i+1]):
    b += 1
print(b)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
ans = 1
c = a[0]
for i in range(1, n):
  if min(c,a[i])== a[i]:
    c = a[i]
    ans += 1
print(ans)
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
ans = 0
MOD = 10 ** 9 + 7
A_bin = list(map(lambda x: bin(x)[2:].zfill(60), A))
one = [0] * 60

for num in A_bin:
    for digit in range(60):
        one[digit] += int(num[-(digit + 1)])

for digit in range(60):
    ans += (one[digit] * (N - one[digit]) * pow(2, digit, MOD)) % MOD
    ans %= MOD

print(ans)


### FIXED CODE ###

import numpy as np

N = int(input())
A = np.array(list(map(int, input().split())))
ans = 0
MOD = 10 ** 9 + 7

for digit in range(60):
    one = np.count_nonzero((A >> digit) & 1)
    ans += (one * (N - one) * pow(2, digit, MOD)) % MOD
    ans %= MOD

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

from collections import deque
import sys
input = sys.stdin.readline

H, W = list(map(int, input().split()))
S = [input() for _ in range(H)]

ans = 0

d = ([0, 1], [0, -1], [1, 0], [-1, 0])


def bfs(x, y):
    m = [[-1] * W for _ in range(H)]

    q = deque()
    q.append([y, x, 0])
    global ans

    while q:
        j, i, v = q.popleft()
        m[j][i] = v
        for d in ((j, i + 1), (j, i-1), (j+1, i), (j-1, i)):
            dj = d[0]
            di = d[1]
            if 0 <= dj < H and 0 <= di < W and m[dj][di] == -1 and S[dj][di] == ".":
                q.append([dj, di, v+1])
                if ans < v+1:
                    ans = v+1


for j in range(H):
    for i in range(W):
        if S[j][i] != ".":
            continue
        bfs(i, j)
print(ans)


### FIXED CODE ###

from collections import deque
import sys
input = sys.stdin.readline

H, W = list(map(int, input().split()))
S = ["#" * (W+2)] + ["#" + input().strip() +
                     "#" for _ in range(H)] + ["#" * (W+2)]

ans = 0

for j in range(1, H+1):
    for i in range(1, W+1):
        if S[j][i] != ".":
            continue
        m = [[-1] * (W+2) for _ in range(H+2)]

        q = deque()
        q.append((j, i, 0))
        m[j][i] = 0

        while q:
            y, x, v = q.popleft()
            if ans < v:
                ans = v
            for dj, di in ((y, x+1), (y, x-1), (y+1, x), (y-1, x)):
                if S[dj][di] == "#" or m[dj][di] != -1:
                    continue
                q.append((dj, di, v+1))
                m[dj][di] = v+1
print(ans)
### BUGGY CODE ###

R, G, B, N = map(int, input().split())
ans = 0
for r in range(N+1):
    if N < R*r:
        break
    for g in range(N+1):
        if N < R*r + G*g:
            break
        if (N - R*r - G*g) % B == 0:
            ans += 1
print(ans)

### FIXED CODE ###

R, G, B, N = map(int, input().split())
ans = 0
for r in range(N+1):
    for g in range(N+1):
        s = N - R*r - G*g
        if s >= 0 and s % B == 0:
            ans += 1
print(ans)
### BUGGY CODE ###

import sys
n = int(sys.stdin.readline())
S = sys.stdin.readline().rstrip()
f = False
for l in range(n//2, 0, -1):
    for j in range(n):
        ptn = S[j:j+l]
        if len(ptn) != l:
            break
        #print(ptn)
        if ptn in S[j+l:]:
            print(l)
            f = True
            break
    if f == True:
        break
else:
    print(0)

### FIXED CODE ###

import sys
n = int(sys.stdin.readline())
S = sys.stdin.readline().rstrip()


dp = [[0 for i in range(n)] for j in range(n)]
#初期条件
for j in range(1, n):
    if S[0] == S[j]:
        dp[0][j] = 1

#print(dp)

for i in range(1, n-1):
    for j in range(i+1, n):
        if S[i] ==S[j]:
            dp[i][j] = min(dp[i-1][j-1] +1, j-i)

maxp = 0
for i in range(n-1):
    for j in range(i+1, n):
        if maxp < dp[i][j]:
            maxp = dp[i][j]
#print(dp)
print(maxp)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)

def judge(c,l,r,counter):
  if counter == 0:
    return c
  elif len(c) == 0:
    return c
  else:
    return judge(list(filter(lambda x: l[0]<=x<=r[0],c)),l[1:],r[1:],counter-1)

def solve(c,l,r,m):
  return len(judge(c,l,r,m))
  
N,M = map(int,input().split())
L = [0]*M
R = [0]*M
for i in range(M):
  L[i],R[i] = map(int,input().split())
cards = range(1,N+1)
print(solve(cards,L,R,M))


### FIXED CODE ###

def solve(l,r):
  l_max = max(l)
  r_min = min(r)
  return max(r_min - l_max + 1, 0)
  
N,M = map(int,input().split())
L = [0]*M
R = [0]*M
for i in range(M):
  L[i],R[i] = map(int,input().split())
print(solve(L,R))
### BUGGY CODE ###

import numpy as np
N = int(input())
A = list(map(int, input().split()))

left = [[0 for _ in range(N)] for _ in range(N)]
right = [[] for _ in range(N)]
for i in range(N):
  right[i].append(A[i])
  for j in range(N):
    if j == i or j == (i + 1) % N:
      left[i][j] = 0.5

A = np.matrix(left)
Y = np.matrix(right)
ans = np.dot(np.linalg.inv(A), Y)

for i in range(N):
  print(int(round(ans[i,0])), end = "")
  if i == N - 1:
    print()
  else:
    print(" ", end = "")

### FIXED CODE ###

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

ans = 0
for i in range(N):
  tmp = ans
  if i == 0:
    for j in range(N):
      if j % 2 == 0:
        ans += A[j]
      else:
        ans -= A[j]
  else:
    ans = 2 * A[i - 1] - tmp

  tmp = ans
  print(ans, end = "")
  if i != N - 1:
    print(" ", end = "")
  else:
    print()