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

import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

h,w,k = map(int, input().split())
x1,y1,x2,y2 = map(int, input().split())
x1,y1,x2,y2 = x1-1,y1-1, x2-1,y2-1
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]

def make_array(dims, fill):
    if len(dims) == 1:
        return [fill]*dims[0]
    nxt = dims[1:]
    ret = [make_array(nxt,fill) for i in range(dims[0])]
    return ret

inf = float("inf")

DP = make_array((h,w,4), inf)

from collections import deque
que = deque()
for i in range(4):
    que.append((x1,y1,i,0))

dxdy = [(-1,0),(1,0),(0,1),(0,-1)]
while que:
    x,y,prev,cost = que.popleft()
    if DP[x][y][prev] <= cost:
        continue
    DP[x][y][prev] = cost
    for i in range(4):
        if i == prev:
            continue
        nprev = i^1
        ddx, ddy = dxdy[i]
        for j in range(1,k+1):
            nx, ny = x+j*ddx, y+j*ddy
            if not (0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny][nprev] > cost+1):
                break
            que.append((nx,ny,nprev,cost+1))

ans = min(DP[x2][y2])
if ans == inf:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

h,w,k = map(int, input().split())
sx,sy,gx,gy = map(lambda x: int(x)-1, input().split())
board = [list(map(lambda x: int(x=="."), input())) for i in range(h)]
inf = 1<<30
DP = [[inf for j in range(w)] for i in range(h)]
DP[sx][sy] = 0

from collections import deque
que = deque([(sx,sy)])

dxdy = [(0,1),(1,0),(0,-1),(-1,0)]
while que:
    x,y = que.popleft()
    for dx,dy in dxdy:
        nx,ny = x,y
        for i in range(k):
            nx += dx
            ny += dy
            if not(0<=nx<h and 0<=ny<w and board[nx][ny] and DP[nx][ny] >= DP[x][y]+1):
                break
            # コストが変わらない場合は無視か...
            if DP[nx][ny] == DP[x][y]+1:
                continue
            DP[nx][ny] = DP[x][y]+1
            que.append((nx,ny))
ans = DP[gx][gy]
if ans == inf:
    ans = -1
print(ans)
### BUGGY CODE ###

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

dp = [0] * (W + 1)
#for n in range(N):
#	w, v = WV[n]
for w, v in WV:
	for sumw in range(W, w - 1, -1):
		#dp[sumw] = max([dp[sumw], dp[sumw - w] + v])
		if dp[sumw] < dp[sumw - w] + v:
			dp[sumw] = dp[sumw - w] + v
	#for x in dp:
	#	print("{:4d}".format(x), end="")
	#print()
print(max(dp))


### FIXED CODE ###

def dp():
  N, W = map(int, input().split())
  WV = (map(int, input().split()) for _ in range(N))

  DP = [0] * (W+1)
  for w, v in WV:
    for j in range(W, w-1, -1):
      dp = DP[j-w] + v 
      if DP[j] < dp:
        DP[j] = dp
  return max(DP)
print(dp())
### BUGGY CODE ###

import numpy as np

n, k = map(int, input().split())
P = np.array(input().split(), dtype=int)
template = np.ones(k)

a = np.convolve(P, template, mode="valid")

print((max(a) + k) / 2)

### FIXED CODE ###

import numpy as np
from scipy.signal import fftconvolve

n, k = map(int, input().split())
P = np.array(input().split(), dtype=int)
template = np.ones(k)

a = fftconvolve(P, template, mode="valid")

print((max(a) + k) / 2)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
h,w = map(int,input().split())
a = []
for i in range(h):
  s = list(map(int,input().split()))
  a.append(s)
b = []
for i in range(h):
  t = list(map(int,input().split()))
  b.append(t)

dp = [[[False]*6321 for i in range(w)] for _ in range(h)]
dp[0][0][abs(a[0][0]-b[0][0])] = True

for i in range(h):
  for j in range(w):
    for k in range(6321):
      if dp[i][j][k]:
        if i < h-1:
          c = abs(a[i+1][j]-b[i+1][j])
          if k+c < 6321:
            dp[i+1][j][k+c] = True
          dp[i+1][j][abs(k-c)] = True
        if j < w-1:
          d = abs(a[i][j+1]-b[i][j+1])
          if k+d < 6321:
            dp[i][j+1][k+d] = True
          dp[i][j+1][abs(k-d)] = True
          
for i in range(6321):
  if dp[h-1][w-1][i]:
    print(i)
    break

### FIXED CODE ###

def main():  
  import sys
  read = sys.stdin.buffer.read
  readline = sys.stdin.buffer.readline
  readlines = sys.stdin.buffer.readlines

  h,w = map(int,readline().split())
  a = []
  for i in range(h):
    s = list(map(int,readline().split()))
    a.append(s)
  b = []
  for i in range(h):
    t = list(map(int,readline().split()))
    b.append(t)
  C = [[0]*w for _ in range(h)]
  for i in range(h):
      for j in range(w):
          C[i][j] = abs(a[i][j] - b[i][j])

  dp = [[[0]*6321 for i in range(w)] for _ in range(h)]
  dp[0][0][C[0][0]] = 1

  for i in range(h):
    for j in range(w):
      for k in range(6321):
        if dp[i][j][k]:
          if i < h-1:
            c = C[i+1][j]
            if k+c < 6321:
              dp[i+1][j][k+c] = 1
            dp[i+1][j][abs(k-c)] = 1
          if j < w-1:
            d = C[i][j+1]
            if k+d < 6321:
              dp[i][j+1][k+d] = 1
            dp[i][j+1][abs(k-d)] = 1

  for i in range(6321):
    if dp[h-1][w-1][i]:
      print(i)
      break
      
if __name__ == "__main__":
    main()
### BUGGY CODE ###

A, B, Q = map(int, input().split())
ls = list(int(input()) for i in range(A))
lt = list(int(input()) for i in range(B))
lx = list(int(input()) for i in range(Q))

def go(x, list, N):
  clist = list.copy()
  for i in range(N):
    clist[i] = abs(clist[i] - x)
  return min(clist)

for x in lx:
  l = []
  for i in range(A):
    if x - ls[i] < 0:
      break
  if i:
    l.append(abs(ls[i-1]-x) + go(ls[i-1], lt, B))
  if i < A:
    l.append(abs(ls[i]-x) + go(ls[i], lt, B))
  for i in range(B):
    if x - lt[i] < 0:
      break
  if i:
    l.append(abs(x-lt[i-1]) + go(lt[i-1], ls, A))
  if i < B:
    l.append(abs(lt[i]-x) + go(lt[i], ls, A))
  print(min(l))

### FIXED CODE ###

import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]

for q in range(Q):
  x = int(input())
  b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
  res = INF
  for S in [s[b-1], s[b]]:
    for T in [t[d-1], t[d]]:
      d1, d2 = abs(S-x) + abs(T-S), abs(T-x) + abs(S-T)
      res = min(res, d1, d2)
  print(res)
### BUGGY CODE ###

N, Q = map(int, input().split())
S = input()
for _ 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 ###

n=int(input())
s=[input() for _ in range(n)]
dic=[]
for i in range(n):
    if s[i] not in dic:
        dic.append(s[i])
print(len(dic))

### FIXED CODE ###

n=int(input())
dic={}
for i in range(n):
    tmp=input()
    if tmp not in dic.keys():
        dic[tmp]=1
print(len(dic))
### BUGGY CODE ###

import numpy as np
n = int(input())
A = list(map(int,input().split()))
q = int(input())
B = []
C = []
for _ in range(q):
    b,c = map(int,input().split())
    A = [c if a == b else a for a in A]
    print(np.sum(A))

### FIXED CODE ###

import numpy as np
n = int(input())
A = list(map(int,input().split()))
q = int(input())
ar = np.zeros(10**5+1)
for i in A:
    ar[i] += 1
ans = np.sum(A) 
for _ in range(q):
    b,c = map(int,input().split())
    ans = ans+ar[b]*(c-b)
    ar[c] += ar[b]
    ar[b] = 0
    print(int(ans))
### BUGGY CODE ###

#!python3

LI = lambda: list(map(int, input().split()))

# input
N = int(input())
A = [LI() for _ in range(N)]


def main():
    ans = 0
    for i in range(N):
        for j in range(N):
            flag = True
            for k in range(N):
                if i == k or j == k:
                    continue
                d = A[i][k] + A[k][j]
                if A[i][j] > d:
                    print(-1)
                    return
                if A[i][j] == d:
                    flag = False
            if flag:
                ans += A[i][j]
    ans //= 2
    print(ans)


if __name__ == "__main__":
    main()


### FIXED CODE ###

#!python3

LI = lambda: list(map(int, input().split()))

# input
N = int(input())
A = [LI() for _ in range(N)]


def main():
    ans = 0
    for i in range(N):
        for j in range(N):
            flag = True
            for k in range(N):
                if i == k or j == k:
                    continue
                d = A[i][k] + A[k][j]
                if A[i][j] > d:
                    print(-1)
                    return
                if A[i][j] == d:
                    flag = False
            if flag:
                ans += A[i][j]
    ans //= 2
    print(ans)


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

#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, M, K = map(int, readline().split())
A = list(map(int, readline().split()))[::-1]
B = list(map(int, readline().split()))[::-1]

cnt = 0
tmp = 0
res = [0]
for a in A:
    if sum(res) + a <= K:
        res.append(a)
        cnt += 1
    else:
        break
for b in B:
    if sum(res) + b <= K:
        res.append(b)
        cnt += 1
    else:
        break
ans = cnt

cnt = 0
tmp = 0

for b in B:
    if tmp + b <= K:
        tmp += b
        cnt += 1
    else:
        break
for a in A:
    if tmp + a <= K:
        tmp += a
        cnt += 1
    else:
        break
ans = max(ans, cnt)

tot = 0
cnt = 0

while True:
    if len(A) > 0:
        acc_a = A.pop()
    else:
        acc_a = sys.maxsize
    if len(B) > 0:
        acc_b = B.pop()
    else:
        acc_b = sys.maxsize
    if acc_a == acc_b == sys.maxsize:
        break
    acc = min(acc_a, acc_b)

    if tot + acc <= K:
        cnt += 1
    else:
        break

    tot += acc
    if acc_a <= acc_b:
        B.append(acc_b)
    else:
        A.append(acc_a)

ans = max(ans, cnt)

print(ans)


### FIXED CODE ###

#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, M, K = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))

A_cum = [0]
B_cum = [0]

for i in range(N):
    A_cum.append(A_cum[i] + A[i])
for i in range(M):
    B_cum.append(B_cum[i] + B[i])

cnt, j = 0, M

for i in range(N + 1):
    if A_cum[i] > K:
        break
    while B_cum[j] > K - A_cum[i]:
        j -= 1
    cnt = max(cnt, i + j)

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

#156D

def comb(n,r,p=7+10**9):
  bunshi = 1
  for i in range(n-r+1, n+1):
    bunshi =bunshi * i % p
  bunbo = 1
  for i in range(1, r+1):
    bunbo = bunbo * i % p
  return bunshi * pow(bunbo, p-2, p)
n,a,b=map(int,input().split())
s=-1+2**n-comb(n,a)-comb(n,b)
print(s%(7+10**9))


### FIXED CODE ###

#156D
p=7+10**9
def comb(n,r):
  bunshi = 1
  for i in range(n-r+1, n+1):
    bunshi =bunshi * i % p
  bunbo = 1
  for i in range(1, r+1):
    bunbo = bunbo * i % p
  return bunshi * pow(bunbo, p-2, p)
n,a,b=map(int,input().split())
s=-1+pow(2,n,p)-comb(n,a)-comb(n,b)
print(s%p)
### BUGGY CODE ###

s = input()
r = 0
n = len(s)
for i in range(n):
    for k in range(i + 3, n + 1):
        if i + 3 <= n:
            if int(s[i:k]) % 2019 == 0:
                r += 1
print(r)


### FIXED CODE ###

S = input()
M = 2019
dp = [0] * M
dp[0] = 1
t = 0
for i, s in enumerate(S[::-1]):
    t = (t + int(s) * (pow(10, i, M))) % M
    dp[t] += 1

r = 0
for d in dp:
    r += d * (d - 1) // 2

print(r)
### BUGGY CODE ###

n = int(input())
a = input().split()
b = []
for i in range(n):
  if i % 2 == 0:
    b = b + a[i:i+1]
  else:
    b = a[i:i+1] + b
if n % 2 == 1:
  b = b[::-1]
print(" ".join(b))


### FIXED CODE ###

n = int(input())
a = input().split()
b = []
for i in range(n - 1, -1, -2):
  b.extend([a[i]])
for j in range(n % 2, n, 2):
  b.extend([a[j]])
print(" ".join(b))
### BUGGY CODE ###


def main():
    t = input()
    if t == "1":
        print(1)
        exit(0)
    li = [int(v) for v in input().split(' ')]
    s_li = sorted(li)
    count = 0
    for v in range(s_li[0] - 1, s_li[-1] + 1):
        t_c = 0
        for vv in s_li:
            if v == vv or v - 1 == vv or v + 1 == vv:
                t_c += 1
            if vv > v + 2:
                break
        if t_c > count:
            count = t_c

    print(count)



if __name__ == '__main__':
    main()

### FIXED CODE ###

n = int(input())
cnt = [0] * 100002
a = list(map(int, input().split()))
for v in a:
    cnt[v] += 1
    cnt[v+1] += 1
    cnt[v+2] += 1
    
print(max(cnt))
### BUGGY CODE ###

import math
from copy import deepcopy
import sys
from itertools import combinations_with_replacement
def input():
    return sys.stdin.readline()[:-1]
# input
N, M = map(int, input().split())
As = list(map(int, input().split()))
ind = [i for i in range(N)]

min_val = None
for combo in combinations_with_replacement(ind, M):
    price = deepcopy(As)
    for i in combo:
        price[i] = math.floor(price[i]/2)
    if min_val is None:
        min_val = sum(price)
    else:
        if sum(price) <= min_val:
            min_val = sum(price)

print(min_val)

### FIXED CODE ###

import heapq
import sys
def input():
    return sys.stdin.readline()[:-1]

# input
N, M = map(int, input().split())
a = []

for i in input().split():
    heapq.heappush(a, -int(i))

for j in range(M):
    highest = heapq.heappop(a)
    heapq.heappush(a, -(- highest // 2))

print(-sum(a))
### BUGGY CODE ###

P=998244353
MAX_N=3*10**5+1
def egcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)
def inv(x):
    return egcd(x,P)[0]
Fact=[0 for i in range(MAX_N+1)]
Finv=[0 for i in range(MAX_N+1)]
Fact[0]=1
Finv[0]=1
for i in range(MAX_N):
    Fact[i+1]=(Fact[i]*(i+1))%P
    Finv[i+1]=inv(Fact[i+1])%P
def C(n,k):
    return (Fact[n]*(Finv[k]*Finv[n-k])%P)%P
N,A,B,K=map(int,input().split())
L=[]
for i in range(K//A+1):
    if (K-A*i)%B==0:
        j=(K-A*i)//B
        if 0<=i<=N and 0<=j<=N:
            L.append((i,j))
ans=0
for x,y in L:
    ans+=C(N,x)*C(N,y)
    ans%=P
print(ans)



### FIXED CODE ###

N,A,B,K=map(int,input().split())
P=998244353
def egcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)
def inv(x):
    return egcd(x,P)[0]
Fact=[0 for i in range(N+1)]
Finv=[0 for i in range(N+1)]
Fact[0]=1
Finv[0]=1
for i in range(1,N+1):
    Fact[i]=(i*Fact[i-1])%P
    Finv[i]=(Finv[i-1]*inv(i))%P
def NC(k):
    tmp=(Finv[k]*Finv[N-k])%P
    return (Fact[N]*tmp)%P
ans=0
for s in range(N+1):
    t=(K-s*A)//B
    if s*A+t*B==K and 0<=t<=N:
        ans+=(NC(s)*NC(t))%P
print(ans%P)
### BUGGY CODE ###

#解説読み 2次元dp
mod = 10**9 + 7
n,m = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
tmp = [1 for _ in range(m+1)]
dp = [tmp]
for ni in range(n):
  last = tmp
  tmp = [1]
  for mi in range(m):
    if A[ni] != B[mi]:
      x = tmp[-1]+last[mi+1]-last[mi]
    else:
      x = tmp[-1]+last[mi+1]
    x %= mod
    tmp.append(x)
  dp.append(tmp)
print(dp[-1][-1])

### FIXED CODE ###

#15:35
h,w = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
mod = 10 ** 9 + 7
now = [1 for _ in range(w+1)]
for i in range(h):
  last = now
  now = [1]
  for j in range(w):
    if a[i] == b[j]:
      now.append((last[j+1]+now[-1])%mod)
    else:
      now.append((last[j+1]+now[-1]-last[j])%mod)
  #print(now)
print(now[-1])
### BUGGY CODE ###

N, K, Q = map(int, input().split())
ans_r = [K] * N
cor = list(map(int, [input() for i in range(Q)]))
for i in cor:
    ans_r[i - 1] += 1
    ans_r = list(map(lambda x: x - 1, ans_r))
ans_r = list(map(lambda x: "No" if x <= 0 else "Yes", ans_r))
for i in ans_r:
    print(i)


### FIXED CODE ###

N, K, Q = map(int, input().split())
ans_r = [K - Q] * N
cor = list(map(int, [input() for i in range(Q)]))
for i in cor:
    ans_r[i - 1] += 1
for i in ans_r:
    if i <= 0:
        print("No")
    else:
        print("Yes")
### BUGGY CODE ###

n,q = list(map(int, input().split()))
s = input()
s_lis = list(s)
for _ in range(q):
    count = 0
    l,r=list(map(int, input().split()))
    for i in range(l-1,r-1):
        #print(s_lis[i])
        if s_lis[i] =='A' and s_lis[i+1]=='C':
            count += 1
    print(count)

### FIXED CODE ###


n,q = list(map(int, input().split()))
s = input()
ans = [0]*(n+1)
lr=[list(map(int, input().split())) for _ in range(q)]
for i in range(1,n):
    
    if s[i-1]=='A' and s[i]=='C':
        ans[i+1] = ans[i] + 1
    else:
        ans[i+1]= ans[i]
#print(ans)
for i in range(q):
    print(ans[lr[i][1]]-ans[lr[i][0]])
### BUGGY CODE ###

import sys
class Bit:
  def __init__(self,n):
    self.size = n
    self.tree = [0]*(n+1)
 
  def sum(self,i):
    s = 0
    while i > 0:
      s += self.tree[i]
      i -= i & -i
    return s

  def add(self,i,x):
    while i <= self.size:
      self.tree[i] += x
      i += i & -i
input = sys.stdin.readline
mod = 998244353
ptable = [1,2]
for i in range(20):
  ptable.append(ptable[-1]**2%mod)
def p2(x):
  if x == 0:
    return 1
  ret = 1
  for i in range(20):
    if x&1<<i:
      ret = (ret*ptable[i+1])%mod
  return ret
n = int(input())
xy = [list(map(int,input().split())) for i in range(n)]
xls,yls = map(list,list(zip(*xy)))
xi = [(xls[i],i) for i in range(n)]
yi = [(yls[i],i) for i in range(n)]
xi.sort()
yi.sort()
xd = [0 for i in range(n)]
yd = [0 for i in range(n)]
for i in range(n):
  x,ix = xi[i]
  y,iy = yi[i]
  xd[ix] = i
  yd[iy] = i
ans = 0
bit = Bit(n+1)
xyd = list(zip(xd,yd))
xyd.sort()
for i in range(n):
  x,y = xyd[i]
  upper = n-y-1
  right = n-x-1
  ld = bit.sum(y+1)
  ru = upper+right+ld-n+1
  rd = right-ru
  lu = upper-ru
  ans += ((p2(lu)-1)*(p2(rd)-1)*(p2(ld+ru))+(p2(ld)-1)*(p2(ru)-1)*(p2(lu+rd))+
  p2(n-1)-(p2(lu)-1)*(p2(rd)-1)*(p2(ld)-1)*(p2(ru)-1))
  ans %= mod
  bit.add(y+1,1)
print(ans)



### FIXED CODE ###

import sys
class Bit:
  def __init__(self,n):
    self.size = n
    self.tree = [0]*(n+1)
 
  def sum(self,i):
    s = 0
    while i > 0:
      s += self.tree[i]
      i -= i & -i
    return s

  def add(self,i,x):
    while i <= self.size:
      self.tree[i] += x
      i += i & -i
input = sys.stdin.readline
from collections import defaultdict
mod = 998244353
n = int(input())
p2t = [1]
for i in range(n):
  p2t.append(p2t[-1]*2%mod)
dcy = defaultdict(int)
xy = [list(map(int,input().split())) for i in range(n)]
xy.sort()
yls = list(zip(*xy))[1]
yls = list(yls)
for i in range(n):
  dcy[yls[i]] = i
yls.sort()
yind = [0 for i in range(n)]
for i in range(n):
  ind = dcy[yls[i]]
  yind[ind] = i
xyd = []
for i in range(n):
  xyd.append((i,yind[i]))
ans = 0
bit = Bit(n+1)
for i in range(n):
  x,y = xyd[i]
  upper = n-y-1
  right = n-x-1
  ld = bit.sum(y+1)
  ru = upper+right+ld-n+1
  rd = right-ru
  lu = upper-ru
  p2lu = p2t[lu]
  p2rd = p2t[rd]
  p2ru = p2t[ru]
  p2ld = p2t[ld]
  ans += ((p2lu-1)*(p2rd-1)*(p2t[ld+ru])%mod+(p2ld-1)*(p2ru-1)*(p2t[lu+rd])%mod+
  p2t[n-1]-(p2lu-1)*(p2rd-1)*(p2ld-1)*(p2ru-1)%mod)
  ans %= mod
  bit.add(y+1,1)
print(ans)
### BUGGY CODE ###

N = int(input())
D = [int(input()) for i in range(N)]
a = D[:]
for i in range(N):
    a[i] = 0
    print(max(a))
    a = D[:]

### FIXED CODE ###

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

M = max(d)
D = d[:]
d.remove(M)
m = max(d)

for j in range(N):
    if D[j]==M:
        print(m)
    else:
        print(M)
### BUGGY CODE ###

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

D = [ [int(i) for i in input().split()] for _ in range(C) ] 
c = [ [int(i) for i in input().split()] for _ in range(N) ] 

costD = [0 for _ in range(C)]

for c0 in range(C):
    tmpD = [0] * 3
    for i in range(N):
        for j in range(N):
            tmpD[(i+j)%3] += D[c[i][j]-1][c0]
    costD[c0] = tmpD

kohoD = []

for c1 in range(C):
    for c2 in range(C):
        if c1 == c2:
            continue
        for c3 in range(C):
            if c1 == c3 or c2 == c3:
                continue
            kohoD += [(costD[c1][0] + costD[c2][1] + costD[c3][2])]

print(min(kohoD))


### FIXED CODE ###

import itertools
 
 
def main():
    N, C = map(int, input().split())
    D = [list(map(int, input().split())) for _ in range(C)]
    CC = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]
 
    cost = [[0 for _ in range(C)] for _ in range(3)]
    for c in range(C):
        for i in range(N):
            for j in range(N):
                cost[(i + j) % 3][c] += D[CC[i][j]][c]
 
    res = float("inf")
    color = [i for i in range(C)]
    for i, c in enumerate(list(itertools.permutations(color, 3))):
        tmp = 0
        for it in range(3):
            tmp += cost[it][c[it]]
        res = min(res, tmp)
 
    print(res)
 
main()
### BUGGY CODE ###

import sys
N,M=map(int,input().split())

graph=[]
for _ in range(N+1):
  graph.append([])

uvlist = []
for i in range(M):
  u,v=map(int,input().split())
  graph[u].append(v)
  
S,T=map(int,input().split())

#print(graph)

vlist=graph[S]
t=0
while(True):
  t+=1
  if t>10**6:
    print(-1)
    sys.exit()
  
  nlist = []
  for v in vlist:
    if v == T and t%3 == 0:
      print(t//3)
      sys.exit()
    else:
      for vv in graph[v]:
        nlist.append(vv)
      
  #print((t,nlist))
  vlist = nlist
  

### FIXED CODE ###

import sys
N,M=map(int,input().split())

graph=[]
for _ in range(N+1):
  graph.append([])

uvlist = []
for i in range(M):
  u,v=map(int,input().split())
  graph[u].append(v)
  
S,T=map(int,input().split())

#print(graph)
table=[]
for i in range(N+1):
  table.append([False]*3)
#print(table)  
  
vlist=graph[S]
table[S][0]=True
t=0
while(True):
  t+=1
  #if t>300003:
  #  print(-1)
  #  sys.exit()
  
  #nlist = []
  nset = set()
  for v in vlist:
    if v == T and t%3 == 0:
      print(t//3)
      sys.exit()
    else:
      table[v][t%3]=True
      for vv in graph[v]:
        if not table[vv][(t+1)%3]:
          #nlist.append(vv)
          nset.add(vv)
          #table[vv][t%3]=True
          #print((vv,t%3))
      
  #print((t,nlist))
  #vlist = nlist
  vlist = list(nset)
  if len(vlist) == 0:
    print(-1)
    break
### BUGGY CODE ###

import math

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

mod = int(math.pow(10, 9) + 7)

sum = 0
for i in range(N - 1):
    Ai = An[i]
    for j in range(i + 1, N):
        Aj = An[j]
        sum += (Ai * Aj) % mod


print(sum % mod)


### FIXED CODE ###

import math

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


mod = int(math.pow(10, 9) + 7)

sum_an = sum(An)
sum_result = 0

for i in range(N - 1):
    Ai = An[i]
    sum_an -= Ai
    sum_result += (Ai * sum_an) % mod


print(sum_result % mod)
### BUGGY CODE ###

N,Q = map(int,input().split())
S = input()
c = [0]*N
for i in range(N-1):
    if S[i] == "A" and S[i+1] == "C":
        c[i] = 1
for i in range(Q):
    l,r = map(int,input().split())
    ans = c[l-1:r-1]
    print(ans.count(1))

### FIXED CODE ###

N,Q = map(int,input().split())
S = input()
c = []
count = 0
for i in range(N-1):
    if S[i] == "A" and S[i+1] == "C":
        count += 1
    c.append(count)
for i in range(Q):
    l,r = map(int,input().split())
    if l == 1:
        print(c[r-2]-0)
    else:
        print(c[r-2]-c[l-2])
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
total = sum(a)
ret = []
for i in range(n):
    s = 0
    for j in range(n // 2):
        s += a[(i + j * 2 + 1) % n] * 2
    ret.append(total - s)
print(" ".join(map(str, ret)))

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
s = 0
for j in range(n // 2):
    s += a[(j * 2 + 1) % n] * 2
ret = []
ret.append(sum(a) - s)
for i in range(0, n - 1):
    ret.append((a[i] - ret[-1] // 2) * 2)
print(" ".join(map(str, ret)))