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 Scala (judge verdict: WA: Presentation Error)

import scala.io.StdIn.readLine
class Stack[T](val stack: List[T]) {
    def push(e: T): Stack[T] = new Stack[T](e :: stack)
    def pop(): Stack[T] = stack match {
        case Nil => new Stack[T](Nil)
        case x :: xs => new Stack[T](xs)
    }
    def top(): Option[T] = stack match {
        case Nil => None
        case x :: xs => Option(x)
    }
    def isEmpty(): Boolean = top == None
}
object Stack {
    def apply[T]() = new Stack[T](Nil)
}
object Main {
    def main(args: Array[String]) {
        val input = readLine.split("").toList
        val ret = calcArea(input)
        println(ret.sum)
        println(ret.length + " " + ret.mkString(" "))
    }
    def calcArea(data: List[String]): List[Int] = {
        def findMaxIndex(list: List[(String, Int)], hight: Int, max: Int, index: Int): Int = list match {
            case Nil => index
            case x :: xs => x._1 match {
                case "_" => findMaxIndex(xs, hight, max, index)
                case "\\" => findMaxIndex(xs, hight - 1, max, index)
                case "/" => {
                    val h = hight + 1
                    if (h > max) findMaxIndex(xs, h, h, x._2)
                    else findMaxIndex(xs, h, max, index)
                }
            }
        }
        def splitMax(list: List[String], maxIndex: Int, first: List[String]): (List[String], List[String]) = list match {
            case x :: xs if maxIndex == 0 && x == "/"=> ((x :: first).reverse, xs)
            case x :: xs => splitMax(xs, maxIndex - 1, x :: first)
            case Nil => (first.reverse, Nil)
        }
        // ある高さにおける水たまり面積のリストを作成 
        def go(zip: List[(String, Int)], stack: Stack[Int], hight: Int, ret: List[(Int, Int)]): List[(Int, Int)] = zip match {
            case Nil => ret
            case (x @ (symbol, idx)) :: xs => symbol match {
                case "_" => go(xs, stack, hight, ret)
                case "\\" => go(xs, stack.push(idx), hight - 1, ret)
                case "/" => {
                    if (stack.isEmpty) {
                        go(xs, stack, hight + 1, ret)
                    } else {
                        val prevIdx = stack.top.get
                        go(xs, stack.pop(), hight + 1, (idx - stack.top.get, hight + 1) :: ret)
                    }
                }
            }
        }
        def sumArea(list: List[(Int, Int)], ret: List[(Int, Int)]): List[(Int, Int)] = list match {
            case Nil => ret
            case x :: xs if ret == Nil => sumArea(xs, x :: ret)
            case curr :: xs => ret match {
                case pre :: ys if curr._2 < pre._2 => sumArea(xs, (curr._1 + pre._1, pre._2) :: ys)
                case pre :: ys => sumArea(xs, curr :: ret)
            }
        }
        
        val maxIndex = findMaxIndex(data.zipWithIndex, 0, 0, 0)
        val (first, last) = splitMax(data, maxIndex, Nil)
        val firstAreaList = go(first.zipWithIndex, Stack[Int](), 0, Nil)
        val lastAreaList = go(last.zipWithIndex, Stack[Int](), 0, Nil)
        val firstSum = sumArea(firstAreaList, Nil)
        val lastSum = sumArea(lastAreaList, Nil)
        (firstSum ::: lastSum).map(_._1)
    }
}