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

import numpy as np
import sys

N = int(input())
As = [list(map(int,input().split())) for i in range(N)]
As = np.array(As)-1


current_waitings = As[:,0]
game_counter = np.zeros(N).astype('int')

day = 0
completed_players = []
day_player = []
for x in range(N*(N-1)//2):
    day_player = []
    for i,e1 in enumerate(current_waitings):
        if i in completed_players:
            continue
        if current_waitings[e1] == i and e1 not in day_player and i not in day_player:
            day_player.append(i)
            day_player.append(e1)
            if game_counter[i] == N-2:
                completed_players.append(i)
            if game_counter[i] <= N-3:
                game_counter[i] += 1
            if game_counter[e1] == N-2:
                completed_players.append(e1)
            if game_counter[e1] <= N-3:
                game_counter[e1] += 1
            current_waitings[i] = As[i][game_counter[i]]
            current_waitings[e1] = As[e1][game_counter[e1]]
        if len(completed_players) == N:
            print(day+1)
            sys.exit()
    day += 1
print(-1)



### FIXED CODE ###

from collections import deque
def solve():
    n = int(input())
    a = []
    for i in range(n):
        ai = [int(j)-1 for j in input().split()]
        a.append(deque(ai))
    d = set()
    q = deque()
    for i in range(n):
        ai = a[i].popleft()
        if i < ai:
            q.append((i, ai))
        else:
            q.append((ai, i))
    ans = 0
    while q:
        ans += 1
        nxt_q = deque()
        while q:
            i, ai = q.pop()
            if (i, ai) in d:
                if a[i]:
                    ni = a[i].popleft()
                    if i < ni:
                        nxt_q.append((i, ni))
                    else:
                        nxt_q.append((ni, i))
                if a[ai]:
                    ni = a[ai].popleft()
                    if ai < ni:
                        nxt_q.append((ai, ni))
                    else:
                        nxt_q.append((ni, ai))
            else:
                d.add((i, ai))
        q = nxt_q
    if any(i for i in a):
        ans = -1
    print(ans)

solve()
### BUGGY CODE ###

p = 10**9+7
def pow(x,m):
    if m==0:
        return 1
    if m==1:
        return x
    if m%2==0:
        return (pow(x,m//2)**2)%13
    else:
        return (x*(pow(x,(m-1)//2)**2)%13)%13
S = input().strip()
n = len(S)
dp = [[0 for _ in range(13)] for _ in range(n+1)]
dp[0][0] = 1
for i in range(n):
    for j in range(13):
        if S[i]!="?":
            a = int(S[i])
            b = (a*pow(10,n-1-i))%13
            dp[i+1][j] = dp[i][(j-b)%13]
        else:
            for k in range(10):
                b = (k*pow(10,n-1-i))%13
                dp[i+1][j] = (dp[i+1][j]+dp[i][(j-b)%13])%p
print(dp[n][5])

### FIXED CODE ###

p = 10**9+7
S = input().strip()
n = len(S)
dp = [[0 for _ in range(13)] for _ in range(n+1)]
dp[0][0] = 1
for i in range(n):
    for j in range(13):
        if S[i]!="?":
            dp[i+1][(10*j+int(S[i]))%13] = dp[i][j]
        else:
            for k in range(10):
                dp[i+1][(10*j+k)%13] = (dp[i+1][(10*j+k)%13]+dp[i][j])%p
print(dp[n][5])
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
a.sort()
i=n-1
res=1
z=10**18
while(i>=0):
    res*=a[i]
    if(a[i]>z):
        break
    i-=1
if(res>z):
    res=-1
print(res)

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
a.sort()
res=1
z=10**18
for i in a:
    if(i==0):
        res=0
        break
i=n-1
while(i>=0):
    if(a[i]==0):
        res=0
        break
    if(res>z/a[i]):
        res=z+1
        break
    res*=a[i]
    if(a[i]>z):
        break
    i-=1
if(res>z):
    res=-1
print(res)
### BUGGY CODE ###

n = int(input())
li = list(map(int,input().split()))
li.sort()
for i in range(1,n+1):
    print(li.count(i))
    if i in li:
        li.remove(i)

### FIXED CODE ###

n = int(input())
li = list(map(int,input().split()))
li.sort(reverse = True)
count = 0
for i in range(1,n+1):
    count = 0
    while True:
        if li:
            if li[-1]==i:
                count+=1
                li.pop()
            else:
                break
        else:
            break
    print(count)
### BUGGY CODE ###

N = int(input())
P = [int(k) for k in input().split()]

result = 0
for i in range(N):
    for j in range(0,i):
        # j < i
        if P[i] > P[j]:
            result = result - 1
            break

print(N+result)


### FIXED CODE ###

N = int(input())
P = [int(k) for k in input().split()]

maxes = []
mins  = []

max = 0
min = 0
result = 0

for i in range(N):
    if i == 0:
        max = P[i]
        min = P[i]
    else:
        if(max < P[i]):
            max = P[i]
        if(min > P[i]):
            min = P[i]

    maxes.append(max)
    mins.append(min)

for i in range(N):
    if mins[i] == P[i]:
        result = result + 1

print(result)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
N = int(input())
P_list = list(map(int,input().split()))
i_num = 0
for i in range(N):
    now_num = P_list[i]
    new_list = P_list[0:i+1]
    if now_num <= min(new_list):
        i_num += 1
    elif now_num > min(new_list):
        pass
print(i_num)


### FIXED CODE ###

N = int(input())
P_list = list(map(int,input().split()))
i_num = 0
min_P = P_list[0]
for p in P_list:
    if p <= min_P:
        min_P = p
        i_num += 1
print(i_num)
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
mod=10**9+7
from math import gcd
ans=a[0]
for i in range(1,n):
  ans=ans*a[i]//gcd(ans,a[i])
s=0
for j in range(n):
  s+=(ans//a[j])%mod
print(s%mod)


### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
mod=10**9+7
from math import gcd
ans=a[0]
for i in range(1,n):
  ans=ans*a[i]//gcd(ans,a[i])
s=sum(pow(x,mod-2,mod) for x in a)
print(s*ans%mod)
### BUGGY CODE ###

# seishin.py
import sys
sys.setrecursionlimit(10**6)
N = int(input())
*P, = map(int, input().split())
MOD = 10**9 + 7
from itertools import tee

G = [[] for i in range(N+1)]
for i, p in enumerate(P):
    G[p].append(i+1)
Q = [None]*(N+1)
E = [1, 1, 0, 1]
def gen(i):
    *I, = map(Q.__getitem__, G[i])
    k = len(G[i])
    r = 1, k, pow(2, k, MOD) - k - 1, k
    while r:
        a,b,c,d=r
        yield (a+c,b,0,d)
        r = None
        for it in I:
            s = next(it, None)
            if s:
                if r:
                    a0, a1, a2, c0 = s
                    b0, b1, b2, c1 = r
                    r = a0*b0 % MOD, (a0*b1 + a1*b0) % MOD, ((a0 + a1)*b2 + a1*b1) % MOD, c0+c1
                else:
                    r = s
*Q, = map(gen, range(N+1))
ans = pow(2, N, MOD)
for a0, a1, a2, c in Q[0]:
    ans += pow(2, N+1-c, MOD) * a1 % MOD
print(ans % MOD)

### FIXED CODE ###

# seishin.py
from collections import deque
N = int(input())
*P, = map(int, input().split())
MOD = 10**9 + 7

G = [[] for i in range(N+1)]
U = [0]*(N+1)
C = [0]*(N+1)
for i, p in enumerate(P):
    G[p].append(i+1)
    U[i+1] = u = U[p]+1
    C[u] += 1
Q = [None]*(N+1)
PP = {}
def pp(k):
    if k not in PP:
        PP[k] = p = pow(2, k, MOD)
        return p
    return PP[k]
L = [0]*(N+1)

ept = []
sz = L.__getitem__
for i in range(N, -1, -1):
    g = G[i]
    if not g:
        continue

    # 子ノードのdequeを集める
    g.sort(key=sz, reverse=1)
    k = len(g)
    e = [pp(k) - k, k, 0]

    g0 = g[0]
    L[i] = L[g0] + 1
    if L[g0] == 0:
        Q[i] = deque([e])
        continue
    Q[i] = R = Q[g0]
    if k > 1:
        # a0 <- a2
        for s, r in zip(Q[g[1]] or ept, R):
            r[0] += r[2]; r[2] = 0

        for j in g[1:]:
            S = Q[j]
            if not S:
                break

            # dequeの小さい方から大きい方へマージする処理
            for (a0, a1, a2), r in zip(S, R):
                b0, b1, b2 = r; a0 += a2
                r[0] = a0*b0 % MOD
                r[1] = (a0*b1 + a1*b0) % MOD
                r[2] = ((a0+a1)*b2 + a1*b1) % MOD
    R.appendleft(e)
print((pp(N) + sum(pp(N+1-c) * a1 % MOD for (a0, a1, a2), c in zip(Q[0], C[1:]))) % MOD)
### BUGGY CODE ###

def IsPrime(n):
    '''

    :param n:??£??´??°
    :return: ?´???°?????????True, ????????????????????°?????????False
    ???????????§???????´???°??? 6k-1????????? 6k+1??§??¨?????????????????????k?????£??´??°
    '''
    if n == 2 or n == 3 or n== 5: return True
    if n == 1 or n%2 == 0 or n%3 == 0:return False
    d, l = 5, int(n**(1/2))
    while d <= l:
        if n%d == 0 or n%(d+2):return False
    return True
c = 0
for i in range(int(input())):
    if IsPrime(int(input())): c += 1
print(c)

### FIXED CODE ###

def IsPrime(n):
    '''

    :param n:??£??´??°
    :return: ?´???°?????????True, ????????????????????°?????????False
    ???????????§???????´???°??? 6k-1????????? 6k+1??§??¨?????????????????????k?????£??´??°
    '''
    if n == 2 or n == 3 or n== 5: return True
    if n == 1 or n%2 == 0 or n%3 == 0:return False
    d, l = 5, int(n**(1/2))
    while d <= l:
        if n%d == 0 or n%(d+2) == 0:return False
        d += 6
    return True
c = 0
for i in range(int(input())):
    if IsPrime(int(input())): c += 1
print(c)
### BUGGY CODE ###

import math
n = int(input())

for i in range(1, n+1):
  count = 0
  rout = int(math.sqrt(i))
  for x in range(1, rout+1):
    for y in range(1, x+1):
      for z in range(1, y+1):
        if (x+y)**2 + (y+z)**2 + (z+x)**2 == 2*i:
          if x == y and y == z and z == x:
          	count += 1
          else:
            count += 3
            
  print(count)

### FIXED CODE ###

import sys
input = sys.stdin.readline

n = int(input())
dp = [0]*(n+1)

for x in range(1, 100):
  for y in range(1, 100):
    for z in range(1, 100):
      tmp = x*x + y*y + z*z + x*y + y*z + z*x
      if tmp <= n:
        dp[tmp] += 1

for i in range(1, n+1):
  print(dp[i])
### BUGGY CODE ###

N = int(input())
s = [''.join(sorted(input())) for i in range(N)]
ns = []
ans = 0
for i in range(len(s)):
    if ns.count(s[i]) == 0:
        aas = s.count(s[i])
        if aas > 0:
            ns.append(s[i])
        for j in range(1, aas):
            ans += j
print(ans)

### FIXED CODE ###

from collections import Counter

N = int(input())
s = Counter()
for i in range(N):
    s[''.join(sorted(input()))] += 1
ans = 0
for kw, count in s.most_common():
    for i in range(1, int(count)):
        ans += i
    if int(count) < 2:
        break
print(ans)
### BUGGY CODE ###

N = int(input())

list = []
for i in range(1, 101):
    for j in range(1, 101):
        for k in range(1, 101):
            list.append(i**2 + j**2 + k**2 +i*j + j*k + k*i)

for i in range(N):
    print(list.count(i+1))


### FIXED CODE ###

N = int(input())

list = [0] * 10000
for i in range(1, 101):
    for j in range(1, 101):
        for k in range(1, 101):
            v = i**2 + j**2 + k**2 +i*j + j*k + k*i
            if v <= 10000:
                list[v-1] += 1

for i in range(N):
    print(list[i])
### BUGGY CODE ###

N,M=map(int,input().split())
def bikkuri(A):
    ans=1
    for i in range(A):
        ans*=i+1
    ans%=1000000007
    return ans

if N==M:
    p=2*(bikkuri(N)*bikkuri(M)%1000000007)
    p%=1000000007
elif  abs(N-M)==1:
    p=(bikkuri(M)*bikkuri(N)%1000000007)
    p%=1000000007
else:
    p=0
print(p)


### FIXED CODE ###

N,M=map(int,input().split())
def bikkuri(A):
    ans=1
    for i in range(A):
        ans*=i+1
        ans%=1000000007
    return ans

if N==M:
    p=2*(bikkuri(N)*bikkuri(M)%1000000007)
    p%=1000000007
elif  abs(N-M)==1:
    p=(bikkuri(M)*bikkuri(N)%1000000007)
    p%=1000000007
else:
    p=0
print(p)
### BUGGY CODE ###

s=input()
ans=0
while(1):
    for i in range(1,len(s)-1):
        if s[i-1]=="A" and s[i]=="B" and s[i+1]=="C":
            ans+=1
            s=s[:i-1]+"BCA"+s[i+2:]
    if not "ABC" in s:
        print(ans)
        exit()

### FIXED CODE ###

s=input().replace("BC","X")
ans=0
acc=0
for i in range(len(s)):
    if s[i]=="B" or s[i]=="C":
        acc=0

    elif s[i]=="A":
        acc+=1
    elif s[i]=="X":
        ans+=acc


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

N=int(input())
A=list(map(int,input().split()))
Q=int(input())
AA=[0]*(10**5+1)
for i in A:
  AA[i]+=1
for i in range(Q):
  B,C=map(int,input().split())
  AA[C]+=AA[B]
  AA[B]=0
  r=0
  for j in range(len(AA)):
    r+=(j)*AA[j]
  print(r)

### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
Q=int(input())
D=dict()
s=0
for i in A:
  s+=i
  if i in D:
    D[i]+=1
  else:
    D[i]=1
for _ in range(Q):
  b,c=map(int,input().split())
  if b in D:
    s+=(c-b)*D[b]
    if c in D:
      D[c]+=D[b]
    else:
      D[c]=D[b]
    del D[b]
  print(s)
### BUGGY CODE ###

n=int(input())
clist = list(map(int, input().split()))
clist=sorted(clist)
answer=0
kihon=2**(n-1)
tas=2**(n-2)
for i in range(n):
    answer+=clist[-i-1]*(kihon)
    kihon+=tas

answer=(answer*(2**n))%(10**9+7)
print(answer)

### FIXED CODE ###

n=int(input())
clist = list(map(int, input().split()))
clist=sorted(clist)
answer=0
kihon=(2**(n-1))%(10**9+7)
tas=(2**(n-2))%(10**9+7)
for i in range(n):
    answer+=clist[-i-1]*(kihon)
    answer=answer%(10**9+7)
    kihon+=tas

answer=(answer*(2**n))%(10**9+7)
print(answer)
### BUGGY CODE ###

import queue

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]


def bfs(gy, gx):
    while not que.empty():
        pos = que.get()
        for y, x in zip(dy, dx):
            ty = pos[0] + y
            tx = pos[1] + x
            if ty < 0 or H <= ty:
                continue
            if tx < 0 or W <= tx:
                continue
            if visited[ty][tx]:
                continue
            if S[ty][tx] == "#":
                continue
            if ty == gy and tx == gx:
                # print(ty, tx)
                # print(pos[2] + 1)
                return pos[2] + 1
            que.put((ty, tx, pos[2] + 1))
            visited[ty][tx] = True
    return -1

H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append([c for c in input()])
ans = 0
for sy in range(H):
    for sx in range(W):
        for gy in range(H):
            for gx in range(W):
                if sy == gy and sx == gx:
                    continue
                if S[sy][sx] == "#" or S[gy][gx] == "#":
                    continue
                visited = [[False for _ in range(W)] for _ in range(H)]
                visited[sy][sx] = True
                que = queue.Queue()
                que.put((sy, sx, 0))
                ans = max(ans, bfs(gy, gx))
print(ans)

### FIXED CODE ###

import queue

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]


def bfs():
    max_dist = 0
    while not que.empty():
        pos = que.get()
        for y, x in zip(dy, dx):
            ty = pos[0] + y
            tx = pos[1] + x
            if ty < 0 or H <= ty:
                continue
            if tx < 0 or W <= tx:
                continue
            if visited[ty][tx]:
                continue
            if S[ty][tx] == "#":
                continue
            que.put((ty, tx, pos[2] + 1))
            visited[ty][tx] = True
            max_dist = max(max_dist, pos[2] + 1)
    return max_dist


H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append([c for c in input()])
ans = 0
for sy in range(H):
    for sx in range(W):
        if S[sy][sx] == "#":
            continue
        visited = [[False for _ in range(W)] for _ in range(H)]
        visited[sy][sx] = True
        que = queue.Queue()
        que.put((sy, sx, 0))
        ans = max(ans, bfs())
print(ans)
### BUGGY CODE ###

N, M = map(int, input().split())
ab = [sorted(list(map(int, input().split()))) for _ in range(M)]

fst = []

for i in ab:
  if i[0]==1:
    fst.append(i[1])

for i in ab:
  for j in fst:
    if set(i) == set([j, N]):
      print("POSSIBLE")
      exit()
print("IMPOSSIBLE")
  

### FIXED CODE ###

N, M = map(int, input().split())
ab = [sorted(list(map(int, input().split()))) for _ in range(M)]

fst = []
scnd = {}

for i in ab:
  if i[0]==1:
    fst.append(i[1])
  if N in i:
    idx = i.index(N)
    scnd[i[1-idx]] = 0

#print(scnd,fst)
    
for j in fst:
  if j in scnd:
    print("POSSIBLE")
    exit()
print("IMPOSSIBLE")
### BUGGY CODE ###

import math
from fractions import gcd
A,B,N=map(int,input().split())
D=int(A*B/math.gcd(A,B))
maxi=0
if D<N:
  for i in range(1,D+1):
    if (i%B)*A/B-(A*i)%B/B>maxi:
      maxi=(i%B)*A/B-(A*i)%B/B
else:
  for i in range(1,N+1):
    if (i%B)*A/B-(A*i)%B/B>maxi:
      maxi=(i%B)*A/B-(A*i)%B/B
print(int(maxi))

### FIXED CODE ###

import math
A,B,N=map(int,input().split())
D=int(A*B/math.gcd(A,B))
maxi=0
if B-1<=N:
  print(int((B-1)%B*A/B-(A*(B-1))%B/B))
else:
  print(int(N%B*A/B-(A*N)%B/B))
### BUGGY CODE ###

from collections import deque

H, W = [int(_) for _ in input().split()]
C = [int(_) - 1 for _ in input().split()]
D = [int(_) - 1 for _ in input().split()]

S = [input() for _ in range(H)]
V = [[-1 for w in range(W)] for h in range(H)]

V[C[0]][C[1]] = 0

pool = deque()
pool.append(tuple([0, C[0], C[1]]))
while(pool):
    c, y, x = pool.pop()
    for dx in [-2, -1, 0, 1, 2]:
        for dy in [-2, -1, 0, 1, 2]:
            if y + dy < 0 or H <= y + dy:
                continue
            if x + dx < 0 or W <= x + dx:
                continue
            if S[y+dy][x+dx] == "#":
                continue
            if c != V[y][x]:
                continue
            if (dx, dy) in [(-1,0),(1,0),(0,-1),(0,1)]:
                if V[y+dy][x+dx] == -1 or V[y+dy][x+dx] > V[y][x]:
                    V[y+dy][x+dx] = V[y][x]
                    pool.appendleft((c, y+dy, x+dx))
            elif V[y+dy][x+dx] == -1 or V[y+dy][x+dx] > V[y][x] + 1:
                V[y+dy][x+dx] = V[y][x] + 1
                pool.append((c+1, y+dy, x+dx))
print(V[D[0]][D[1]])


### FIXED CODE ###

from collections import deque

H, W = [int(_) for _ in input().split()]
C = [int(_) - 1 for _ in input().split()]
D = [int(_) - 1 for _ in input().split()]

S = [input() for _ in range(H)]
V = [[-1 for w in range(W)] for h in range(H)]

V[C[0]][C[1]] = 0

pool = deque()
pool.append(tuple([0, C[0], C[1]]))
while(pool):
    c, y, x = pool.popleft()
    if c != V[y][x]:
        continue
    for dx in [-2, -1, 0, 1, 2]:
        for dy in [-2, -1, 0, 1, 2]:
            if y + dy < 0 or H <= y + dy:
                continue
            if x + dx < 0 or W <= x + dx:
                continue
            if S[y+dy][x+dx] == "#":
                continue
            if (dx, dy) in [(-1,0),(1,0),(0,-1),(0,1)]:
                if V[y+dy][x+dx] == -1 or V[y+dy][x+dx] > V[y][x]:
                    V[y+dy][x+dx] = V[y][x]
                    pool.appendleft((c, y+dy, x+dx))
            elif V[y+dy][x+dx] == -1 or V[y+dy][x+dx] > V[y][x] + 1:
                V[y+dy][x+dx] = V[y][x] + 1
                pool.append((c+1, y+dy, x+dx))
print(V[D[0]][D[1]])
### BUGGY CODE ###

a, b, k = map(int, input().split())

int_list = [i for i in range(a, b + 1)]
out_list = sorted(int_list[:k] + int_list[-k:])

last_val = 0
for out_val in out_list:
    # Neglect duplicated value
    if last_val != out_val:
        print(out_val)
    last_val = out_val

### FIXED CODE ###

a, b, k = map(int, input().split())

lower_list = [i for i in range(a, a + k)]
upper_list = [i for i in range(b, b - k, -1)]
out_list = sorted(set(lower_list + upper_list))

for out_val in out_list:
    if a <= out_val <= b:
        print(out_val)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
if n % 2:
    if a.count(0) != 1:
        print(0)
        exit()
    for i in range(n):
        if i % 2 or i == 0:
            continue
        if a.count(i) != 2:
            print(0)
            exit()
else:
    if a.count(1) != 2:
        print(0)
        exit()
    for i in range(n):
        if i % 2 == 0 or i == 1:
            continue
        if a.count(i) != 2:
            print(0)
            exit()
print(2 ** (n // 2))

### FIXED CODE ###

import collections

n = int(input())
a = list(map(int, input().split()))
ac = collections.Counter(a)
if n % 2 == 1:
    for i in ac:
        if i == 0:
            if ac[i] != 1:
                print(0)
                exit()
        else:
            if ac[i] != 2:
                print(0)
                exit()
    print((2 ** (n // 2)) % (10 ** 9 + 7))
else:
    for i in ac:
        if i == 0:
            if ac[i] != 0:
                print(0)
                exit()
        else:
            if ac[i] != 2:
                print(0)
                exit()
    print((2 ** (n // 2)) % (10 ** 9 + 7))
### BUGGY CODE ###

import itertools
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
M_chr = []
for i in range(1,M+1):
    M_chr.append(str(i))
As = list(itertools.combinations_with_replacement(M_chr, N))
pointA_max = 0
for i, A in enumerate(As):
    pointA = 0
    for i in range(Q):
        a = abcd[i][0]
        b = abcd[i][1]
        c = abcd[i][2]
        d = abcd[i][3]
        if int(A[b-1])-int(A[a-1]) == c:
            pointA = pointA + d
    if pointA_max < pointA:
        pointA_max = pointA
print(pointA_max)


### FIXED CODE ###

import itertools
N, M, Q = map(int, input().split())
abcd = [list(map(int, input().split())) for _ in range(Q)]
M_chr = []
for i in range(1,M+1):
    M_chr.append(str(i))
As = list(itertools.combinations_with_replacement(M_chr, N))
pointA_max = 0
for i, A in enumerate(As):
    pointA = 0
    for i in range(Q):
        a = abcd[i][0]
        b = abcd[i][1]
        c = abcd[i][2]
        d = abcd[i][3]
        if int(A[b-1])-int(A[a-1]) == c:
            pointA = pointA + d
    if pointA_max < pointA:
        pointA_max = pointA
print(pointA_max)
### BUGGY CODE ###

def merge(A, left, mid, right):
	global count
	n1 = mid - left
	n2 = right - mid
	L=[]
	R=[]
	for i in range(n1):
		L.append(A[left + i])
	for i in range(n2):
		R.append(A[mid + i])
	L.append(10e10)
	R.append(10e10)
	i = 0
	j = 0
	for k in range(left , right):
		count +=1
		if L[i] <= R[j]:
			A[k] = L[i]
			i = i + 1
		else: 
 			A[k] = R[j]
 			j =	 j + 1

def mergeSort(A, left, right):
	if left+1 < right:
		mid = (left + right)//2
		mergeSort(A, left, mid)
		mergeSort(A, mid, right)
		merge(A, left, mid, right)
n = int(input())
A = list(map(int, input().split()))
count = 0
mergeSort(A, 0, n)
print(*A)
print(count)



### FIXED CODE ###

def merge(A, left, mid, right):
	global count
	n1 = mid - left
	n2 = right - mid
	L=[]
	R=[]
	
#	for i in range(n1):
#		L.append(A[left + i])
#	for i in range(n2):
#		R.append(A[mid + i])
	L=A[left:mid]
	R=A[mid:right]

	L.append(10e10)
	R.append(10e10)
	i = 0
	j = 0
	for k in range(left , right):
		count +=1
		if L[i] <= R[j]:
			A[k] = L[i]
			i = i + 1
		else: 
 			A[k] = R[j]
 			j =	 j + 1

def mergeSort(A, left, right):
	if left+1 < right:
		mid = (left + right)//2
		mergeSort(A, left, mid)
		mergeSort(A, mid, right)
		merge(A, left, mid, right)
		
n = int(input())
A = list(map(int, input().split()))
count = 0
mergeSort(A, 0, n)
print(*A)
print(count)
### BUGGY CODE ###

import sys
from numpy.linalg import solve
n = int(input())
a = list(map(int,input().split()))


left2 = [[0 for i in range(n)] for i in range(n)]

for i in range(n):
  left2[i][i] +=1
  if i == n-1:
    left2[i][0] += 1
  else:
    left2[i][i+1] +=1

right2 = a
tmparr = solve(left2, right2)

tmparr = [int(tmparr[i]*2) for i in range(n)]

print(' '.join(map(str, tmparr)))

### FIXED CODE ###

n = int(input())
dam = list(map(int,input().split()))
mountain =[0]*n
all_water = sum(dam)
def dam_water(num):
  sum_dam = 0
  for i in range(num,n,2):
    sum_dam += dam[i]
  return sum_dam

mountain[0] = all_water - 2 * (dam_water(1))

for i in range(1,n):
  mountain[i] = 2*dam[i-1] - mountain[i-1]

print(" ".join(map(str,mountain)))