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: Time Limit Exceeded

Solution code:
#import copy
N = int(input())
An = list(map(int,list(input())))
#print(An)
def solve(n):
    previous_num = -1
    previous_num = -1
    insert_idx = 0
    for i in range(n-1):
        current_num = abs(An[i]-An[i+1])
        if current_num == previous_num:
            continue
        An[insert_idx] = current_num
        previous_num = current_num
        insert_idx += 1
    if insert_idx == 1:
        An[1] = An[0]
    return insert_idx

def solve2(n):
    previous_num = -1
    previous_previous_num = -1
    insert_idx = 0
    for i in range(n-1):
        current_num = abs(An[i]-An[i+1])
        if current_num == previous_num and previous_num == previous_previous_num:
            continue
        An[insert_idx] = current_num
        previous_previous_num = previous_num
        previous_num = current_num
        insert_idx += 1
    if insert_idx == 1:
        An[1] = An[0]
    return insert_idx


current_n = N
while 2 < current_n:
    current_n = solve2(current_n)
print(abs(An[1]-An[0]))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
a=[int(c) for c in s]
b=a
ans=[]
for j in range(n):
    for i in range(len(b)):
        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: Time Limit Exceeded

Solution code:
import sys
import numpy as np
#input = sys.stdin.readline

def main():
    n=int(input())
    a=np.array(list(map(int,list(input()))))
    while len(a)>1:
        a=np.abs(a[1:]-a[:-1])
    print(a[0])

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
a = list(input())
a_i = [int(i) for i in a]

def calcabs(list):
    result = []
    for x in range(len(list)):
        if x == 0: continue
        #elif abs(list[x] - list[x - 1]) == 0: continue
        else: 
            val = abs(list[x] - list[x - 1])
            if len(result) == 0: result.append(val)
            else: result.append(val)
    return result

while len(a_i) > 1:
    a_i = calcabs(a_i)
if len(a_i) == 0: print(0)
elif len(a_i) == 1: print(a_i[0])

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np


def input():
    return sys.stdin.readline().strip()


def calc(A):
    B = np.abs(A[1:] - A[:-1])
    return B


N = int(input())
A = input()

A = np.array([int(a) for a in A])

for i in range(N - 1):
    A = calc(A)
print(A[0])

Submission status: Time Limit Exceeded

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

for _ in range(N-1):
  x = np.abs(x[1:] - x[:-1])
  #print(x)

#print(x)
print(int(x[0]))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
s = list(map(int,list(input())))
special=0
if s[0] == 3 and s.count(1) == len(s)-1:
    print(2)
    special=1
elif s[-1] == 3 and s.count(1) == len(s)-1:
    print(2)
    special=1
for i in range(n-2):
    if len(s)==2:
        break
    for j in range(len(s)-1):
        s.append(abs(s[0]-s[1]))
        s.pop(0)
        #print(s)
    s.pop(0)
    if len(s)==2:
        break
    if s[0] == 3 and s.count(1) == len(s)-1:
        print(2)
        special=1
    elif s[-1] == 3 and s.count(1) == len(s)-1:
        print(2)
        special=1
    if len(s)>=2:
        while True:
            if s[0] == 1 and s[1] == 1:
                s.pop(0)
                if len(s)<2:
                    break
            else:
                break
    if len(s)>=2:
        while True:
            if s[-1] == 1 and s[-2] == 1:
                s.pop(-1)
                if len(s)<2:
                    break
            else:
                break
        #print(s)

   # print(s)
if special == 0:
    print(abs(s[0]-s[1]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input(''))
s = input('')

a = []
    
for i in range(n):
    a.append(int(s[i]))

x = []
for i in range(n):
    b = []
    for j in range(n):
        b.append(0)
    x.append(b)

for i in range(n):
    x[0][i] = a[i]

for i in range(1, n):
    for j in range(0, n-i):
        x[i][j] = abs(x[i-1][j] - x[i-1][j+1])

print(x[n-1][0])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
a = [int(i) for i in input()]

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

Submission status: Time Limit Exceeded

Solution code:
given = [input() for i in range(2)]
N = int(given[0])

result = [[0] * N] * N
result[0] = [int(c) for c in given[1]]

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

for i in range(2, N):
    for j in range(N - i):
        if result[i - 1][j] == 0:
            result[i][j] = result[i - 1][j + 1]
        elif result[i - 1][j] == 1:
            if result[i - 1][j + 1] == 0:
                result[i][j] = 1
            else:
                result[i][j] = result[i - 1][j + 1] - 1
        elif result[i - 1][j] == 2:
            if result[i - 1][j + 1] == 0:
                result[i][j] = 2
            elif result[i - 1][j + 1] == 1:
                result[i][j] = 1
            else:
                result[i][j] = 0
print(result[N - 1][0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
seq = list(map(int, input()))
def get_sub_seq(l):
    r = range(len(l)-1)
    new = [(l[i]-l[i+1]) for i in r]
    new = list(map(abs, new))
    return new
while len(seq) > 1:
    seq = get_sub_seq(seq)
print(seq[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(int, list(input())))
# S = list(map(int, input().split()))


def main():
    # print(S)
    x = [[] for _ in range(N)]
    # print(x)
    x[0] = [s for s in S]
    # print(x)
    for i in range(1,N):
        # print(x[i])
        for j in range(N-i):
            # print(i,j)
            # print(x[i-1][j])
            # print(x[i-1][j+1])
            x[i].append(abs(x[i-1][j] - x[i-1][j+1]))
    print(x[N-1][0])

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
al = tuple(int(x) for x in list(input()))


def gen(al):
    return tuple(abs(al[i] - al[i + 1]) for i in range(len(al) - 1))


for i in range(n - 1):
    al = gen(al)

print(al[0])

Submission status: Time Limit Exceeded

Solution code:

### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline
    n=int(readline())
    x=list(readline().rstrip())
    for i in range(n):
        x[i]=int(x[i])
    for i in range(1,n):
        for j in range(n-i):
            x[j]=abs(x[j]-x[j+1])
    print(x[0])
    #n,m,k=map(int, readline().rstrip().split())
    #arr=list(map(int, readline().rstrip().split()))
    #n=int(readline())
    #ss=readline().rstrip()
    #yn(1)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = input()
for i in range(N-1):
    b = [0] * (N-1-i)
    for j in range(N-1-i):
        b[j] = abs(int(a[j]) - int(a[j+1]))
    a = b
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_li = list(map(int, list(input())))

while n > 1:
    for i in range(n):
        if i == n - 1:
            n -= 1
            break
        a_li[i] = abs(a_li[i] - a_li[i+1])
print(a_li[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(input())
x = [[0] * n for j in range(n)]
for i in range(n):
    x[0][i] = int(a[i])
cnt = n
for i in range(1,n):
    cnt -= 1
    for j in range(cnt):
        x[i][j] = abs(x[i-1][j] - x[i-1][j+1])
print(x[n-1][0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
x1 = input()
x1ar = []
for i in range(len(x1)):
    x1ar.append(int(x1[i]))
xar = []
xar.append(x1ar)
def solve():
    for i in range(N-1):
        xar.append([])
        flag = True
        for j in range(N-i-1):
            v = abs(xar[i][j] - xar[i][j+1])
            xar[i+1].append(v)
            if v != 0:
                flag = False
        if flag:
            print(0)
            return
    print(xar[len(xar)-1][0])
solve()

Submission status: Time Limit Exceeded

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

def x_number(s):
  if len(s) > 1:
    X=[]
    l=len(s)-1
    for i in range(l):
      x=abs(int(s[i])-int(s[i+1]))
      X.append(x)
  else:
    return s[0]
  return x_number(X)

print(x_number(a))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
a=np.array(list(map(int,input())))

while len(a)>1:a=abs(np.diff(a,n=1))

  
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = input()
x = [int(i) for i in s]
y = []
for _ in range(N-1):
    y = [abs(x[i] - x[i+1]) for i in range(len(x)-1)]
    x=y[:]
print(y[0])

Submission status: Time Limit Exceeded

Solution code:
import sys

N = int(sys.stdin.readline())
a = list(sys.stdin.readline())
x =[a[:N]]
for j in range(N):
    l = []
    for index in range(len(x[j])):
        if index == len(x[j]) - 1:
            break
        l.append(abs(int(x[j][index]) - int(x[j][index+1])))
    x.append(l)
print(x[N-1][0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, list(input())))
x = [max(a[i], a[i+1]) - min(a[i], a[i+1]) for i in range(N-1)]

def make_x(l):
	n = len(l)
	if n == 1:
		return l
	l2 = [l[0]]
	cnt = 1
	length = 1
	for i in range(1, n):
		if l[i] != l2[-1]:
			if cnt % 2 == 0:
				l2.append(l2[-1])
			cnt = 1
			l2.append(l[i])
			length += 1
		else:
			cnt += 1
	if length == 1:
		return [0]
	return l2

def next_x(l):
	n = len(l)
	return [max(l[i], l[i+1]) - min(l[i], l[i+1]) for i in range(n-1)]

while len(x) > 1:
	x = make_x(next_x(x))
print(x[0])

Submission status: Time Limit Exceeded

Solution code:
def main():

    N = int(input())
    a = input()
    la = []
    for c in a:
        la.append(int(c))
        
    def na(l):
        tmp=[]
        for e in l:
            tmp.append(abs(e))
        return tmp

    def nd(l):
        tmp=[]
        for i,e in enumerate(l):
            if i==0:
                _e = e
            else:
                tmp.append(_e-e)
                _e = e
                if i == len(l) - 1:
                    break
        return tmp

    for _ in range(N - 1):
        la = na(nd(la))

    print(*la)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = input()
b = [0] * (N-1)
for i in range(N-1):
    b = [0] * (N-1-i)
    for j in range(N-1-i):
        b[j] = abs(int(a[j]) - int(a[j+1]))
    a = b
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
a = int(input())
s = input()
s = s.replace("1","0").replace("2","1").replace("3","2")

memo = [[0],[0,1,2]]
from itertools import product
for i in range(2,11):
    memo_i = []
    ma = memo_i.append
    for j in product([0,1,2],repeat=i):
        ret = ""
        for k,l in zip(j,j[1::]):
            ret+=str(abs(k-l))
        ma(memo[i-1][int(ret,3)])
    memo.append(memo_i)

while len(s)>=10:
    nl = ""
    for i in range(len(s)-9):
        nl+=str(memo[10][int(s[i:i+10],3)])
    s=nl
print(memo[len(s)][int(s,3)])

Submission status: Time Limit Exceeded

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

num = [int(x) for x in list(str(A))]
length = len(num)
count_stopper = len(num)

cnt = 1
a = 0
big_count = 0
new = []
while big_count != length-1:
    while cnt != count_stopper:
        a = 0
        a = num[cnt - 1] - num[cnt]
        new.append(abs(a))
        cnt += 1
    cnt = 1
    num = new
    new = []
    big_count += 1
    count_stopper -= 1

print(num[0])

Submission status: Time Limit Exceeded

Solution code:
n=int(input(''))
m=input('')
s=[]
for a in range(n):
    s.append(int(m[a]))

kuri=n-1
for a in range(n-1):
    for b in range(kuri):
        if s[b]==s[b+1]:
            s[b]=0
        elif s[b]*s[b+1]==2:
            s[b]=1
        elif s[b]*s[b+1]==3:
            s[b]=2
        elif s[b]*s[b+1]==6:
            s[b]=1
        else:
            if s[b]==0:
                s[b]=s[b+1]

    
    kuri=kuri-1

print(s[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
seq = list(map(int, input()))
get_sub_seq = lambda l:[(l[i]-l[i+1]) for i in range(len(l)-1)]
while len(seq) > 1:
    seq = get_sub_seq(seq)
    seq = list(map(abs, seq))
print(seq[0])

Submission status: Time Limit Exceeded

Solution code:
class Binary_Indexed_Tree():
    def __init__(self, n):
        self.n = n
        self.data = [0]*(n+1)

    def add(self, i, x):
        i += 1
        while i <= self.n:
            self.data[i] += x
            i += i & -i

    def get(self, i):
        return self.sum_range(i, i)

    def sum(self, i):
        i += 1
        ret = 0
        while i:
            ret += self.data[i]
            i &= i-1
        return ret

    def sum_range(self, l, r):
        return self.sum(r)-self.sum(l-1)

    def lower_bound(self, w):
        if w<=0:
            return 0
        i = 0
        k = 1<<(self.n.bit_length())
        while k:
            if i+k <= self.n and self.data[i+k] < w:
                w -= self.data[i+k]
                i += k
            k >>= 1
        return i

n = int(input())
a = list(map(lambda x: int(x)-1, input()))

if 1 in a:
    BIT = Binary_Indexed_Tree(n+1)
    for i, ai in enumerate(a):
        if ai == 1:
            BIT.add(i, 1)
        while True:
            s = BIT.sum(i)
            if s == 0:
                break
            i = BIT.lower_bound(s)
            s = BIT.sum(i)
            if s > 1:
                j = BIT.lower_bound(s-1)
                for k in range(j+1, i):
                    BIT.add(k,1)
                BIT.add(j, -1)
                i = j
            else:
                for k in range(i):
                    BIT.add(k,1)
                break
    ans = BIT.get(0)
else:
    BIT = Binary_Indexed_Tree(n+1)
    a = [int(i==2) for i in a]
    for i, ai in enumerate(a):
        if ai == 1:
            BIT.add(i, 1)
        while True:
            s = BIT.sum(i)
            if s == 0:
                break
            i = BIT.lower_bound(s)
            s = BIT.sum(i)
            if s > 1:
                j = BIT.lower_bound(s-1)
                for k in range(j+1, i):
                    BIT.add(k,1)
                BIT.add(j, -1)
                i = j
            else:
                for k in range(i):
                    BIT.add(k,1)
                break
    ans = 2*BIT.get(0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = input().strip()
al = list(map(int, a))
al2 = [abs(al[i] - al[i + 1]) for i in range(len(al) - 1)]
while len(al2) > 1:
    al2 = [abs(al2[i] - al2[i + 1]) for i in range(len(al2) - 1)]
print(al2[0] if len(al2) else 0)

Submission status: Time Limit Exceeded

Solution code:
def main():
  n = int(input())
  params = [int(t) for t in input()]
  
  while len(params) > 1:
    temp = []
    i = 0
    while(i < len(params)-1):
      if params[i] == params[i+1]:
        temp.append(0)
        i += 1
        while(i < len(params)-1):
          if params[i] == params[i+1]:
            i += 1
          else:
            break
        continue
      temp.append(abs(params[i] - params[i+1]))
      i += 1
    params = temp 
  return params[0]


print(main())

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, list(input().strip())))

tri = [A]
while len(tri[-1]) > 1:
    nx = []
    nw = tri[-1]
    for j in range(1,len(nw)):
        _abs = abs(nw[j]-nw[j-1])
        if abs:
            nx.append(abs(nw[j]-nw[j-1]))
    tri.append(nx)
print(tri[-1][0])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

temp = A
for i in range(n - 2):
    next_temp = []
    for j in range(n - i - 1):
        value = abs(temp[j] - temp[j + 1])
        next_temp.append(value)
    temp = next_temp
    if next_temp[1:-1] == [0] * (n - i - 3):
        break

print(abs(temp[0] - temp[-1]))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import numpy as np

def cal(b, N):
    # c = np.array([abs(b[i] - b[i + 1]) for i in range(N)])
    c = np.diff(b, n= 1)
    c = np.abs(c)

    return c

N = int(input())
# print(N)
a = input()

b = np.array([int(a[i:i + 1]) for i in range(N)])
# print(a)
# print(b)

for i in range(N - 1):
    b = cal(b, N - 1)
    N -= 1
    # print(b)
print(b[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = input()
l = [int(a[i]) for i in range(N)]
k = []

for j in range(1,N):
    for i in range(N-j):
        min = abs(l[i] - l[i+1])
        k.append(min)
    l = k
    k = []
        
ans = l[0]
print(ans)

Submission status: Time Limit Exceeded

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

n = int(input())
nums = int(input())
str_list = list(str(nums))

nums_list = [ int(i) for i in str_list ]
tuples = tuple(nums_list)
from functools import lru_cache

@lru_cache(maxsize=1000)
def keisan(t):
  nums_list = list(t)
  result = [ abs(nums_list[i] - nums_list[i+1]) for i in range(len(nums_list)-1) ]
  if len(result) > 1:
    return keisan(tuple(result))
  else:
    return result

nums_list = keisan(tuples)  
print(nums_list[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
al = tuple(int(x) for x in list(input()))


def gen(al):
    new_al = tuple(abs(al[i] - al[i + 1]) for i in range(len(al) - 1))
    return new_al


for i in range(n - 1):
    al = gen(al)

print(al[0])

Submission status: Time Limit Exceeded

Solution code:
n = input()
res = list(map(int,input()))
d = len(res)
res2 = []
while d > 1:
    for i in range(len(res) - 1):
        res[i] = (abs(res[i] - res[i+1]))
    d -= 1
print(res[0])

Submission status: Time Limit Exceeded

Solution code:
def main():
  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: Time Limit Exceeded

Solution code:
n = input()
a = list(input())
a = [int(i) for i in a]

def func(a):
        ls = []
        for i, x in enumerate(a):
                if i + 1 <= len(a) - 1:
                        ls.append((abs(x - a[i+1])))
        return ls

while len(a) > 1:
        a = func(a)
print(a[0])

Submission status: Time Limit Exceeded

Solution code:

def calc(x):
    if len(x) == 1:
        return x
    result = ""
    for i in range(len(x) - 1):
        value = abs(int(x[i]) - int(x[i + 1]))
        result += str(value)
    return calc(result)

def main():
    N = int(input())
    a = input()

    r = calc(a)
    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input()
b = []
alist =[]
while len(a) > 1:
  for i in range(1,len(a)):
    alist.append(str(abs(int(a[i])-int(a[i-1]))))
  a = "".join(alist)
  alist = []
print(int(a))

Submission status: Time Limit Exceeded

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

a = int(input())

A =  [int(i) for i in list(input())]

x = [[ 0 for j in range(a)] for i in range(a)]

for j in range(a):
    x[0][j] = A[j]

for i in range(1, a):
    for j in range(a-i):
        x[i][j] = abs(x[i-1][j] - x[i-1][j+1])

print(x[a-1][0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
an = [int(x) for x in list(input())]

for i in range(N-1,0,-1):
    bn = []
    for j in range(i):
        bn.append(abs(an[j] - an[j+1]))
    # print(bn)
    an = bn

print(an[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
array = []
array += input()
array = list(map(int,array))
while len(array) != 1:
    for i in range(len(array)-1):
        array[i] = abs(array[i] - array[i+1])
    array.pop()
print(array[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, list(input().strip())))

tri = [A]
while len(tri[-1]) > 1:
    nx = []
    nw = tri[-1]
    for j in range(1,len(nw)):
        _abs = abs(nw[j]-nw[j-1])
        if abs:
            nx.append(abs(nw[j]-nw[j-1]))
    tri.append(nx)
print(tri[-1][0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list(map(int, list(input())))
ans = 0
prev = [A[0]]
for a in A[1:]:
    tmp = [a]
    for x in prev:
        a = abs(x-a)
        tmp.append(a)
    prev = tmp
print(prev[-1])

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
b=int(input())
b = str(b)
list = list(b)
list = [int(s) for s in list]
listforcal = []

for i in reversed(range (2,a+1)):
    for j in range (i-1):
        number = abs(list[j] - list[j+1])
        listforcal.append(number)
    list.clear()
    list = listforcal
    listforcal=[]

print(list[0])

Submission status: Time Limit Exceeded

Solution code:
a=input()
n=input()
current_list=[]
for i in n :
  current_list.append(int(i))
k=len(current_list) 
while (k>1) :
  current=[]
  for i,num in enumerate(current_list) :
    if i!=len(current_list)-1 :
    	current.append(abs(num-current_list[i+1]))
  
  current_list=current
  k-=1
 
print (current_list[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = input()

before_list = a_list
for y in range(1, n):
    next_list = ''
    for x in range(len(before_list)-1):
        if y == 1:
            next_value = abs(int(a_list[x]) - int(a_list[x+1]))
            next_list += str(next_value)
        else:
            next_value = abs(int(before_list[x]) - int(before_list[x+1]))
            next_list += str(next_value)

    before_list = next_list
    next_list = ''

print(before_list[0])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N=int(input())
A = input()
a = np.array([int(A[i]) for i in range(len(A))])
while len(a) !=1:
  a = np.absolute(np.diff(np.absolute(a),1))
  if len(a) == 1:
    print(a[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
x = list(str(input()))
x = [int(i) for i in x]

for i in range(n-1):
    tmp = []
    for j in range(1,len(x)):
        tmp.append(abs(x[j]-x[j-1]))
    x = tmp

print(x[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = [int(k) for k in list(input())]
def triangle (num, *args):
    tmp = []
    n = num
    if n == 1:
        print(abs(args[0]))
        exit(0)
    else:
        for i in range(n - 1):
            tmp.append(abs(args[i] - args[i + 1]))
        triangle(n - 1, *tmp)
triangle(N, *a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = input()
x = [0]*len(s)
y = []
c = 0
for i in s:
    x[c] = int(i)
    c+=1
for _ in range(N-1):
    y = [0]*(len(x)-1)
    for i in range(len(x)-1):
        y[i] = abs(x[i] - x[i+1])
    x=y
print(y[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
x_1 = list(input())

def a(x):
    result = list()
    if len(x) <= 1:
        return x
    
    for i in range(len(x)-1):
        result.append(abs(int(x[i])-int(x[i+1])))
    kaeri = a(result)
    return kaeri

b = a(x_1)
c = b[0]
print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
al = [int(x) for x in list(input())]


def gen(al):
    new_al = [abs(al[i] - al[i + 1]) for i in range(len(al) - 1)]
    return new_al


for i in range(n - 1):
    al = gen(al)

if len(al) == 1:
    print(al[0])
else:
    print('Noooo!')

Submission status: Time Limit Exceeded

Solution code:
import sys
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: Time Limit Exceeded

Solution code:
n = int(input())
a_list = input()

before_list = a_list
for y in range(1, n):
    next_list = []
    for x in range(len(before_list)-1):
        if y == 1:
            next_value = abs(int(a_list[x]) - int(a_list[x+1]))
            next_list.append(next_value)
        else:
            next_value = abs(before_list[x] - before_list[x+1])
            next_list.append(next_value)

    before_list = next_list
    next_list = []

print(before_list[0])

Submission status: Time Limit Exceeded

Solution code:
i=int(input())
s=input()
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=[]
  for k in range(len(a)-1):
    b=abs(a[k]-a[k+1])
    c.append(b)
  a=c
print(str(a[0]))

Submission status: Time Limit Exceeded

Solution code:
def calc(P):
    #Pはリスト
    Q=[]
    for str in A:
        if str=="1":
            pass
        elif str=="2":
            Q.append("1")
        else:
            Q.append("2")
    return Q

def solve(P):
    #Pはリスト
    N=len(P)
    if N>1:
        Q=[]
        for i in range(N-1):
            if   abs( int(P[i])-int(P[i+1]) )==0:
                Q.append("0")
            elif abs( int(P[i])-int(P[i+1]) )==1:
                Q.append("1")
            else:
                Q.append("2")
        return solve(Q)
    elif N==0:
    	return(0)
    else:
        return P[0]

N=int(input())
A=input()
B=list(A)

#print(A)
#print(B) 
short=calc(B)
#print(short)

ans=solve(short)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
seq = list(map(int, input()))
get_sub_seq = lambda l:[abs(l[i]-l[i+1]) for i in range(len(l)-1)]
while len(seq) > 1:
    seq = get_sub_seq(seq)
print(seq[0])

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
b=input()
b=list(map(int,b))
for j in range(a):
 for i in range(len(b)-(j+1)):
    c=abs(b[i]-b[i+1])
    b[i]=c
 v=b[:-j]
print(v[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = list(map(int, input()))

before_list = a_list
for y in range(1, n):
    next_list = []
    for x in range(len(before_list)-1):
        if y == 1:
            # next_value = abs(x_list[y-1][x] - x_list[y-1][x+1])
            next_value = abs(a_list[x] - a_list[x+1])
            next_list.append(next_value)
        else:
            next_value = abs(before_list[x] - before_list[x+1])
            next_list.append(next_value)

    before_list = next_list
    next_list = []

print(before_list[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int,input()))
x = [[] for i in range(N)]
#print(x)2次元リスト
for i in range(N):
  if i == 0:
    x[i] = a
    #print(x[i])
  else:
    tmp = []
    for j in range(N - i):
      tmp.append(abs(x[i-1][j]-x[i-1][j+1]))
    x[i] = tmp
#print(a)
print(x[-1][0])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math
from functools import lru_cache

n = int(input())
nums = int(input())
str_list = list(str(nums))

nums_list = [ int(i) for i in str_list ]

def keisan(nums_list):
  result = [ abs(nums_list[i] - nums_list[i+1]) for i in range(len(nums_list)-1) ]
  if len(result) > 1:
    return keisan(result)
  else:
    return result

nums_list = keisan(nums_list)  
print(nums_list[0])

Submission status: Time Limit Exceeded

Solution code:
n=input()
a=input()
b=""
c=[]
while True:
    if len(a)==1:
        print(a[0])
        break
    c=[abs(int(a[i+1])-int(a[i])) for i in range(len(a)-1)]
    a=c
    c=[]

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


def array_subtraction(arr):
    while len(arr) !=1:
        arr = np.abs(arr[1:]-arr[:-1])
        yield arr
    yield arr


def main():
  input()
  z = np.array([int(i) for i in input()])
  for num in array_subtraction(z):
    pass
  print(*num)

if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
import math


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

#print(data)


for end in range(len(data)-1,0,-1):
#    print(end)
    for i in range(0,end):
        data[i] = abs(data[i]-data[i+1])

#    print(data)

print(data[0])

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
b=input()
b=list(map(int,b))
for j in range(a):
 for i in range(len(b)-(j+1)):
    c=b[i]-b[i+1]
    if c<0:
        c=-c
    b[i]=c
 v=b[:-j]
print(v[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ls = list(input())
while len(ls)>1:
    temp = []
    for i,v in enumerate(ls[:-1]):
        temp.append(abs(int(ls[i+1])-int(v)))
    ls.clear()
    ls = temp[:]
print(ls[0])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N=int(input())
A = input()
a = np.array([int(A[i]) for i in range(len(A))])
while 1:
  a = np.absolute(np.diff(np.absolute(a),1))
  if len(a) == 1:
    print(a[0])
    break

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
a = list(input())
count = N-1
for n in range(1,N):
    for i in range(count):
        a[i] = abs(int(a[i])-int(a[i+1]))
    count -= 1
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = list(input())

X = {}
for i in range(N):
    X[(1,i)] = int(A[i])


for i in range(2,N+1):
    for j in range(N- i+1):
        X[(i,j)] = abs(X[(i-1,j)] - X[(i-1, j+1)])

print(X[(N,0)])