Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Areas on the Cross-Section Diagram

Your task is to simulate a flood damage.

For a given cross-section diagram, reports areas of flooded sections.

Assume that rain is falling endlessly in the region and the water overflowing from the region is falling in the sea at the both sides.
For example, for the above cross-section diagram, the rain will create floods which have areas of 4, 2, 1, 19 and 9 respectively.

Input

A string, which represents slopes and flatlands by '/', '\' and '_' respectively, is given in a line. For example, the region of the above example is given by a string "\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\".

output

Report the areas of floods in the following format:

$A$

$k$ $L_1$ $L_2$ ... $L_k$

In the first line, print the total area $A$ of created floods.

In the second line, print the number of floods $k$ and areas $L_i (i = 1, 2, ..., k)$ for each flood from the left side of the cross-section diagram. Print a space character before $L_i$.

Constraints

$1 \leq$ length of the string $\leq 20,000$

Sample Input 1

\\//

Sample Output 1

4
1 4

Sample Input 2

\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\

Sample Output 2

35
5 4 2 1 19 9

Solution in JAVA (judge verdict: Memory Limit Exceeded)

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public int lineNumber;

    public ArrayList<Integer> stack;
    public ArrayList<Integer> stackStart;
    public ArrayList<Integer> puddles;
    public ArrayList<Integer> puddleStart;
    public int puddleNumber = 0;
    public int currentDepth = 0;
    public int prevPuddleHeight = -1;

    public int sum = 0;

    public void add(int idx, int n, boolean isConfirm) {
        if (this.stack.size() - 1 < idx) {
            this.stack.add(idx, 0);
            this.stackStart.add(idx, lineNumber);
        }
        this.stack.set(idx, (this.stack.get(idx) + n));
        if (isConfirm) {
            this.sum += this.stack.get(idx);
            this.puddles.add(puddleNumber, this.stack.get(idx));
            this.puddleStart.add(puddleNumber, this.stackStart.get(idx));
            if ((prevPuddleHeight != -1) && ((prevPuddleHeight - 1) == idx)) {
                int start = this.puddleStart.get(puddleNumber);
                for (int i = 0; i <= puddleNumber; i++) {
                    if (start < this.puddleStart.get(i)) {
                        this.puddleStart.set(i, start);
                        for (int j = i + 1; j <= puddleNumber; j++) {
                            this.puddles.set(i, this.puddles.get(i) + this.puddles.get(j));
                        }
                        for (int j = puddleNumber; j >= i + 1; j--) {
                            this.puddles.remove(j);
                            this.puddleStart.remove(j);
                        }
                        puddleNumber = i + 1;
                        break;
                    }
                }
            } else {
                puddleNumber++;
            }
            prevPuddleHeight = idx;
            this.stack.remove(idx);
            this.stackStart.remove(idx);
        }
    }

    public void up() {
        if (currentDepth == 0) {
            return;
        }
        currentDepth--;
        this.add(currentDepth, 1, true);
        for (int i = 0; i < currentDepth; i++) {
            this.add(i, 2, false);
        }
    }

    public void hori() {
        for (int i = 0; i < currentDepth; i++) {
            this.add(i, 2, false);
        }
    }

    public void down() {
        for (int i = 0; i < currentDepth; i++) {
            this.add(i, 2, false);
        }
        this.add(currentDepth, 1, false);
        currentDepth++;
    }

    public void print() {
        System.out.println(sum / 2);
        System.out.print(this.puddles.size());
        int len = this.puddles.size();
        for (int i = 0; i < len; i++) {
            System.out.print(" " + (this.puddles.get(i) / 2));
        }
        System.out.println();
    }

    public void go() {
        this.stack = new ArrayList<Integer>();
        this.stackStart = new ArrayList<Integer>();
        this.puddles = new ArrayList<Integer>();
        this.puddleStart = new ArrayList<Integer>();
        Scanner sc = new Scanner(System.in);
        String line = sc.next();
        String[] strings = line.split("");
        for (int i = 0; i < strings.length; i++) {
            String s = strings[i];
            this.lineNumber = i;
            switch (s) {
            case "\\":
                this.down();
                break;
            case "_":
                this.hori();
                break;
            case "/":
                this.up();
                break;
            }
        }
        print();
    }

    public static void main(String[] args) {
        Main cui = new Main();
        cui.go();
    }
}