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:
# coding: utf-8

A = set([(0, 0, 0, 0, 0), (0, 0, 0, 1, 0), (0, 0, 0, 1, 2), (0, 0, 0, 2, 0), (0, 0, 0, 3, 0), (0, 0, 1, 0, 0), (0, 0, 1, 0, 2), (0, 0, 1, 1, 0), (0, 0, 1, 1, 2), (0, 0, 1, 2, 0), (0, 0, 1, 2, 2), (0, 0, 1, 3, 0), (0, 0, 1, 3, 2), (0, 0, 2, 0, 0), (0, 0, 2, 1, 0), (0, 0, 2, 1, 2), (0, 0, 2, 2, 0), (0, 0, 2, 3, 0), (0, 0, 2, 3, 2), (0, 0, 3, 0, 0), (0, 0, 3, 2, 0), (0, 0, 3, 2, 2), (0, 0, 3, 3, 0), (0, 1, 0, 0, 0), (0, 1, 0, 0, 2), (0, 1, 0, 1, 0), (0, 1, 0, 1, 2), (0, 1, 0, 2, 0), (0, 1, 0, 2, 2), (0, 1, 0, 3, 0), (0, 1, 1, 0, 0), (0, 1, 1, 0, 2), (0, 1, 1, 1, 0), (0, 1, 1, 1, 2), (0, 1, 1, 2, 0), (0, 1, 1, 2, 2), (0, 1, 1, 3, 0), (0, 1, 1, 3, 2), (0, 1, 2, 0, 0), (0, 1, 2, 0, 2), (0, 1, 2, 1, 0), (0, 1, 2, 1, 2), (0, 1, 2, 2, 0), (0, 1, 2, 2, 2), (0, 1, 2, 3, 2), (0, 1, 3, 0, 2), (0, 1, 3, 1, 0), (0, 1, 3, 1, 2), (0, 1, 3, 2, 0), (0, 1, 3, 2, 2), (0, 1, 3, 3, 0), (0, 1, 3, 3, 2), (0, 2, 0, 0, 0), (0, 2, 0, 1, 0), (0, 2, 0, 1, 2), (0, 2, 0, 2, 0), (0, 2, 0, 3, 0), (0, 2, 0, 3, 2), (0, 2, 1, 0, 0), (0, 2, 1, 0, 2), (0, 2, 1, 1, 0), (0, 2, 1, 1, 2), (0, 2, 1, 2, 0), (0, 2, 1, 2, 2), (0, 2, 1, 3, 0), (0, 2, 1, 3, 2), (0, 2, 2, 0, 0), (0, 2, 2, 1, 0), (0, 2, 2, 1, 2), (0, 2, 2, 2, 0), (0, 2, 2, 3, 0), (0, 2, 2, 3, 2), (0, 2, 3, 0, 0), (0, 2, 3, 0, 2), (0, 2, 3, 1, 0), (0, 2, 3, 1, 2), (0, 2, 3, 2, 0), (0, 2, 3, 2, 2), (0, 2, 3, 3, 2), (0, 3, 0, 0, 0), (0, 3, 0, 1, 0), (0, 3, 0, 1, 2), (0, 3, 0, 2, 0), (0, 3, 0, 2, 2), (0, 3, 0, 3, 0), (0, 3, 1, 0, 0), (0, 3, 1, 0, 2), (0, 3, 1, 1, 0), (0, 3, 1, 1, 2), (0, 3, 1, 2, 0), (0, 3, 1, 2, 2), (0, 3, 1, 3, 0), (0, 3, 1, 3, 2), (0, 3, 2, 0, 0), (0, 3, 2, 0, 2), (0, 3, 2, 2, 0), (0, 3, 2, 2, 2), (0, 3, 2, 3, 0), (0, 3, 3, 0, 0), (0, 3, 3, 1, 0), (0, 3, 3, 1, 2), (0, 3, 3, 3, 0), (1, 0, 0, 0, 1), (1, 0, 0, 1, 1), (1, 0, 0, 1, 3), (1, 0, 0, 2, 1), (1, 0, 0, 2, 3), (1, 0, 0, 3, 1), (1, 0, 1, 0, 1), (1, 0, 1, 1, 1), (1, 0, 1, 1, 3), (1, 0, 1, 2, 1), (1, 0, 1, 2, 3), (1, 0, 1, 3, 1), (1, 0, 1, 3, 3), (1, 0, 2, 0, 1), (1, 0, 2, 0, 3), (1, 0, 2, 1, 1), (1, 0, 2, 1, 3), (1, 0, 2, 2, 1), (1, 0, 2, 2, 3), (1, 0, 2, 3, 1), (1, 0, 2, 3, 3), (1, 0, 3, 0, 1), (1, 0, 3, 1, 1), (1, 0, 3, 1, 3), (1, 0, 3, 3, 1), (1, 1, 0, 0, 1), (1, 1, 0, 1, 1), (1, 1, 0, 1, 3), (1, 1, 0, 2, 1), (1, 1, 0, 2, 3), (1, 1, 0, 3, 1), (1, 1, 0, 3, 3), (1, 1, 1, 0, 1), (1, 1, 1, 0, 3), (1, 1, 1, 1, 1), (1, 1, 1, 2, 1), (1, 1, 1, 2, 3), (1, 1, 1, 3, 1), (1, 1, 2, 0, 1), (1, 1, 2, 0, 3), (1, 1, 2, 1, 1), (1, 1, 2, 1, 3), (1, 1, 2, 2, 1), (1, 1, 2, 2, 3), (1, 1, 2, 3, 1), (1, 1, 2, 3, 3), (1, 1, 3, 0, 1), (1, 1, 3, 0, 3), (1, 1, 3, 1, 1), (1, 1, 3, 2, 1), (1, 1, 3, 2, 3), (1, 1, 3, 3, 1), (1, 2, 0, 0, 1), (1, 2, 0, 0, 3), (1, 2, 0, 1, 1), (1, 2, 0, 1, 3), (1, 2, 0, 2, 1), (1, 2, 0, 2, 3), (1, 2, 0, 3, 1), (1, 2, 1, 0, 1), (1, 2, 1, 1, 1), (1, 2, 1, 1, 3), (1, 2, 1, 2, 1), (1, 2, 1, 2, 3), (1, 2, 1, 3, 1), (1, 2, 1, 3, 3), (1, 2, 2, 0, 1), (1, 2, 2, 0, 3), (1, 2, 2, 1, 1), (1, 2, 2, 1, 3), (1, 2, 2, 2, 1), (1, 2, 2, 2, 3), (1, 2, 2, 3, 1), (1, 2, 2, 3, 3), (1, 2, 3, 0, 3), (1, 2, 3, 1, 1), (1, 2, 3, 1, 3), (1, 2, 3, 2, 1), (1, 2, 3, 2, 3), (1, 2, 3, 3, 1), (1, 2, 3, 3, 3), (1, 3, 0, 0, 1), (1, 3, 0, 1, 1), (1, 3, 0, 1, 3), (1, 3, 0, 2, 1), (1, 3, 0, 2, 3), (1, 3, 0, 3, 1), (1, 3, 1, 0, 1), (1, 3, 1, 0, 3), (1, 3, 1, 1, 1), (1, 3, 1, 2, 1), (1, 3, 1, 2, 3), (1, 3, 1, 3, 1), (1, 3, 2, 0, 1), (1, 3, 2, 0, 3), (1, 3, 2, 1, 1), (1, 3, 2, 1, 3), (1, 3, 2, 2, 1), (1, 3, 2, 2, 3), (1, 3, 2, 3, 1), (1, 3, 2, 3, 3), (1, 3, 3, 0, 1), (1, 3, 3, 1, 1), (1, 3, 3, 2, 1), (1, 3, 3, 2, 3), (1, 3, 3, 3, 1), (2, 0, 0, 0, 2), (2, 0, 0, 1, 0), (2, 0, 0, 1, 2), (2, 0, 0, 2, 2), (2, 0, 0, 3, 2), (2, 0, 1, 0, 0), (2, 0, 1, 0, 2), (2, 0, 1, 1, 0), (2, 0, 1, 1, 2), (2, 0, 1, 2, 0), (2, 0, 1, 2, 2), (2, 0, 1, 3, 0), (2, 0, 1, 3, 2), (2, 0, 2, 0, 2), (2, 0, 2, 1, 0), (2, 0, 2, 1, 2), (2, 0, 2, 2, 2), (2, 0, 2, 3, 0), (2, 0, 2, 3, 2), (2, 0, 3, 0, 2), (2, 0, 3, 1, 0), (2, 0, 3, 1, 2), (2, 0, 3, 2, 0), (2, 0, 3, 2, 2), (2, 0, 3, 3, 2), (2, 1, 0, 0, 0), (2, 1, 0, 0, 2), (2, 1, 0, 1, 0), (2, 1, 0, 1, 2), (2, 1, 0, 2, 0), (2, 1, 0, 2, 2), (2, 1, 0, 3, 0), (2, 1, 1, 0, 0), (2, 1, 1, 0, 2), (2, 1, 1, 1, 0), (2, 1, 1, 1, 2), (2, 1, 1, 2, 0), (2, 1, 1, 2, 2), (2, 1, 1, 3, 0), (2, 1, 1, 3, 2), (2, 1, 2, 0, 0), (2, 1, 2, 0, 2), (2, 1, 2, 1, 0), (2, 1, 2, 1, 2), (2, 1, 2, 2, 0), (2, 1, 2, 2, 2), (2, 1, 2, 3, 2), (2, 1, 3, 0, 2), (2, 1, 3, 1, 0), (2, 1, 3, 1, 2), (2, 1, 3, 2, 0), (2, 1, 3, 2, 2), (2, 1, 3, 3, 0), (2, 1, 3, 3, 2), (2, 2, 0, 0, 2), (2, 2, 0, 1, 0), (2, 2, 0, 1, 2), (2, 2, 0, 2, 2), (2, 2, 0, 3, 0), (2, 2, 0, 3, 2), (2, 2, 1, 0, 0), (2, 2, 1, 0, 2), (2, 2, 1, 1, 0), (2, 2, 1, 1, 2), (2, 2, 1, 2, 0), (2, 2, 1, 2, 2), (2, 2, 1, 3, 0), (2, 2, 1, 3, 2), (2, 2, 2, 0, 2), (2, 2, 2, 1, 0), (2, 2, 2, 1, 2), (2, 2, 2, 2, 2), (2, 2, 2, 3, 0), (2, 2, 2, 3, 2), (2, 2, 3, 0, 0), (2, 2, 3, 0, 2), (2, 2, 3, 1, 0), (2, 2, 3, 1, 2), (2, 2, 3, 2, 0), (2, 2, 3, 2, 2), (2, 2, 3, 3, 2), (2, 3, 0, 0, 2), (2, 3, 0, 2, 0), (2, 3, 0, 2, 2), (2, 3, 0, 3, 2), (2, 3, 1, 0, 0), (2, 3, 1, 0, 2), (2, 3, 1, 1, 0), (2, 3, 1, 1, 2), (2, 3, 1, 2, 0), (2, 3, 1, 2, 2), (2, 3, 1, 3, 0), (2, 3, 1, 3, 2), (2, 3, 2, 0, 0), (2, 3, 2, 0, 2), (2, 3, 2, 1, 0), (2, 3, 2, 1, 2), (2, 3, 2, 2, 0), (2, 3, 2, 2, 2), (2, 3, 2, 3, 2), (2, 3, 3, 0, 2), (2, 3, 3, 1, 0), (2, 3, 3, 1, 2), (2, 3, 3, 2, 0), (2, 3, 3, 2, 2), (2, 3, 3, 3, 2), (3, 0, 0, 0, 3), (3, 0, 0, 2, 1), (3, 0, 0, 2, 3), (3, 0, 0, 3, 3), (3, 0, 1, 0, 3), (3, 0, 1, 1, 1), (3, 0, 1, 1, 3), (3, 0, 1, 3, 1), (3, 0, 1, 3, 3), (3, 0, 2, 0, 1), (3, 0, 2, 0, 3), (3, 0, 2, 1, 1), (3, 0, 2, 1, 3), (3, 0, 2, 2, 1), (3, 0, 2, 2, 3), (3, 0, 2, 3, 1), (3, 0, 2, 3, 3), (3, 0, 3, 0, 3), (3, 0, 3, 1, 1), (3, 0, 3, 1, 3), (3, 0, 3, 2, 1), (3, 0, 3, 2, 3), (3, 0, 3, 3, 3), (3, 1, 0, 0, 1), (3, 1, 0, 1, 1), (3, 1, 0, 1, 3), (3, 1, 0, 2, 1), (3, 1, 0, 2, 3), (3, 1, 0, 3, 1), (3, 1, 0, 3, 3), (3, 1, 1, 0, 1), (3, 1, 1, 0, 3), (3, 1, 1, 1, 3), (3, 1, 1, 2, 1), (3, 1, 1, 2, 3), (3, 1, 1, 3, 3), (3, 1, 2, 0, 1), (3, 1, 2, 0, 3), (3, 1, 2, 1, 1), (3, 1, 2, 1, 3), (3, 1, 2, 2, 1), (3, 1, 2, 2, 3), (3, 1, 2, 3, 1), (3, 1, 2, 3, 3), (3, 1, 3, 0, 1), (3, 1, 3, 0, 3), (3, 1, 3, 1, 3), (3, 1, 3, 2, 1), (3, 1, 3, 2, 3), (3, 1, 3, 3, 3), (3, 2, 0, 0, 1), (3, 2, 0, 0, 3), (3, 2, 0, 1, 1), (3, 2, 0, 1, 3), (3, 2, 0, 2, 1), (3, 2, 0, 2, 3), (3, 2, 0, 3, 1), (3, 2, 1, 0, 1), (3, 2, 1, 1, 1), (3, 2, 1, 1, 3), (3, 2, 1, 2, 1), (3, 2, 1, 2, 3), (3, 2, 1, 3, 1), (3, 2, 1, 3, 3), (3, 2, 2, 0, 1), (3, 2, 2, 0, 3), (3, 2, 2, 1, 1), (3, 2, 2, 1, 3), (3, 2, 2, 2, 1), (3, 2, 2, 2, 3), (3, 2, 2, 3, 1), (3, 2, 2, 3, 3), (3, 2, 3, 0, 3), (3, 2, 3, 1, 1), (3, 2, 3, 1, 3), (3, 2, 3, 2, 1), (3, 2, 3, 2, 3), (3, 2, 3, 3, 1), (3, 2, 3, 3, 3), (3, 3, 0, 0, 3), (3, 3, 0, 1, 1), (3, 3, 0, 1, 3), (3, 3, 0, 3, 3), (3, 3, 1, 0, 1), (3, 3, 1, 0, 3), (3, 3, 1, 1, 3), (3, 3, 1, 2, 1), (3, 3, 1, 2, 3), (3, 3, 1, 3, 3), (3, 3, 2, 0, 1), (3, 3, 2, 0, 3), (3, 3, 2, 1, 1), (3, 3, 2, 1, 3), (3, 3, 2, 2, 1), (3, 3, 2, 2, 3), (3, 3, 2, 3, 1), (3, 3, 2, 3, 3), (3, 3, 3, 0, 3), (3, 3, 3, 1, 3), (3, 3, 3, 2, 1), (3, 3, 3, 2, 3), (3, 3, 3, 3, 3)])
B = set([(0, 0, 0, 0, 1), (0, 0, 0, 1, 1), (0, 0, 0, 1, 3), (0, 0, 0, 2, 1), (0, 0, 0, 2, 3), (0, 0, 0, 3, 1), (0, 0, 1, 0, 1), (0, 0, 1, 0, 3), (0, 0, 1, 1, 1), (0, 0, 1, 1, 3), (0, 0, 1, 2, 1), (0, 0, 1, 2, 3), (0, 0, 1, 3, 1), (0, 0, 1, 3, 3), (0, 0, 2, 0, 1), (0, 0, 2, 0, 3), (0, 0, 2, 1, 1), (0, 0, 2, 1, 3), (0, 0, 2, 2, 1), (0, 0, 2, 2, 3), (0, 0, 2, 3, 1), (0, 0, 2, 3, 3), (0, 0, 3, 0, 1), (0, 0, 3, 1, 1), (0, 0, 3, 1, 3), (0, 0, 3, 2, 1), (0, 0, 3, 2, 3), (0, 0, 3, 3, 1), (0, 1, 0, 0, 1), (0, 1, 0, 0, 3), (0, 1, 0, 1, 1), (0, 1, 0, 1, 3), (0, 1, 0, 2, 1), (0, 1, 0, 2, 3), (0, 1, 0, 3, 1), (0, 1, 0, 3, 3), (0, 1, 1, 0, 1), (0, 1, 1, 0, 3), (0, 1, 1, 1, 1), (0, 1, 1, 1, 3), (0, 1, 1, 2, 1), (0, 1, 1, 2, 3), (0, 1, 1, 3, 1), (0, 1, 1, 3, 3), (0, 1, 2, 0, 1), (0, 1, 2, 0, 3), (0, 1, 2, 1, 1), (0, 1, 2, 1, 3), (0, 1, 2, 2, 1), (0, 1, 2, 2, 3), (0, 1, 2, 3, 1), (0, 1, 2, 3, 3), (0, 1, 3, 0, 1), (0, 1, 3, 0, 3), (0, 1, 3, 1, 1), (0, 1, 3, 1, 3), (0, 1, 3, 2, 1), (0, 1, 3, 2, 3), (0, 1, 3, 3, 1), (0, 1, 3, 3, 3), (0, 2, 0, 0, 1), (0, 2, 0, 0, 3), (0, 2, 0, 1, 1), (0, 2, 0, 1, 3), (0, 2, 0, 2, 1), (0, 2, 0, 2, 3), (0, 2, 0, 3, 1), (0, 2, 0, 3, 3), (0, 2, 1, 0, 1), (0, 2, 1, 0, 3), (0, 2, 1, 1, 1), (0, 2, 1, 1, 3), (0, 2, 1, 2, 1), (0, 2, 1, 2, 3), (0, 2, 1, 3, 1), (0, 2, 1, 3, 3), (0, 2, 2, 0, 1), (0, 2, 2, 0, 3), (0, 2, 2, 1, 1), (0, 2, 2, 1, 3), (0, 2, 2, 2, 1), (0, 2, 2, 2, 3), (0, 2, 2, 3, 1), (0, 2, 2, 3, 3), (0, 2, 3, 0, 1), (0, 2, 3, 0, 3), (0, 2, 3, 1, 1), (0, 2, 3, 1, 3), (0, 2, 3, 2, 1), (0, 2, 3, 2, 3), (0, 2, 3, 3, 1), (0, 2, 3, 3, 3), (0, 3, 0, 0, 1), (0, 3, 0, 1, 1), (0, 3, 0, 1, 3), (0, 3, 0, 2, 1), (0, 3, 0, 2, 3), (0, 3, 0, 3, 1), (0, 3, 1, 0, 1), (0, 3, 1, 0, 3), (0, 3, 1, 1, 1), (0, 3, 1, 1, 3), (0, 3, 1, 2, 1), (0, 3, 1, 2, 3), (0, 3, 1, 3, 1), (0, 3, 1, 3, 3), (0, 3, 2, 0, 1), (0, 3, 2, 0, 3), (0, 3, 2, 1, 1), (0, 3, 2, 1, 3), (0, 3, 2, 2, 1), (0, 3, 2, 2, 3), (0, 3, 2, 3, 1), (0, 3, 2, 3, 3), (0, 3, 3, 0, 1), (0, 3, 3, 1, 1), (0, 3, 3, 1, 3), (0, 3, 3, 2, 1), (0, 3, 3, 2, 3), (0, 3, 3, 3, 1), (1, 0, 0, 0, 0), (1, 0, 0, 0, 2), (1, 0, 0, 1, 0), (1, 0, 0, 1, 2), (1, 0, 0, 2, 0), (1, 0, 0, 2, 2), (1, 0, 0, 3, 0), (1, 0, 0, 3, 2), (1, 0, 1, 0, 0), (1, 0, 1, 0, 2), (1, 0, 1, 1, 0), (1, 0, 1, 1, 2), (1, 0, 1, 2, 0), (1, 0, 1, 2, 2), (1, 0, 1, 3, 0), (1, 0, 1, 3, 2), (1, 0, 2, 0, 0), (1, 0, 2, 0, 2), (1, 0, 2, 1, 0), (1, 0, 2, 1, 2), (1, 0, 2, 2, 0), (1, 0, 2, 2, 2), (1, 0, 2, 3, 0), (1, 0, 2, 3, 2), (1, 0, 3, 0, 0), (1, 0, 3, 0, 2), (1, 0, 3, 1, 0), (1, 0, 3, 1, 2), (1, 0, 3, 2, 0), (1, 0, 3, 2, 2), (1, 0, 3, 3, 0), (1, 0, 3, 3, 2), (1, 1, 0, 0, 0), (1, 1, 0, 0, 2), (1, 1, 0, 1, 0), (1, 1, 0, 1, 2), (1, 1, 0, 2, 0), (1, 1, 0, 2, 2), (1, 1, 0, 3, 0), (1, 1, 0, 3, 2), (1, 1, 1, 0, 0), (1, 1, 1, 0, 2), (1, 1, 1, 1, 0), (1, 1, 1, 1, 2), (1, 1, 1, 2, 0), (1, 1, 1, 2, 2), (1, 1, 1, 3, 0), (1, 1, 1, 3, 2), (1, 1, 2, 0, 0), (1, 1, 2, 0, 2), (1, 1, 2, 1, 0), (1, 1, 2, 1, 2), (1, 1, 2, 2, 0), (1, 1, 2, 2, 2), (1, 1, 2, 3, 0), (1, 1, 2, 3, 2), (1, 1, 3, 0, 0), (1, 1, 3, 0, 2), (1, 1, 3, 1, 0), (1, 1, 3, 1, 2), (1, 1, 3, 2, 0), (1, 1, 3, 2, 2), (1, 1, 3, 3, 0), (1, 1, 3, 3, 2), (1, 2, 0, 0, 0), (1, 2, 0, 0, 2), (1, 2, 0, 1, 0), (1, 2, 0, 1, 2), (1, 2, 0, 2, 0), (1, 2, 0, 2, 2), (1, 2, 0, 3, 0), (1, 2, 0, 3, 2), (1, 2, 1, 0, 0), (1, 2, 1, 0, 2), (1, 2, 1, 1, 0), (1, 2, 1, 1, 2), (1, 2, 1, 2, 0), (1, 2, 1, 2, 2), (1, 2, 1, 3, 0), (1, 2, 1, 3, 2), (1, 2, 2, 0, 0), (1, 2, 2, 0, 2), (1, 2, 2, 1, 0), (1, 2, 2, 1, 2), (1, 2, 2, 2, 0), (1, 2, 2, 2, 2), (1, 2, 2, 3, 0), (1, 2, 2, 3, 2), (1, 2, 3, 0, 0), (1, 2, 3, 0, 2), (1, 2, 3, 1, 0), (1, 2, 3, 1, 2), (1, 2, 3, 2, 0), (1, 2, 3, 2, 2), (1, 2, 3, 3, 0), (1, 2, 3, 3, 2), (1, 3, 0, 0, 0), (1, 3, 0, 0, 2), (1, 3, 0, 1, 0), (1, 3, 0, 1, 2), (1, 3, 0, 2, 0), (1, 3, 0, 2, 2), (1, 3, 0, 3, 0), (1, 3, 0, 3, 2), (1, 3, 1, 0, 0), (1, 3, 1, 0, 2), (1, 3, 1, 1, 0), (1, 3, 1, 1, 2), (1, 3, 1, 2, 0), (1, 3, 1, 2, 2), (1, 3, 1, 3, 0), (1, 3, 1, 3, 2), (1, 3, 2, 0, 0), (1, 3, 2, 0, 2), (1, 3, 2, 1, 0), (1, 3, 2, 1, 2), (1, 3, 2, 2, 0), (1, 3, 2, 2, 2), (1, 3, 2, 3, 0), (1, 3, 2, 3, 2), (1, 3, 3, 0, 0), (1, 3, 3, 0, 2), (1, 3, 3, 1, 0), (1, 3, 3, 1, 2), (1, 3, 3, 2, 0), (1, 3, 3, 2, 2), (1, 3, 3, 3, 0), (1, 3, 3, 3, 2), (2, 0, 0, 0, 1), (2, 0, 0, 0, 3), (2, 0, 0, 1, 1), (2, 0, 0, 1, 3), (2, 0, 0, 2, 1), (2, 0, 0, 2, 3), (2, 0, 0, 3, 1), (2, 0, 0, 3, 3), (2, 0, 1, 0, 1), (2, 0, 1, 0, 3), (2, 0, 1, 1, 1), (2, 0, 1, 1, 3), (2, 0, 1, 2, 1), (2, 0, 1, 2, 3), (2, 0, 1, 3, 1), (2, 0, 1, 3, 3), (2, 0, 2, 0, 1), (2, 0, 2, 0, 3), (2, 0, 2, 1, 1), (2, 0, 2, 1, 3), (2, 0, 2, 2, 1), (2, 0, 2, 2, 3), (2, 0, 2, 3, 1), (2, 0, 2, 3, 3), (2, 0, 3, 0, 1), (2, 0, 3, 0, 3), (2, 0, 3, 1, 1), (2, 0, 3, 1, 3), (2, 0, 3, 2, 1), (2, 0, 3, 2, 3), (2, 0, 3, 3, 1), (2, 0, 3, 3, 3), (2, 1, 0, 0, 1), (2, 1, 0, 0, 3), (2, 1, 0, 1, 1), (2, 1, 0, 1, 3), (2, 1, 0, 2, 1), (2, 1, 0, 2, 3), (2, 1, 0, 3, 1), (2, 1, 0, 3, 3), (2, 1, 1, 0, 1), (2, 1, 1, 0, 3), (2, 1, 1, 1, 1), (2, 1, 1, 1, 3), (2, 1, 1, 2, 1), (2, 1, 1, 2, 3), (2, 1, 1, 3, 1), (2, 1, 1, 3, 3), (2, 1, 2, 0, 1), (2, 1, 2, 0, 3), (2, 1, 2, 1, 1), (2, 1, 2, 1, 3), (2, 1, 2, 2, 1), (2, 1, 2, 2, 3), (2, 1, 2, 3, 1), (2, 1, 2, 3, 3), (2, 1, 3, 0, 1), (2, 1, 3, 0, 3), (2, 1, 3, 1, 1), (2, 1, 3, 1, 3), (2, 1, 3, 2, 1), (2, 1, 3, 2, 3), (2, 1, 3, 3, 1), (2, 1, 3, 3, 3), (2, 2, 0, 0, 1), (2, 2, 0, 0, 3), (2, 2, 0, 1, 1), (2, 2, 0, 1, 3), (2, 2, 0, 2, 1), (2, 2, 0, 2, 3), (2, 2, 0, 3, 1), (2, 2, 0, 3, 3), (2, 2, 1, 0, 1), (2, 2, 1, 0, 3), (2, 2, 1, 1, 1), (2, 2, 1, 1, 3), (2, 2, 1, 2, 1), (2, 2, 1, 2, 3), (2, 2, 1, 3, 1), (2, 2, 1, 3, 3), (2, 2, 2, 0, 1), (2, 2, 2, 0, 3), (2, 2, 2, 1, 1), (2, 2, 2, 1, 3), (2, 2, 2, 2, 1), (2, 2, 2, 2, 3), (2, 2, 2, 3, 1), (2, 2, 2, 3, 3), (2, 2, 3, 0, 1), (2, 2, 3, 0, 3), (2, 2, 3, 1, 1), (2, 2, 3, 1, 3), (2, 2, 3, 2, 1), (2, 2, 3, 2, 3), (2, 2, 3, 3, 1), (2, 2, 3, 3, 3), (2, 3, 0, 0, 1), (2, 3, 0, 0, 3), (2, 3, 0, 1, 1), (2, 3, 0, 1, 3), (2, 3, 0, 2, 1), (2, 3, 0, 2, 3), (2, 3, 0, 3, 1), (2, 3, 0, 3, 3), (2, 3, 1, 0, 1), (2, 3, 1, 0, 3), (2, 3, 1, 1, 1), (2, 3, 1, 1, 3), (2, 3, 1, 2, 1), (2, 3, 1, 2, 3), (2, 3, 1, 3, 1), (2, 3, 1, 3, 3), (2, 3, 2, 0, 1), (2, 3, 2, 0, 3), (2, 3, 2, 1, 1), (2, 3, 2, 1, 3), (2, 3, 2, 2, 1), (2, 3, 2, 2, 3), (2, 3, 2, 3, 1), (2, 3, 2, 3, 3), (2, 3, 3, 0, 1), (2, 3, 3, 0, 3), (2, 3, 3, 1, 1), (2, 3, 3, 1, 3), (2, 3, 3, 2, 1), (2, 3, 3, 2, 3), (2, 3, 3, 3, 1), (2, 3, 3, 3, 3), (3, 0, 0, 0, 2), (3, 0, 0, 1, 0), (3, 0, 0, 1, 2), (3, 0, 0, 2, 0), (3, 0, 0, 2, 2), (3, 0, 0, 3, 2), (3, 0, 1, 0, 0), (3, 0, 1, 0, 2), (3, 0, 1, 1, 0), (3, 0, 1, 1, 2), (3, 0, 1, 2, 0), (3, 0, 1, 2, 2), (3, 0, 1, 3, 0), (3, 0, 1, 3, 2), (3, 0, 2, 0, 0), (3, 0, 2, 0, 2), (3, 0, 2, 1, 0), (3, 0, 2, 1, 2), (3, 0, 2, 2, 0), (3, 0, 2, 2, 2), (3, 0, 2, 3, 0), (3, 0, 2, 3, 2), (3, 0, 3, 0, 2), (3, 0, 3, 1, 0), (3, 0, 3, 1, 2), (3, 0, 3, 2, 0), (3, 0, 3, 2, 2), (3, 0, 3, 3, 2), (3, 1, 0, 0, 0), (3, 1, 0, 0, 2), (3, 1, 0, 1, 0), (3, 1, 0, 1, 2), (3, 1, 0, 2, 0), (3, 1, 0, 2, 2), (3, 1, 0, 3, 0), (3, 1, 0, 3, 2), (3, 1, 1, 0, 0), (3, 1, 1, 0, 2), (3, 1, 1, 1, 0), (3, 1, 1, 1, 2), (3, 1, 1, 2, 0), (3, 1, 1, 2, 2), (3, 1, 1, 3, 0), (3, 1, 1, 3, 2), (3, 1, 2, 0, 0), (3, 1, 2, 0, 2), (3, 1, 2, 1, 0), (3, 1, 2, 1, 2), (3, 1, 2, 2, 0), (3, 1, 2, 2, 2), (3, 1, 2, 3, 0), (3, 1, 2, 3, 2), (3, 1, 3, 0, 0), (3, 1, 3, 0, 2), (3, 1, 3, 1, 0), (3, 1, 3, 1, 2), (3, 1, 3, 2, 0), (3, 1, 3, 2, 2), (3, 1, 3, 3, 0), (3, 1, 3, 3, 2), (3, 2, 0, 0, 0), (3, 2, 0, 0, 2), (3, 2, 0, 1, 0), (3, 2, 0, 1, 2), (3, 2, 0, 2, 0), (3, 2, 0, 2, 2), (3, 2, 0, 3, 0), (3, 2, 0, 3, 2), (3, 2, 1, 0, 0), (3, 2, 1, 0, 2), (3, 2, 1, 1, 0), (3, 2, 1, 1, 2), (3, 2, 1, 2, 0), (3, 2, 1, 2, 2), (3, 2, 1, 3, 0), (3, 2, 1, 3, 2), (3, 2, 2, 0, 0), (3, 2, 2, 0, 2), (3, 2, 2, 1, 0), (3, 2, 2, 1, 2), (3, 2, 2, 2, 0), (3, 2, 2, 2, 2), (3, 2, 2, 3, 0), (3, 2, 2, 3, 2), (3, 2, 3, 0, 0), (3, 2, 3, 0, 2), (3, 2, 3, 1, 0), (3, 2, 3, 1, 2), (3, 2, 3, 2, 0), (3, 2, 3, 2, 2), (3, 2, 3, 3, 0), (3, 2, 3, 3, 2), (3, 3, 0, 0, 2), (3, 3, 0, 1, 0), (3, 3, 0, 1, 2), (3, 3, 0, 2, 0), (3, 3, 0, 2, 2), (3, 3, 0, 3, 2), (3, 3, 1, 0, 0), (3, 3, 1, 0, 2), (3, 3, 1, 1, 0), (3, 3, 1, 1, 2), (3, 3, 1, 2, 0), (3, 3, 1, 2, 2), (3, 3, 1, 3, 0), (3, 3, 1, 3, 2), (3, 3, 2, 0, 0), (3, 3, 2, 0, 2), (3, 3, 2, 1, 0), (3, 3, 2, 1, 2), (3, 3, 2, 2, 0), (3, 3, 2, 2, 2), (3, 3, 2, 3, 0), (3, 3, 2, 3, 2), (3, 3, 3, 0, 2), (3, 3, 3, 1, 0), (3, 3, 3, 1, 2), (3, 3, 3, 2, 0), (3, 3, 3, 2, 2), (3, 3, 3, 3, 2)])
C = set([(0, 0, 0, 0, 2), (0, 0, 0, 2, 2), (0, 0, 0, 3, 2), (0, 0, 2, 0, 2), (0, 0, 2, 2, 2), (0, 0, 3, 0, 2), (0, 0, 3, 1, 0), (0, 0, 3, 1, 2), (0, 0, 3, 3, 2), (0, 1, 0, 3, 2), (0, 1, 2, 3, 0), (0, 1, 3, 0, 0), (0, 2, 0, 0, 2), (0, 2, 0, 2, 2), (0, 2, 2, 0, 2), (0, 2, 2, 2, 2), (0, 2, 3, 3, 0), (0, 3, 0, 0, 2), (0, 3, 0, 3, 2), (0, 3, 2, 1, 0), (0, 3, 2, 1, 2), (0, 3, 2, 3, 2), (0, 3, 3, 0, 2), (0, 3, 3, 2, 0), (0, 3, 3, 2, 2), (0, 3, 3, 3, 2), (1, 0, 0, 0, 3), (1, 0, 0, 3, 3), (1, 0, 1, 0, 3), (1, 0, 3, 0, 3), (1, 0, 3, 2, 1), (1, 0, 3, 2, 3), (1, 0, 3, 3, 3), (1, 1, 0, 0, 3), (1, 1, 1, 1, 3), (1, 1, 1, 3, 3), (1, 1, 3, 1, 3), (1, 1, 3, 3, 3), (1, 2, 0, 3, 3), (1, 2, 1, 0, 3), (1, 2, 3, 0, 1), (1, 3, 0, 0, 3), (1, 3, 0, 3, 3), (1, 3, 1, 1, 3), (1, 3, 1, 3, 3), (1, 3, 3, 0, 3), (1, 3, 3, 1, 3), (1, 3, 3, 3, 3), (2, 0, 0, 0, 0), (2, 0, 0, 2, 0), (2, 0, 0, 3, 0), (2, 0, 2, 0, 0), (2, 0, 2, 2, 0), (2, 0, 3, 0, 0), (2, 0, 3, 3, 0), (2, 1, 0, 3, 2), (2, 1, 2, 3, 0), (2, 1, 3, 0, 0), (2, 2, 0, 0, 0), (2, 2, 0, 2, 0), (2, 2, 2, 0, 0), (2, 2, 2, 2, 0), (2, 2, 3, 3, 0), (2, 3, 0, 0, 0), (2, 3, 0, 1, 0), (2, 3, 0, 1, 2), (2, 3, 0, 3, 0), (2, 3, 2, 3, 0), (2, 3, 3, 0, 0), (2, 3, 3, 3, 0), (3, 0, 0, 0, 1), (3, 0, 0, 1, 1), (3, 0, 0, 1, 3), (3, 0, 0, 3, 1), (3, 0, 1, 0, 1), (3, 0, 1, 2, 1), (3, 0, 1, 2, 3), (3, 0, 3, 0, 1), (3, 0, 3, 3, 1), (3, 1, 0, 0, 3), (3, 1, 1, 1, 1), (3, 1, 1, 3, 1), (3, 1, 3, 1, 1), (3, 1, 3, 3, 1), (3, 2, 0, 3, 3), (3, 2, 1, 0, 3), (3, 2, 3, 0, 1), (3, 3, 0, 0, 1), (3, 3, 0, 2, 1), (3, 3, 0, 2, 3), (3, 3, 0, 3, 1), (3, 3, 1, 1, 1), (3, 3, 1, 3, 1), (3, 3, 3, 0, 1), (3, 3, 3, 1, 1), (3, 3, 3, 3, 1)])


def five(s):
    x = ''
    for p in range(len(s)-4):
        t = (s[p], s[p+1], s[p+2], s[p+3], s[p+4])
        if t in A:
            x.join('0')
        elif t in B:
            x.join('1')
        else:
            x.join('2')
    return x
    
def four(s):
    x = ''
    for p in range(len(s)-1):
        x.join(str(abs(int(s[p])-int(s[p+1]))))
    return x
    
N = int(input())
s = input()
    
while len(s) != 1:
    if len(s) >= 5:
        s = five(s)
    else:
        s = four(s)
    print(s)
            
print(s)


"""
A = []
B = []
C = []

for i in range(4):
    for j in range(4):
        for k in range(4):
            for l in range(4):
                for m  in range(4):
                    x = abs(abs(abs(abs(i-j)-abs(j-k))-abs(abs(j-k)-abs(k-l)))-abs(abs(abs(j-k)-abs(k-l))-abs(abs(k-l)-abs(l-m))))
                    if x == 0:
                        A.append((i,j,k,l,m))
                    if x == 1:
                        B.append((i,j,k,l,m))              
                    if x == 2:
                        C.append((i,j,k,l,m))      
                
print(A)
print(B)
print(C)
"""

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input()
a = [int(i) for i in A]

for n in range(N-1, 0, -1):
    t = []
    for i in range(n):
        t.append(abs(a[i]-a[i+1]))
    a = t
print(*a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())						# 5

g = [ [] for _ in range(N+1) ]			#=> [[], [], [], [], [], [], [], []]
g=[]

g.append(input())

z=0
#for i in range(N):
for j in range(N-1):
	s = g[j]
	x=""
	for k in range(1,len(s)):
		a = int(s[k-1])
		b = int(s[k-0])
		c = abs(a-b)
		x = x + str(c)

	g.append(x)

#print(g)
print(g[N-1])

Submission status: Time Limit Exceeded

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


def solve(*args: str) -> str:
    n = int(args[0])
    A = np.array([int(a) for a in args[1]])

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

    return str(A[0])


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
def resolve():
    N = int(input())
    s = input()
    s = list(map(int, list(s)))
    a = np.array(s)
    for _ in range(N-1):
        a = np.abs(np.diff(a, n=1))
    print(a[0])

if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
n = (int)(input())
s = input()
a = []
x = [[] for j in range(n)]
for i in range(len(s)):
    a.append((int)(s[i]))
    x[0].append((int)(s[i]))

for i in range(1, n, 1):
    #print("i = {}".format(i))
    for j in range(n-i):
        #print(" j = {}".format(j))
        x[i].append(abs(x[i-1][j]-x[i-1][j+1]))
    #print(x)

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    import numpy as np
    import sys
    input = sys.stdin.readline

    N = int(input())
    a = input()[:-1]
    la = np.array([int(c) for c in a])

    na, nd = np.abs, np.diff
    def f(l1):
        return na(nd(l1))

    while(len(la)> 1):
       la =  f(la)

    print(*la)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
num = int(input())
num = str(num)
list_num = list(num)
list_num = [int(s) for s in list_num]
length=N-1
answer=list_num

for count in range(N-1):
    for count_x in range(length):
        answer[count_x]=abs(answer[count_x]-answer[count_x+1])
        count_x+=1
    answer.pop(count_x)
    length-=1
    count+=1

print(answer[0])

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 = []
for i in str_list:
  nums_list.append(int(i))

def keisan(nums_list):
  result = []
  for i in range(len(nums_list)-1):
    tmp = nums_list[i] - nums_list[i+1]
    if tmp < 0:
      tmp = -tmp
    result.append(tmp)
  return result

while len(nums_list) >1:
  nums_list = keisan(nums_list)
  
print(nums_list[0])

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3 ## -*- python -*-
import sys
 
n = int(input())
t = input()
a = []
a.append(-1)    # index 0は使わない
for i in range(n):
    a.append(int(t[i]))

x = [[-1]*(n+1)]*(n+1)  #index 0は使わない
for i,c in enumerate(a):
    x[1][i] = c

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

Submission status: Time Limit Exceeded

Solution code:
n = input()
n = int(n)
num = input()
#print(num)

mat = [[0 for _ in range(n)] for __ in range(n)]
lens = n
for i in range(n):
    for j in range(lens):
        if i == 0:
            mat[i][j] = int(num[j])
        else:
            mat[i][j] = abs(mat[i - 1][j] - mat[i - 1][j + 1])
    lens -= 1
print(mat[n - 1][0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
numstr = input()
l = [int(i) for i in list(str(numstr))]
k = n-1
while k != 0:
    for i, x in enumerate(range(k)):
        l[i] = abs(l[i] - l[i+1])
    k = k-1
print(l[0])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

def func(x1, x2):
    x = abs(x1 - x2)
    return x

def getVal(n, array_str):
    x_org = [int(c) for c in array_str]
    for i in range(n-1):
        x_next = [0] * (n-i-1)
        for j in range(n-i-1):
            x_next[j] = func(x_org[j], x_org[j+1])
        x_org = x_next
    return x_next[0]
        

# print(getVal(4, "1231"))
# print(getVal(10, "2311312312"))

n = int(input())
ar = str(int(input()))
print(getVal(n, ar))

Submission status: Time Limit Exceeded

Solution code:
i=int(input())
s=input()
a=""
for j in range(i-1):
  a=""
  for k in range(len(s)-1):
    a += str(abs((int(s[k])-int(s[k+1]))))
  s=a
print(s)

Submission status: Time Limit Exceeded

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

while t >= 2:
  for i in range (t-1):
    a[i] = abs(a[i]-a[i+1])
  t = t-1
print(a[0])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
input()
z=np.array([int(i) for i in input()])
while len(z)!=1:
    x,y = z[:-1],z[1:]
    t = np.abs(x - y)
    if len(t) == 1:
        break
    z=t
print(*t)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = [[0 for i in range(n)] for j in range(n)]
L[0] = list(map(int, input()))
#print(L[0])
for i in range(1,n):
    for j in range(n-i):
        L[i][j] = abs(L[i-1][j] - L[i-1][j+1])
        #print( L[i][j], L[i-1][j] , L[i-1][j+1])
    #print(L[i])
    
print(L[n-1][0])

Submission status: Time Limit Exceeded

Solution code:
size = int(input())
values = map(int, input())
temp_values = []
pre_value = -1
for exec_count in range(size - 1):
    for value_string in list(values):
        value = int(value_string)
        if pre_value != -1:
            temp_values.append(abs(value - pre_value))
        pre_value = value
    values = temp_values
    temp_values = []
    pre_value = -1
print(values[0])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ln = [int(el) for el in s]
for i in range(n-1):
  for j in range(n-i-1):
    ln[j] = abs( ln[j] - ln[j+1] )
print(ln[0])

Submission status: Time Limit Exceeded

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

result = [[0 for i in range(N)] for j in range(N)]
result[0] = [int(c) for c in given[1]]

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    A = [[-1 for i in range(n - j)] for j in range(n)]
    A[0] = list(map(lambda x: int(x) - 1, input()))
    for i in range(n):
        for j in range(len(A[i]) - 1):
            A[i + 1][j] = abs(A[i][j] - A[i][j + 1])

    print(A[n - 1][0])
    return


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

x = [[0]*(n+1)]*(n+1)

for j in range(n):
    x[1][j+1] = a[j]

for i in range(n-2):
    for j in range(n-1-i):
        x[i+2][j+1] = abs(x[i+1][j+1] - x[i+1][j+2])
        #print('x[',i+2,'][',j+1,']: ' ,x[i+2][j+1])

x[n][1] = abs(x[n-1][1] - x[n-1][2])

print(x[n][1])

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[:-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())
# 1項目
dat = list(map(int, input()))

# 2項目~
def result_val(g_list):
    # 長さ1なら出力
    if(len(g_list) == 1):
        return g_list[0]
    else:
        dat_list = []
        for i in range(len(g_list)-1):
            dat_list.append(abs(g_list[i] - g_list[i+1]))
        return result_val(dat_list)

val = result_val(dat)

print(val)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(20000)

n = int(input())
an = str(input())

# n=4
# an="1231"
# n=10
# an="2311312312"
#n=1000
#an="2312"*(n/4)

# print(n)
# print(an[1:2])
xx = [[-1 for i in range(n+1)] for j in range(n+1)]

def x(i,j):
    global an;
    global xx;
    if xx[i][j] >= 0:
        return xx[i][j]
    elif i == 1:
        xx[i][j] = int(an[j-1:j])
        return xx[i][j]
    else:
        xx[i][j] = abs(x(i-1,j) - x(i-1,j+1))
        return xx[i][j]





# print(x(1,4))
print(x(n,1))

Submission status: Time Limit Exceeded

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

results = numbers
while len(results) != 1:
    box = []
    for i, res in enumerate(results):
        if i + 1 < len(results):
            box.append(abs(res - results[i + 1]))

    results = box

print(results[0])

Submission status: Time Limit Exceeded

Solution code:
vec=[]
M=0
N=int(input())
M=N
vec=list(map(int,input()))
for j in range(N-1):
  for i in range(M-1):
    vec[i]=abs(vec[i]-vec[i+1])
  M-=1
print(vec[0])

Submission status: Time Limit Exceeded

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

for j in range(n-1):
    for i in range(n-1-j):
        c = abs(a[i+1] - a[i])
        b.append(c)
    a = b
    b = []

print(a[0])

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
x=[int(i) for i in list(input().strip())]
for i in range(1,n):
    w=[]
    for j in range(len(x)-1):
        w.append(abs(x[j]-x[j+1]))
#    print(w)
    x=w

print(x[0])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = input()
b = list(a)
c = [int(s) for s in b]
k = 0
for j in range(N):
  for i in range(N-k-1):
    for x in range(3):
      if c[i] - c[i+1] == x or c[i+1] - c[i] == x:
        c[i] = x
        break
  k = k+1
print (c[0])

Submission status: Time Limit Exceeded

Solution code:
def pairwiseDifference(arr, n):
    z=[]
    for i in range(n - 1) :
        diff = abs(int(arr[i]) - int(arr[i + 1]))
        z.append(diff)
    return z
if __name__=="__main__":
    n = int(input())
    arr = input()
    ln=arr
    for i in range(n-1):
        pop = pairwiseDifference(ln, len(ln))
        ln = pop
    print(*pop)