#P17325. [ICPC 2018 Nanjing R] Huge Discount

[ICPC 2018 Nanjing R] Huge Discount

Problem Description

John heard an urban legend from Dreamoon about convenience stores in Incredible Convenient Purchasing Country (ICPC). The original price of any good there is usually as high as 1010510^{10^5}. Of course, nobody is going to pay such a high price. Instead, one can remove any two consecutive distinct digits from the original price. One can perform such operation as many times as he wants. Needless to say, each removal must be valid.

For example, if the original price is 123123, one could pay 11 dollar by removing 2323 or pay 33 dollars by removing 1212. However, it's illegal to pay 22 dollars because 11 and 33 are not adjacent. However, if the original price is 111111, no removal can be performed as all digits are the same.

There may be leading zeroes on the price tag. Also, leading zeroes may occur after some of such removals. In these cases, the leading zeroes are not removed automatically. Therefore, if the price tag reads 00330033, one can get it for free by removing 0303 twice.

John found some of such convenience stores. In these particular stores, there are some interesting properties on the prices:

  1. Only digits 00, 11 and 22 are used.
  2. For every ii, if the first digit on the price tag of good ii is removed, it becomes the price tag of good i+1i+1.

For example, if the price tag of good 11 is 012012, the price tag of good 22 is 1212 and the price tag of good 33 is 22.

Please tell John how much it costs to buy all goods in one particular store.

Input Format

The first line contains an integer, nn (1≤n≤1051 \le n \le 10^5), the number of goods in the store.

The second line contains a string, ss (∣s∣=n|s| = n, si∈{0,1,2},∀i∈[1,n]s_i \in \lbrace 0,1,2 \rbrace, \forall i\in [1,n]), the price tag of good 11 in the store.

Output Format

An integer, the cost to buy all goods, without leading zeroes.

5
11012
3
3
111
123