Problem:


Score : 700 points

Problem Statement

Given is a sequence of N digits a_1a_2\ldots a_N, where each element is 1, 2, or 3.
Let x_{i,j} defined as follows:

x_{1,j} := a_j \quad (1 \leq j \leq N)

x_{i,j} := | x_{i-1,j} - x_{i-1,j+1} | \quad (2 \leq i \leq N and 1 \leq j \leq N+1-i)

Find x_{N,1}.

Constraints

2 \leq N \leq 10^6

a_i = 1,2,3 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
a_1a_2\ldotsa_N

Output

Print x_{N,1}.

Sample Input 1

4
1231

Sample Output 1

1

x_{1,1},x_{1,2},x_{1,3},x_{1,4} are respectively 1,2,3,1.

x_{2,1},x_{2,2},x_{2,3} are respectively |1-2| = 1,|2-3| = 1,|3-1| = 2.

x_{3,1},x_{3,2} are respectively |1-1| = 0,|1-2| = 1.

Finally, x_{4,1} = |0-1| = 1, so the answer is 1.

Sample Input 2

10
2311312312

Sample Output 2

0



Sample input:
4
1231


Sample output:
1

Submission status: Runtime Error

Solution code:
import numpy as np
N = int()
arr = np.array(list(map(int,input().split())))

for _ in range(N):
    diff = np.diff(arr)
print(abs((diff)))

Submission status: Runtime Error

Solution code:

def calc_A_i_j(N,j):
    print(N,j)
    if N == 0 :
        if j >= len(a)-1:
            return 0
        return abs(a[j]-a[j+1])
    return abs(calc_A_i_j(N-1,j) - calc_A_i_j(N-1,j+1))


N = int(input())
a = list(map(int,input()))
#A_i_j = [[-1000000000]*len(a)]*N
#print(A_i_j)
print("len(a):",len(a))
ans = calc_A_i_j(N,1)

print(ans)

Submission status: Runtime Error

Solution code:
import copy

n = int(input())
a = list(str(input()))
a = [int(y) for y in a]

x = []
z = []

for j in range(n):
    z.append(a[j])


for i in range(2,n):
    for j in range(n+1-i):
        x.append(abs(z[j] - z[j+1]))
        if j == (n+1-i):
            z=copy.copy(x)
            x.clear()

print(abs(z[1]-z[2]))

Submission status: Runtime Error

Solution code:
N=int(input())
arr = list(input())
newarr={}
M=N
for j in range(N):
    M=N-1
    for num in range(1,M):
        newarr[num]=abs(int(arr[num-1])-int(arr[num]))
        arr[num] = newarr[num]

print(abs(arr[1]-arr[2]))

Submission status: Runtime Error

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(sl):
  # sl=sorted(l)

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(a)
      a=x
      c=1
  res.append(a)

  return res
# Summarize count of factor within list --- END ---

def main():
  n=I()
  _l=list(S())

  if n==2:
    aaa
    return abs(int(_l[0])-int(_l[1]))

  if _l.count('3')!=0:
    l=[]
    for i in range(n-1):
      x=abs(int(_l[i+1])-int(_l[i]))
      if x!=0:
        l.append(x)
  
    if len(l)==0:
      return 0
  
    ll=l

  else:
    ll=[]
    for x in _l:
      ll.append(int(x))

  # # TLE
  # while True:
  #   if len(l)==1:
  #     # return l[0]
  #     print(l[0])
  #     break
  #   a=[]
  #   x=-1
  #   for i in range(len(l)-1):
  #     b=abs(int(l[i+1])-int(l[i]))
  #     if x!=b:
  #       a.append(b)
  #   l=a

  ans=summarize_list(ll)
  # print(ans)
  # print(ll)

  if len(ans)==2:
    return 1
  return 0

# main()
print(main())

Submission status: Runtime Error

Solution code:
n = int(input())
 
numlist = list(input())

l_numlist = len(numlist)

while l_numlist > 1:
 
    for a in range(0, l_numlist-1):
        d = int(numlist[a]) - int(numlist[a+1])
        if not d == 0:
            numlist.append(abs(d))
 
    numlist = numlist[l_numlist:]
    l_numlist = len(numlist)
    
print(numlist[0])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = list(map(int, input()))

while max(A) >= 2:
    tmp = []
    for i in range(len(A)-1):
        tmp.append(abs(A[i] - A[i+1]))
    A = tmp

while len(A) > 2:
    tmp1 = [A[0]]
    for i in range(1, len(A)):
        if A[i] != tmp1[-1]:
            tmp1.append(A[i])
    tmp2 = []
    for i in range(len(tmp1)-1):
        tmp2.append(abs(tmp1[i] - tmp1[i+1]))
    A = tmp2
if len(A) == 1:
    print(A[0])
else:
    print(abs(A[0] - A[1]))

Submission status: Runtime Error

Solution code:
i=int(input())
s=input().replace('11', '1')
a=[]
for k in range(len(s)-1):
  b=abs((int(s[k])-int(s[k+1])))
  a.append(b)
while len(a)>1:
  c=[]
  d=0
  for k in range(len(a)-1):
    b=abs(a[k]-a[k+1])
    if d==0:
      if len(c) == 1:
        if b == 0 and c[0] == 0:
          continue
        else:
          d=1
          c.append(b)
      else:
        c.append(b)
        d=1
    else:
      c.append(b)
    #str(abs(int(s[k])-int(s[k+1])))
    #print(a[k])
  #print("".join(a))
  a=c
  #s="".join(a)
  #s="1"
print(str(a[0]))

Submission status: Runtime Error

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarize_list(sl):
  # sl=sorted(l)

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(a)
      a=x
      c=1
  res.append(a)

  return res
# Summarize count of factor within list --- END ---

def main():
  n=I()
  _l=list(S())

  if n==2:
    return abs(int(_l[0])-int(_l[1]))

  if _l.count('3')!=0:
    l=[]
    for i in range(n-1):
      x=abs(int(_l[i+1])-int(_l[i]))
      if x!=0:
        l.append(x)
  
    if len(l)==0:
      aaa
  
    ll=l

  else:
    ll=[]
    for x in _l:
      ll.append(int(x))

  # # TLE
  # while True:
  #   if len(l)==1:
  #     # return l[0]
  #     print(l[0])
  #     break
  #   a=[]
  #   x=-1
  #   for i in range(len(l)-1):
  #     b=abs(int(l[i+1])-int(l[i]))
  #     if x!=b:
  #       a.append(b)
  #   l=a

  ans=summarize_list(ll)
  # print(ans)
  # print(ll)

  if len(ans)==2:
    return 1
  return 0

# main()
print(main())

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = list(map(int, input()))

while max(A) >= 2:
    tmp = []
    for i in range(len(A)-1):
        tmp.append(abs(A[i] - A[i+1]))
    A = tmp

while len(A) > 2:
    tmp1 = [A[0]]
    for i in range(1, len(A)):
        if A[i] != tmp1[-1]:
            tmp1.append(A[i])
    tmp2 = []
    for i in range(len(tmp1)-1):
        tmp2.append(abs(tmp1[i] - tmp1[i+1]))
    A = tmp2
print(abs(A[0] - A[1]))

Submission status: Runtime Error

Solution code:
N = int(input())
a = input()

base = []
for i in range(0, N):
    base.append(int(a[i]))

length = N
total = 0

while length > 1:
  if length > 2:
    length -= 1
    for i in range(0, length):
      base.append(abs(base[total + i + 1] - base[total + i]))
    total += length
  elif length == 2:
    length -= 1
    a = abs(base[total + 1] - base[total])
    b = abs(base[total + 2] - base[total + 1])
    
print(abs(b - a))

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
a = input()
X = np.zeros((N,N),dtype=np.int64)
X[0,:] = [int(a[i]) for i in range(N)]
for i in range(1, N):
    for j in range(i,N):
        X[i, j] = abs(X[i-1, j-1] - X[i-1, j])
ans = int(X[-1,-1])
print(ans)

Submission status: Runtime Error

Solution code:
# Your code here!
N=int(input())
arr = list(input())
newarr={}
M=N
for j in range(N):
    
    for num in range(1,N):
        newarr[num]=abs(int(arr[num-1])-int(arr[num]))
        arr[num] = newarr[num]

print(abs(arr[1]-arr[2]))

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(n) for n in list(input())]

memo = [A]
for _ in range(N-1):
    memo.append([100]*(N-1))

for i in range(1,N):
    for j in range(0,N-i):
        sub = abs(memo[i-1][j]-memo[i-1][j+1])
        memo[i][j] = sub

print(abs(memo[N-1][0]))

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
a = input()
if n == 2:
  print(abs(int(a[0]-int(a[1]))))
  sys.exit()

key = [0]*(n-1)
for i in range(n-1):
  key[i] = abs(int(a[i])-int(a[i+1]))
key2 = [0]*(n-2)
for i in range(n-2):
  key2[i] = abs(key[i]-key[i+1])

comb = 1
ans = 0
for i in range(n-2):
  ans += comb*key2[i]
  ans %= 2
  comb *= (n-3-i)
  comb *= pow(i+1, 7, 2)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9+7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

# nC0~nCn odd or even
def nCk(n):
    num = [0]*(n+1)
    for i in range(n+1):
        now = 2
        while now <= i:
            num[i] += i//now
            now *= 2
    cumnum = [num[0]]
    for i in range(1,n+1):
        cumnum.append(cumnum[-1]+num[i])
    ret = [0]*(n+1)
    for i in range(n+1):
        if cumnum[n] > cumnum[i]+cumnum[n-i]:
            ret[i] = 0
        else:
            ret[i] = 1
    return ret

N = I()
A = list(input())
a = [0]*N
for i in range(N):
    a[i] = int(A[i])

b = [0]*(N-1)
for i in range(N-1):
    b[i] = abs(a[i]-a[i+1])

flag = False
for i in range(N-1):
    if b[i] == 1:
        flag = True
        break

nlist = nCk(N-2)

ans = 0
if not flag:
    for i in range(N-1):
        ans += nlist[i]*b[i]
        ans %= 4
    print(ans)
    #raise(ValueError)
else:
    for i in range(N-1):
        ans += nlist[i]*(b[i]%2)
        ans %= 2
    #print(ans)
    raise(ValueError)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
a = input()
X = np.zeros((N,N))
X[0,:] = [int(a[i]) for i in range(N)]
for i in range(1, N):
    for j in range(i,N):
        X[i, j] = abs(X[i-1, j-1] - X[i-1, j])
ans = int(X[-1,-1])
print(ans)

Submission status: Runtime Error

Solution code:
size = int(input())
values = list(map(int, input()))
temp_values = []
pre_value = -1
for exec_count in range(size - 1):
    check_values = values
    if len(list(set(check_values))) == 1:
        # 全要素が同一値になったらその時点で終了
        temp_values.append(0)
        values = temp_values
        break
    i = 0
    # 計算
    for value_string in list(values):
        value = int(value_string)
        if pre_value != -1:
            temp_values.append(abs(value - pre_value))
        pre_value = value
        i += 1
    values = temp_values
    temp_values = []
    pre_value = -1

    # 束ね処理
    sum_value = []
    count = 0
    i = 0
    if len(values) > 5:
        for value in values:
            if pre_value == value:
                count += 1
            else:
                if count > 0:
                    if count > 2:
                        sum_value.pop(i - count - 1)
                        sum_value.append(pre_value)
                        sum_value.append(0)
                        sum_value.append(pre_value)
                    else:
                        for i in range(count):
                            sum_value.append(pre_value)
                sum_value.append(value)
                pre_value = value
                count = 0
            i += 1
        if count > 0:
            for i in range(count):
                sum_value.append(pre_value)
        values = sum_value
    if len(values) == 1:
        break
    pre_value = -1
print(values[0])

Submission status: Runtime Error

Solution code:

import numpy as np
N = int(input())
numbers = input()
input_arr = np.array([int(i) for i in numbers])
def memoize(f): #メモ化関数
    table = {}
    def func(*args):
        if not len(args) in table:
            table[len(args)] = f(*args)
        return table[len(args)]
    return func

@memoize
def myfunc(arr):
    if(len(arr)) == 1:
        return arr[0]
    return myfunc(np.abs(arr[1:] - arr[:-1]))

print(myfunc(input_arr))

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import numpy as np

n = ni()
a = list(map(int, list(ns())))
#n = 10**5
#a = [2]*(10**5)

if n > 10**4:
    a = a[:10**4]

b = np.array(a)

for i in range(n-1):
    b = np.abs(b[1:]-b[:-1])

print(b[0])

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
a = input()
if N<=100:
    ls = np.array(list(a), dtype=np.int8)
    b = np.zeros((N, N), dtype=np.int16)
    b[0] = ls
    for i in range(1, N):
        for j in range(N - i):
            b[i][j] = abs(b[i - 1][j] - b[i - 1][j + 1])
    print(b[N-1][0])
elif N%2==1:
    ls = np.array([a[0],a[2],a[4],a[6],a[-7],a[-5],a[-3],a[-1]], dtype=np.int8)
    b = np.zeros((8, 8), dtype=np.int16)
    b[0] = ls
    for i in range(1, 8):
        for j in range(8 - i):
            b[i][j] = abs(b[i - 1][j] - b[i - 1][j + 1])
    print(b[8 - 1][0])
else:
    assert()

Submission status: Runtime Error

Solution code:
size = int(input())
values = list(map(int, input()))
temp_values = []
pre_value = -1
for exec_count in range(size - 1):
    check_values = values
    if len(list(set(check_values))) == 1:
        # 全要素が同一値になったらその時点で終了
        temp_values.append(0)
        values = temp_values
        break
    i = 0
    # 計算
    for value_string in list(values):
        value = int(value_string)
        if pre_value != -1:
            temp_values.append(abs(value - pre_value))
        pre_value = value
        i += 1
    values = temp_values
    temp_values = []
    pre_value = -1

    # 束ね処理
    sum_value = []
    count = 0
    i = 0
    if len(values) > 5:
        for value in values:
            if pre_value == value:
                count += 1
            else:
                if count > 0:
                    if count > 2:
                        sum_value.pop(i - count - 1)
                        sum_value.append(pre_value)
                        sum_value.append(0)
                        sum_value.append(pre_value)
                    else:
                        for i in range(count):
                            sum_value.append(pre_value)
                sum_value.append(value)
                pre_value = value
                count = 0
            i += 1
        if count > 0:
            for i in range(count):
                sum_value.append(pre_value)
        values = sum
    if len(values) == 1:
        break
    pre_value = -1
print(values[0])

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    S = input().strip()

    if N == 2:
        print(abs(S[0] - S[1]))
        return

    if N == 3:
        print(abs(S[0] - S[1]))
        return

    A = []
    for a, b in zip(S, S[1:]):
        A.append(abs(int(a) - int(b)))

    if N % 2 == 1:
        A[-1] = -A[-1]
    print(abs(A[0] - A[-1]))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math
import numpy as np

N = int(input())
arr_rev = list(map(int, list(input())))
arr_rev.reverse()
arr_rev = np.array(arr_rev)

def calc(a, b):
    return abs(-a + b)


def chanto(arr_rev):
    inp = arr_rev
    diff_1 = [calc(inp[x], inp[x + 1]) for x in range(len(inp) - 1)]
    diff_1_nonz = diff_1[diff_1>0]
    if len(diff_1_nonz) == 0:
        return 0

    return diff_1_nonz[0] + diff_1_nonz[len(diff_1_nonz)-1] % 2


print(chanto(arr_rev))

Submission status: Runtime Error

Solution code:
n=c=int(input())
a=[int(i) for i in input()]
x=[[0]*n for i in range(n)]
for i in range(n):
  j=0
  while j<c:
    if i==0:
      x[0][j]=a[j]
    else:
      x[i][j]=abs(x[i-1][j]-x[i-1][j+1])
    j+=1
  c-=1
print(x[n-1][0])

Submission status: Runtime Error

Solution code:
N = int(input())
a = input()

if len(a) == 1 :
    print(a)
    exit()
elif len(set(a)) == 1 :
    print(0)
    exit()

b = ""
for i in range(1,len(a)) :
    if a[i] != a[i-1] :
        b += str(abs(int(a[i]) - int(a[i-1])))

if len(b) == 1 :
    print(c)
    exit()
elif len(set(b)) == 1 :
    print("0")
    exit()

c = 0
for i in range(1,len(b)) :
    if b[i] != b[i-1] :
        c += 1

if c == 1 :
    print(1)
else :
    print(0)

Submission status: Runtime Error

Solution code:
import itertools
import sys
import math
import heapq
import numpy as np
from collections import deque
from itertools import combinations
from functools import lru_cache
sys.setrecursionlimit(10**9)

INF = 10**20
def main():
    N = int(input())
    A = list(map(int,input()))

    @lru_cache(None)
    def solve(x,y):
        if x == 0:
            return A[y]
        return abs(solve(x-1,y) - solve(x-1,y+1))

    print(solve(N-1,0))

if __name__ == "__main__":
  main()

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
a = input()
X = np.zeros((N,N))
X[0,:] = [int(a[i]) for i in range(N)]
for i in range(1, N):
    for j in range(i-1,N-1):
        X[i, j+1] = abs(X[i-1, j] - X[i-1, j+1])
ans = int(X[-1,-1])
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

N = int(input())

if N > 10:
  print(0)
  sys.exit()

L = [int(s) for s in input()]

a = np.array(L)
for i in range(N-1):
  a = np.abs(np.diff(a))
  a = a[np.nonzero(a)] 
print(a[0])

Submission status: Runtime Error

Solution code:
size = int(input())
values = list(map(int, input()))
temp_values = []
pre_value = -1
for exec_count in range(size - 1):
    check_values = values
    if len(list(set(check_values))) == 1:
        temp_values.append(0)
        values = temp_values
        break
    i = 0
    for value_string in list(values):
        value = int(value_string)
        if pre_value != -1:
            temp_values.append(abs(value - pre_value))
        pre_value = value
        i += 1
    values = temp_values
    temp_values = []
    pre_value = -1
    # 束ね処理
    sum = []
    count = 0
    i = 0
    if len(values) > 5:
        for value in values:
            if pre_value == value:
                count += 1
            else:
                if count > 0:
                    if count > 2:
                        sum.pop(i - count - 1)
                        sum.append(pre_value)
                        sum.append(0)
                        sum.append(pre_value)
                    else:
                        for i in range(count):
                            sum.append(pre_value)
                sum.append(value)
                pre_value = value
                count = 0
            i += 1
        if count > 0:
            for i in range(count):
                sum.append(pre_value)
        values = sum
    if len(values) == 1:
        break
print(values[0])

Submission status: Runtime Error

Solution code:
import sys; sys.setrecursionlimit(2147483647); input = sys.stdin.readline
from math import floor, ceil, sqrt, factorial, log
from collections import Counter, defaultdict, deque
from operator import itemgetter
INF = float('inf'); MOD = 10**9+7
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(MI())
def LIR(n): return [LI() for i in range(n)]
def IS(): return input().rstrip()

def main():
    N = I(); a = IS()
    x = [abs(int(k[0])-int(k[1])) for k, v in Counter([a[i]+a[i+1] for i in range(N-1)]).items() if v%2 == 1]

    while len(x) > 1:
        x = [abs(x[i]-x[i+1]) for i in range(len(x)-1)]
    print(x[0])
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
def keisan(a,b):
    return abs(a-b)
    
if n>=4:
    p,q,r=int(s[2]),int(s[1]),int(s[0])
    ans=keisan(keisan(p,q), keisan(q,r))
    for c in range(n-3):
        p3=int(s[c])
        p2=int(s[c+1])
        p1=int(s[c+2])
        d0=int(s[c+3])
        ddd=keisan(keisan(d0,p1), keisan(p1,p2))
        ans=keisan(ans,ddd)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

n = int(input())
a = input()
x = np.zeros((n,n),np.int32)
def F(i,j):
    return abs(x[i-1][j] - x[i-1][j+1])

for i in range(n):
    x[0,i] = a[i]
for i in range(1,n):
    for j in range(n-1):
        x[i,j] = F(i,j)
print(x[n-1][0])

Submission status: Runtime Error

Solution code:
size = int(input())
values = list(map(int, input()))
temp_values = []
pre_value = -1
for exec_count in range(size - 1):
    check_values = values
    if len(list(set(check_values))) == 1:
        temp_values.append(0)
        values = temp_values
        break
    i = 0
    for value_string in list(values):
        value = int(value_string)
        if pre_value != -1:
            temp_values.append(abs(value - pre_value))
        pre_value = value
        i += 1
    values = temp_values
    temp_values = []
    pre_value = -1
    # 束ね処理
    sum = []
    count = 0
    i = 0
    if len(values) > 5:
        for value in values:
            if pre_value == value:
                count += 1
            else:
                if count > 0:
                    if count > 2:
                        sum.remove(i - count - 1)
                        sum.append(pre_value)
                        sum.append(0)
                        sum.append(pre_value)
                    else:
                        for i in range(count - 1):
                            sum.append(pre_value)
                sum.append(value)
                pre_value = value
                count = 0
            i += 1
        if count > 0:
            for i in range(count - 1):
                sum.append(pre_value)
        values = sum
    if len(values) == 1:
        break
print(values[0])

Submission status: Runtime Error

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = list(map(int,input()))
  import copy
  for _ in range(n-1):
    b=[0]*(len(a)-1)
    for i in range(len(a)-1):
      b[i]=abs(a[i]-a[i+1])
    a = copy.deepcopy(b)

  print(b[0])
if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int,list(sys.stdin.readline().strip())))


while(len(a)>1):
	a = [abs(a[i]-a[i+1]) for i in range(N-1)]
	N -= 1


print(a[0])

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
a=[int(c) for c in s]
b=a
ans=[]
for j in range(n):
    if j+1<len(b):
        if b[j]==b[j+1]:
            b.remove(b[j])
    for i in range(n-j):
        if i+1<len(b):
            ans.append(abs(b[i]-b[i+1]))
    b=ans
    if len(ans)>1:
        ans=[]

print(ans[0])
#print(a)

Submission status: Runtime Error

Solution code:
i=input().strip(' ')
n=int(i)
s = input().strip()
a=[[0]*n for i in range(n)]
for i in range(n):
    a[0][i]=int(s[i])
for i in range(1,n):
    for j in range(0,n):
        if j==n-i:
            break
        else:
            a[i][j]=abs(a[i-1][j]-a[i-1][j+1])
print(a[n-1][0])

Submission status: Runtime Error

Solution code:
def main():
    import numpy as np

    N = int(input())
    x = np.array([int(i) for i in input()])
    for i in range(N-1):
        x = np.delete(np.abs(x - np.roll(x, -1)), -1)

    print(x[0])

if __name__ == '__main__'
    main()

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

N = int(input())

if N > 10:
  print(0)
  sys.exit()

L = [int(s) for s in input()]

a = np.array(L)
for i in range(N-1):
  a = np.abs(np.diff(a), N)
  print(a)
print(a)

Submission status: Runtime Error

Solution code:
import math

n = int(input())
a = input()
l = [int(a[i]) for i in range(n)]

def sum(n, i, k):
  if n < i:
    return k
  A = abs(l[i] - l[i+1]) 
  return  sum(n,i+1, k + A*comb(n,i)*(-1)**i)

if n != 1:
  print(int(sum(n-2,0,0)))
else:
  print(int(l[0]))

Submission status: Runtime Error

Solution code:
import itertools
from functools import lru_cache
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

if N == 1:
    print(A[0])
    exit()
elif N == 2:
    print(abs(A[0] - A[1]))
    exit()

TRI = [[[0] * 4 for _ in range(4)] for _ in range(4)]

for a, b, c in itertools.product(range(1, 4), repeat=3):
    p = abs(a - b)
    q = abs(b - c)
    r = abs(p - q)
    TRI[a][b][c] = r


@lru_cache(maxsize=None)
def F(i, j):
    if i == 3:
        a = A[j - 1]
        b = A[j]
        c = A[j + 1]
        return TRI[a][b][c]

    a = F(i - 1, j)
    b = F(i - 1, j + 1)
    r = abs(a - b)

    return r


ans = F(N, 1)

print(ans)

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache

sys.setrecursionlimit(10**9)
N = int(input())
S = [0] + list(map(int, list(input())))
# S = list(map(int, input().split()))

@lru_cache(maxsize=None)
def func(i,j):
    if i == 1:
        return S[j]
    if j > N+1-i:
        return 0
    return abs(func(i-1,j) - func(i-1, j+1))

def main():
    # print(S)
    print(abs(func(N, 1)))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def v(a, i, j):
    if(i == 1):
        return int(a[j-1])
    else:
        return abs(v(a, i-1, j) - v(a, i-1, j+1))
N = int(input())
a = input()
i = N
j = 1
print(v(a, i, j))

Submission status: Runtime Error

Solution code:
from scipy.misc import comb
import math

n = int(input())
a = input()
l = [int(a[i]) for i in range(n)]

def sum(n, i, k):
  if n < i:
    return k
  A = abs(l[i] - l[i+1]) 
  return  sum(n,i+1, k + A*comb(n,i)*(-1)**i)

if n != 1:
  print(sum(n-2,0,0))
else :
  print(l[0])

Submission status: Runtime Error

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = list(map(int,input()))
  import copy
  for _ in range(n-1):
    b=[0]*(len(a)-1)
    for i in range(len(a)-1):
      b[i]=abs(a[i]-a[i+1])
    a = copy.deepcopy(b)

  print(b[0])
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = list(map(int,input()))
  import copy
  for _ in range(n-1):
    b=[0]*(len(a)-1)
    for i in range(len(a)-1):
      b[i]=abs(a[i]-a[i+1])
    a = copy.deepcopy(b)

  print(b[0])
if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase

def main():
    n =int(input())
    a = list(map(int,input()))
    if n<=10000:
        new = a.copy()
        for i in range(n-1):
            new1 = new.copy()
            for j in range(1,n-i):
                new[j] = abs(new1[j]-new1[j-1])
            # print(new)
            new.pop(0)
            # print(new)
        print(new[0])
    else:
        m = 99
        for i in range(1,len(a)):
            m = min(m,abs(a[i]-a[i-1]))
        print(m)





# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from operator import mul


def test(a):
    print(*a)
    for i in range(len(a) - 1):
        a = [abs(a1 - a0) for a0, a1 in zip(a, a[1:])]
        print(' ' * i, *a)


def first(aaa):
    aaa = list(map(int, aaa))
    aaa = [abs(a1 - a0) for a0, a1 in zip(aaa, aaa[1:])]
    return aaa


def last(aaa):
    for i in range(len(aaa) - 1):
        aaa = [abs(a1 - a0) for a0, a1 in zip(aaa, aaa[1:])]
    return aaa[0]


def prepare():
    # for v in product([0, 1, 2], repeat=10):
    ret = [0] * (3 ** 10)
    for i in range(3 ** 10):
        v = i
        v, m1 = divmod(v, 3)
        tmp = []
        for _ in range(9):
            v, m2 = divmod(v, 3)
            tmp.append(abs(m1 - m2))
            m1 = m2
        tmp.reverse()
        for _ in range(8):
            tmp = [abs(a1 - a0) for a0, a1 in zip(tmp, tmp[1:])]
        ret[i] = tmp[0]
    return ret


# test([2, 3, 1, 1, 3, 1, 2, 3, 1, 2])
# test([2, 3, 1, 1, 3, 2, 2, 3, 1, 2])
# test([2, 3, 1, 1, 3, 3, 2, 3, 1, 2])
# test([1, 3, 1, 3, 1, 3, 1, 3, 1, 3])

n = int(input())
aaa = input()
aaa = first(aaa)
ppp = prepare()
pow3 = [3 ** (9 - i) for i in range(10)]
while len(aaa) >= 10:
    aaa = [ppp[sum(map(mul, zip(aaa[i:i + 10], pow3)))] for i in range(len(aaa) - 9)]
ans = last(aaa)
print(ans)