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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <unordered_set>

typedef long long ll;

using namespace std;

vector<unordered_set<int>> cc;

int findVertex(int v) {
    for(int i = 0; i < cc.size(); ++i) {
        if(cc[i].count(v) > 0) {
            return i;
        }
    }
    return -1;
}

int main() {
    ll n = 0, m = 0;
    cin >> n >> m;

    vector<int> vs;
    vector<int> ve;
    for(int i = 0; i < m; ++i) {
        int s = 0, e = 0;
        cin >> s >> e;
        vs.push_back(s);
        ve.push_back(e);
    }

    ll total = n * (n - 1) / 2;
    vector<ll> answers;
    
    for(int i = 1; i <= n; ++i) {
        unordered_set<int> iset;
        iset.insert(i);
        
        cc.push_back(iset);
    }
    
    answers.push_back(total);
    
    ll curTotal = 0;
    for(ll i = m - 1; i >= 0; --i) {
        int posSi = findVertex(vs[i]);
        int posEi = findVertex(ve[i]);
        
        // if current two vertexs are already in one set
        if(posSi == posEi) {
            answers.push_back(total - curTotal);
            continue;
        }
        
        ll cs = (cc[posSi].size() * (cc[posSi].size() - 1)) / 2;
        ll ce = (cc[posEi].size() * (cc[posEi].size() - 1)) / 2;
        
        curTotal -= cs + ce;
        
        // merge the two sets
        for(unordered_set<int>::iterator it = cc[posEi].begin(); it != cc[posEi].end(); ++it) {
            cc[posSi].insert(*it);
        }
        
        // remove the original two sets
        cc[posEi].clear();
        
        // re-calculate total
        curTotal += (cc[posSi].size() * (cc[posSi].size() - 1)) / 2;
        answers.push_back(total - curTotal);
    }
    
    for(int i = int(answers.size()) - 2; i >= 0; --i) {
        cout << answers[i] << endl;
    }
    
    return 0;
}