Palindrome

Time limit: 0.4s Memory limit: 64MB Input: palindrome.in Output: palindrome.out

Task

Little Square found an array aa consisting of NN positive numbers formed of nonzero digits. He can replace any number from a with another number consisting of as many digits, all nonzero.

After making all the changes he wants, Little Square will form a new number MM by writing the elements of the array aa in order, without spaces. What is the minimum numbers of elements of the array aa which need to be replaced by Little Square for the number MM obtained to be a palindrome?

Input data

The first line of the input file palindrome.in will contain an integer NN, the size of the array aa. The second line will contain NN integers consisting only of nonzero digits representing the array aa.

Attention! The numbers read from the input file may be too big for the int data type. It is recommended to use the long long data type.

Output data

The output file palindrome.out will contain only one integer, representing the minimum number of elements of the array aa that need to be replaced.

Constraints

  • 1N500 0001 \leq N \leq 500 \ 000
  • 1ai<10181 \leq a_i \lt 10^{18}, for every 1iN1 \leq i \leq N
  • A number is a palindrome if its first digit is equal to its last one, its second digit is equal is equal to the next to last digit and so on. Thus, numbers 4,121,14 5414, 121, 14 \ 541 are palindromes, while numbers 21,433,1 234 31221, 433, 1 \ 234 \ 312 are not palindromes.
# Points Constraints
1 10 1ai91 \leq a_i \leq 9, for every 1iN1 \leq i \leq N
2 10 1N161 \leq N \leq 16
3 15 1N321 \leq N \leq 32
4 20 1N1 0001 \leq N \leq 1 \ 000
5 10 MM can become a palindrome by replacing at most 22 numbers.
6 35 No additional constraints.

Example 1

palindrome.in

6
13577 98 348 997 8 31

palindrome.out

2

Explanation

In the first example, Little Square can replace the first number with 13 87913 \ 879 and the third number with 448448. The number MM will become 1 387 998 448 997 8311 \ 387 \ 998 \ 448 \ 997 \ 831 which is a palindrome. There is no solution with less than two replacements.

Example 2

palindrome.in

3
455 21 1347742112664

palindrome.out

1

Explanation

In the second example, Little Square can replace the third number with 134 7743 112 554134 \ 7743 \ 112 \ 554.

Log in or sign up to be able to send submissions!