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 C (GCC 9.2.1) (judge verdict: Time Limit Exceeded)

//ex9_2
//TKC001

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 100000

typedef struct stack{
    char num;
    struct stack *next;
}stack;

void push(stack **stk, char num){
    stack *new = calloc(1, sizeof(stack));
    if(new == NULL) exit(1);

    new->num = num;
    new->next =*stk;
    *stk = new;
}

char pop(stack **stk){
    char num;
    stack *tmp = *stk;
    if(tmp == NULL) exit(1);
    num = tmp->num;
    *stk = tmp->next;

    free(tmp);
    return num;
}
//mainの中のwhileループを続けるか判断する関数
//スタックのnumが全部同じだったらもうやらない
int jadge(stack **stk){
    stack *tmp = *stk;
    char compare = tmp->num;

    while(1){
        if(tmp->num == 'F') return 0;//もうやらない  
        if(tmp->num != compare) return 1;//続ける
        tmp = tmp->next; 
    }
    free(tmp);
}

int main(void){
    int i, count = 0;
    stack *stk = NULL, *comp = NULL;
    char str[SIZE+1], last, prelast, fromcomp;
    //関数の引数にNULLがきたらダメっぽいので先頭にFirstのFを追加
    push(&stk, 'F');
    //入力
    scanf("%s", str);
    //スタックに文字列を追加
    for(i = 0; i < strlen(str); i++){
        push(&stk, str[i]);
    }

    if(strlen(str) == 1){
        count = 0;
    }
    else{
        while (1){
            last = pop(&stk);
            prelast = pop(&stk);
            //lastとprelastが同じならcompに移動、違ったら消し、countを増やす。
            if(last == prelast){
                push(&comp, last);
                push(&stk, prelast);
            }
            else{
                count++;
                //compの最後の要素はstkの最後の要素と隣り合うのでstkに移動
                if(comp != NULL){
                    fromcomp = pop(&comp);
                    push(&stk, fromcomp);
                }
            }

            if(jadge(&stk) == 0) break;
        }
    }
    //出力
    printf("%d", count*2);
  
    return 0;
}