Problem:
Score : 300 points

Problem Statement

There are N cubes stacked vertically on a desk.

You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is 0, and blue if that character is 1.

You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent, and remove them. Here, the cubes that were stacked on the removed cubes will fall down onto the object below them.

At most how many cubes can be removed?

Constraints

1 \leq N \leq 10^5

|S| = N

Each character in S is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of cubes that can be removed.

Sample Input 1

0011

Sample Output 1

4

All four cubes can be removed, by performing the operation as follows:

Remove the second and third cubes from the bottom. Then, the fourth cube drops onto the first cube.

Remove the first and second cubes from the bottom.

Sample Input 2

11011010001011

Sample Output 2

12

Sample Input 3

0

Sample Output 3

0

Solution in Haskell (GHC 7.10.3) (judge verdict: Accepted)

import           Data.List  (find)
import           Data.Maybe (fromJust)

main :: IO ()
main = print . solve . map fromChar =<< getLine

data Cube =
    Red
  | Blue
  deriving (Eq)

fromChar :: Char -> Cube
fromChar '0' = Red
fromChar '1' = Blue
fromChar _   = error "unexpected character"

solve :: [Cube] -> Int
solve cubes =
  let
    convergence' = fromJust $ convergence $ iterate annihilate'' cubes
  in
    length cubes - length convergence'

annihilate :: [Cube] -> [Cube]
annihilate []                = []
annihilate [c]               = [c]
annihilate (Red : Blue : cs) = annihilate cs
annihilate (Blue : Red : cs) = annihilate cs
annihilate (c : cs)          = c : annihilate cs

annihilate' :: [Cube] -> [Cube]
annihilate' cs = foldr f [] $ zip cs $ tail cs
  where
    f (Red, Blue) cs' = cs'
    f (Blue, Red) cs' = cs'
    f (c1, c2) cs'    = c2 : cs'

annihilate'' :: [Cube] -> [Cube]
annihilate'' cs = foldr f [] cs
  where
    f Red (Blue : cs') = cs'
    f Blue (Red : cs') = cs'
    f c cs'            = c : cs'

converge :: Eq a => [a] -> [a]
converge [] = []
converge [x] = [x]
converge (x : xs@(y: _))
  | x == y    = [x]
  | otherwise = x : converge xs

convergence :: Eq a => [a] -> Maybe a
convergence = fmap fst . find (uncurry (==)) . (zip <*> tail)