atcoder#AGC043B. [AGC043B] 123 Triangle

[AGC043B] 123 Triangle

Score : 700700 points

Problem Statement

Given is a sequence of NN digits a1a2aNa_1a_2\ldots a_N, where each element is 11, 22, or 33. Let xi,jx_{i,j} defined as follows:

  • x1,j:=ajx_{1,j} := a_j \quad (1jN1 \leq j \leq N)
  • xi,j:=xi1,jxi1,j+1x_{i,j} := | x_{i-1,j} - x_{i-1,j+1} | \quad (2iN2 \leq i \leq N and 1jN+1i1 \leq j \leq N+1-i)

Find xN,1x_{N,1}.

Constraints

  • 2N1062 \leq N \leq 10^6
  • ai=1,2,3a_i = 1,2,3 (1iN)(1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

NN

a1a_1a2a_2\ldotsaNa_N

Output

Print xN,1x_{N,1}.

4
1231
1

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

x2,1,x2,2,x2,3x_{2,1},x_{2,2},x_{2,3} are respectively 12=1,23=1,31=2|1-2| = 1,|2-3| = 1,|3-1| = 2.

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

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

10
2311312312
0