Problem:
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 (Python):
import collections
import sys

S1 = collections.deque()
S2 = collections.deque()
S3 = collections.deque()

for i, j in enumerate(sys.stdin.readline()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while True:
                if S2:
                    if S2[-1] > left_edge:
                        S2.pop()
                        new_puddle += S3.pop()
                    else:
                        break
                else:
                    break
            S2.append(left_edge)
            S3.append(new_puddle)
        else:
            pass


print(sum(S3))

print(len(S3), *S3)

Solution (Python):
import collections
import sys

S1 = collections.deque()
S2 = collections.deque()
S3 = collections.deque()

for i, j in enumerate(sys.stdin.readline()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while S2 and (S2[-1] > left_edge):
                if S2[-1] > left_edge:
                    S2.pop()
                    new_puddle += S3.pop()
            S2.append(left_edge)
            S3.append(new_puddle)
        else:
            pass


print(sum(S3))

print(len(S3), *S3)

Solution (Python):
import collections
import sys

S1 = collections.deque()
S2 = collections.deque()
S3 = collections.deque()

for i, j in enumerate(sys.stdin.readline()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while S2 and (S2[-1] > left_edge):
                if S2[-1] > left_edge:
                    S2.pop()
                    new_puddle += S3.pop()
            S2.append(left_edge)
            S3.append(new_puddle)
        else:
            pass


print(sum(S3))

print(len(S3), *S3)

Solution (Python):
import collections
import sys

S1 = collections.deque()
S2 = collections.deque()
S3 = collections.deque()

for i, j in enumerate(sys.stdin.readline()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while S2 and (S2[-1] > left_edge):
                if S2[-1] > left_edge:
                    S2.pop()
                    new_puddle += S3.pop()
            S2.append(left_edge)
            S3.append(new_puddle)
        else:
            pass


print(sum(S3))

print(len(S3), *S3)

Solution (Python):
import collections
import sys

S1 = collections.deque()
S2 = collections.deque()
S3 = collections.deque()

for i, j in enumerate(sys.stdin.readline()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while S2 and (S2[-1] > left_edge):
                if S2[-1] > left_edge:
                    S2.pop()
                    new_puddle += S3.pop()
            S2.append(left_edge)
            S3.append(new_puddle)
        else:
            pass


print(sum(S3))

print(len(S3), *S3)

Solution (Python):
import collections
import sys

S1 = collections.deque()
S2 = collections.deque()
S3 = collections.deque()

for i, j in enumerate(sys.stdin.readline()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while S2 and (S2[-1] > left_edge):
                if S2[-1] > left_edge:
                    S2.pop()
                    new_puddle += S3.pop()
            S2.append(left_edge)
            S3.append(new_puddle)
        else:
            pass


print(sum(S3))

print(len(S3), *S3)

Solution (Python):
import collections
import sys

S1 = collections.deque()
S2 = collections.deque()
S3 = collections.deque()

for i, j in enumerate(sys.stdin.readline()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while S2 and (S2[-1] > left_edge):
                S2.pop()
                new_puddle += S3.pop()
            S2.append(left_edge)
            S3.append(new_puddle)
        else:
            pass


print(sum(S3))

print(len(S3), *S3)

Solution (Python):
import collections
import sys

S1 = collections.deque()
S2 = collections.deque()

for i, j in enumerate(sys.stdin.readline()):
    if j == '\\':
        S1.append(i)
    elif j == '/':
        if S1:
            left_edge = S1.pop()
            new_puddle = i - left_edge
            while S2 and (S2[-1][0] > left_edge):
                new_puddle += S2[-1][1]
                S2.pop()
            S2.append((left_edge, new_puddle))
        else:
            pass


print(sum(j for i, j in S2))

print(len(S2), *(j for i, j in S2))

Solution (Python):



def calc_area(startIndex, endIndex, terrain):
    level = terrain[startIndex]
    area = 0
    for i in xrange(startIndex, endIndex):
        area += (level - terrain[i]) + 0.5 * (terrain[i] - terrain[i + 1])
    return int(area)


def check_area(terrain):
    i = 0
    startIndex = 0
    endIndex = 0
    while i < len(terrain) - 1:
        if terrain[i + 1] - terrain[i] < 0:
            startIndex = i
            break
        i += 1
    for j in xrange(i + 1, len(terrain)):
        if terrain[i] == terrain[j]:
            endIndex = j
            return startIndex, endIndex
    else:
        return 0, 0


def main():
    string = list(raw_input())
    terrain = []
    height = 0
    for c in string:
        terrain.append(height)
        if c == "\\":
            height -= 1
        elif c == "/":
            height += 1
    terrain.append(height)

    waterAreaRight = []
    while True:
        i, j = check_area(terrain)
        if i == j:
            break
        waterAreaRight.append(calc_area(i, j, terrain))
        terrain = terrain[j:]

    terrain = terrain[::-1]
    waterAreaLeft = []
    while True:
        i, j = check_area(terrain)
        if i == j:
            break
        waterAreaLeft.append(calc_area(i, j, terrain))
        terrain = terrain[j:]
    waterAreaRight.extend(waterAreaLeft[::-1])

    print sum(waterAreaRight)
    print len(waterAreaRight),
    if len(waterAreaRight) > 0:
        print " ".join(map(str, waterAreaRight))

main()

Solution (Python):

from collections import deque
input_line = input().strip()
stack1, stack2 = deque(), deque()
for index, i in enumerate(input_line):
    if i == "\\":
        stack1.append(index)
    elif i == "/" and len(stack1) != 0:
        a = stack1.pop()
        
        if len(stack2) != 0 and abs(stack2[-1][0] - a) == 1:
            stack2.append([a, index - a + stack2.pop()[1]])
        else:
            stack2.append([a, index - a])
        
        while len(stack2) >= 2 and stack2[-1][0] < stack2[-2][0]:
            stack2.append([stack2[-1][0], stack2.pop()[1] + stack2.pop()[1]])
ans = [i[1] for i in stack2]
print(sum(ans))
ans.insert(0, len(ans))
print(*ans)

Solution (Python):
def marge_ponds(lx, area_of_pond):
    global ponds
    if ponds:
        lp = ponds.pop()
        if lp[0] > lx:
            return marge_ponds(lx, area_of_pond + lp[1])
        else:
            ponds.append(lp)
    return area_of_pond


terrains = input().strip()
x, last_x, ponds = 0, [], []

for terrain in terrains:
    if terrain == '\\':
        last_x.append(x)
    elif terrain == '/':
        if last_x:
            lx = last_x.pop()
            area_of_pond = marge_ponds(lx, x - lx)
            ponds.append((lx, area_of_pond))
    x += 1

print(sum(pond[1] for pond in ponds))
ponds.insert(0, (0, len(ponds)))
print(' '.join(map(str, [pond[1] for pond in ponds])))

Solution (Python):
from collections import deque


def marge_ponds(lx, area_of_pond):
    global ponds
    if ponds and ponds[-1][0] > lx:
        return marge_ponds(lx, area_of_pond + ponds.pop()[1])
    return area_of_pond


terrains = input().strip()
x, last_x, ponds = 0, deque(), deque()

for terrain in terrains:
    if terrain == '\\':
        last_x.append(x)
    elif terrain == '/':
        if last_x:
            lx = last_x.pop()
            ponds.append((lx, marge_ponds(lx, x - lx)))
    x += 1

print(sum(pond[1] for pond in ponds))
ponds.appendleft((0, len(ponds)))
print(' '.join(map(str, [pond[1] for pond in ponds])))

Solution (Python):
from collections import deque

def main():
    S1 = deque([])
    S2 = deque([])
    Sum = 0
    S = raw_input()
    for i,j in enumerate(S):
        if j == '\\': S1.append(i)
        elif j == '/' and len(S1) > 0 :
            k = S1.pop()
            Sum += i - k
            a = i - k
            while len(S2)>0 and S2[len(S2)-1][0] > k:
                a += S2.pop()[1]
            S2.append([k,a])

    ans = []
    while len(S2)>0:
        ans.append(S2.pop()[1])
    ans = ans[::-1]
    print sum(ans)
    print len(ans),
    for i in xrange(len(ans)):
        print ans[i],
    print
    return 0

main()

Solution (Python):
line = input()
stack, edges, pools = [], [], []

for i in range(len(line)):
    if line[i] == '\\':
        stack.append(i)
    elif line[i] == '/' and stack:
        j = stack.pop()
        tmp = i - j
        while edges and edges[-1] > j:
            edges.pop()
            tmp += pools.pop()
        edges.append(j)
        pools.append(tmp)

print(sum(pools))
print(len(pools), *pools)

Solution (Python):
def calc(list):
    
    d = 0
    a = 0.0
    for c in list:
        if c == "\\":
            d += 1
            a += d - 1 + 0.5
        elif c == "_":
            a += d
        else:
            a += d - 1 + 0.5
            d -= 1
    return a

if __name__ == "__main__":
    line = raw_input()
    areas = []
    mode = False
    d = 0
    edge = 0
    for i in range(len(line)):
        
        c = line[i]
        if mode:
            if c == "\\":
                d += 1
            elif c == "/":
                d -= 1
                if d == 0:
                    areas.append(calc(line[edge:i + 1]))
                    mode = False
        else:
            if c == "\\":
                d += 1
                edge = i
                mode = True
            elif c == "_":
                edge = i
            else:
                edge = i

    last = len(areas)
    high = edge
    if d != 0:
        d = 0
        edge = len(line) - 1
        mode = False
        for i in range(len(line) - 1 , high - 1, -1):
            
            c = line[i]
            if mode:
                if c == "/":
                    d += 1
                elif c == "\\":
                    d -= 1
                    if d == 0:
                        
                        areas.insert(last, calc(line[i:edge + 1]))
                        mode = False
            else:
                if c == "/":
                    d += 1
                    edge = i
                    mode = True
                elif c == "_":
                    edge = i
                else:
                    edge = i

    if len(areas) == 0:
        print 0
        print 0
    else:
        areas = map(int, areas)
        print sum(areas)
        print len(areas),
        for i in range(len(areas) - 1):
            print areas[i],
        print areas[len(areas) - 1]

Solution (Python):
def main():
    a = 0
    i = 0
    s = []
    u = []
    l = raw_input()
    for c in l:
        if c == '\\':
            s.append(i)
        elif c == '/' and len(s) > 0:
            j = s.pop()
            t = i - j
            a += t
            while len(u) > 0 and u[-1][0] > j:
                t += u.pop()[1]
            u.append((j, t))
        i += 1
    u.insert(0, (0, len(u)))

    print(a)
    print(' '.join(map(lambda x: str(x[1]), u)))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    a = 0
    i = 0
    s = []
    u = []
    l = raw_input()
    for c in l:
        if c == '\\':
            s.append(i)
        elif c == '/' and len(s) > 0:
            j = s.pop()
            t = i - j
            a += t
            while len(u) > 0 and u[-1][0] > j:
                t += u.pop()[1]
            u.append((j, t))
        i += 1
    u.insert(0, (0, len(u)))

    print(a)
    print(' '.join(map(lambda x: str(x[1]), u)))


if __name__ == '__main__':
    main()

Solution (Python):


l = input()
S1, S2 = [], []

sum = 0
n = len(l)
for i in range(n):
	if l[i] == "\\":
		S1.append(i)
	elif l[i] == "/" and S1:
		j = S1.pop()
		a = i - j
		sum += a

		while S2 and S2[-1][0] > j:
			a += S2.pop()[1]
		S2.append([j, a])

print(sum)
print(len(S2), *(a for j, a in S2))

Solution (Python):


if __name__ == '__main__':

	l = input()
	S1, S2 = [], []
	
	sum = 0
	n = len(l)
	for i in range(n):
		if l[i] == "\\":
			S1.append(i)
		elif l[i] == "/" and S1:
			j = S1.pop()
			a = i - j
			sum += a
	
			while S2 and S2[-1][0] > j:
				a += S2.pop()[1]
			S2.append([j, a])
	
	print(sum)
	print(len(S2), *(a for j, a in S2))

Solution (Python):


if __name__ == '__main__':

	l = input()
	S1, S2 = [], []
	
	sum = 0
	n = len(l)
	for i in range(n):
		if l[i] == "\\":
			S1.append(i)
		elif l[i] == "/" and S1:
			j = S1.pop()
			a = i - j
			sum += a
	
			while S2 and S2[-1][0] > j:
				a += S2.pop()[1]
			S2.append([j, a])
	
	print(sum)
	print(len(S2), *(a for j, a in S2))

Solution (Python):


line = map(str, raw_input())
place = 0
area = 0
stack1 = []
stack2 = []
for i in line:
    if i == '\\':
        stack1.append(place)
    elif i == '/':
        if len(stack1) != 0:
            j = stack1.pop()
            s = place-j
            area += s
            while len(stack2) != 0:
                if stack2[-1][0] <= j:
                    break
                tmp = stack2.pop()
                s += tmp[1]
            stack2.append([j, s])
    place += 1
print area
temp = []
for a in stack2:
    temp.append(a[1])
if len(stack2) > 0:
    print len(stack2),
    print " ".join(map(str, temp))
else:
    print len(stack2)

Solution (Python):
from collections import deque

s = input()

d = deque()
l = deque()


def calc_area(j, i):
    return i - j


for i, c in enumerate(s):
    if c == "\\":
        d.append(i)
    if c == "/":
        if len(d) == 0:
            continue

        j = d.pop()

        s = calc_area(j, i)
        while len(l) > 0:
            _j, _i, _s = l.pop()
            if j < _j:
                s += _s
            else:
                l.append((_j, _i, _s))
                break

        l.append((j, i, s))

ans = str(len(l))
sum_area = 0
for j, i, s in l:
    ans += " " + str(s)
    sum_area += s

print(sum_area)
print(ans)

Solution (Python):
from collections import deque

if __name__ == '__main__':
    s = input()
    d = deque()
    l = deque()

    for i, c in enumerate(s):
        if c == "\\":
            d.append(i)
        if c == "/":
            if len(d) == 0:
                continue

            j = d.pop()

            s = i - j
            while len(l) > 0:
                _j, _i, _s = l.pop()
                if j < _j:
                    s += _s
                else:
                    l.append((_j, _i, _s))
                    break

            l.append((j, i, s))

    
    ans = str(len(l))
    sum_area = 0
    for j, i, s in l:
        ans += " " + str(s)
        sum_area += s

    
    print(sum_area)
    print(ans)

Solution (Python):


if __name__ == '__main__':

	l = input()
	S1, S2 = [], []
	
	sum = 0
	n = len(l)
	for i in range(n):
		if l[i] == "\\":
			S1.append(i)
		elif l[i] == "/" and S1:
			j = S1.pop()
			a = i - j
			sum += a
	
			while S2 and S2[-1][0] > j:
				a += S2.pop()[1]
			S2.append([j, a])
	
	print(sum)
	print(len(S2), *(a for j, a in S2))

Solution (Python):
from collections import deque

def scan(que, ps, way):
    Li = 0
    pp = ps
    while len(que) > 0:
        if way==0:
            p = que.popleft()
        else:
            p = que.pop()
        if p < ps:
            Li += ps - p
        else:
            break
        pp = p
    return Li

if __name__=='__main__':
    D = input()
    que = deque()
    p = 0
    for c in D:
        que.append(p)
        if   c == "\\" : p += -1
        elif c == "/"  : p +=  1
    que.append(p)
    
    Ll = [] 
    Lr = [] 
    ls = que.popleft()
    if len(que) > 0: rs = que.pop()
    while len(que) > 0:
        while len(que) > 0:
            if que[0] < ls:
                break
            else:
                ls = que.popleft()
        while len(que) > 0:
            if que[-1] < rs:
                break
            else:
                rs = que.pop()
        if len(que) > 0:
            if ls < rs: Ll.append(scan(que,ls,0))
            else      : Lr.insert(0,scan(que,rs,1))
    L = Ll + Lr
    if len(L) > 0:
        print(sum(L))
        print(len(L), *L)
    else:
        print(0)
        print(0)

Solution (Python):
DOWN = []
EDGE = []
POOL = []

for i, c in enumerate(input()):
    if c == '\\':
        DOWN.append(i)
    elif c == '/':
        if DOWN:
            e = DOWN.pop()
            Li = i - e 
            while EDGE: 
                if EDGE[-1] > e:
                    EDGE.pop()
                    Li += POOL.pop()
                else:
                    break
            EDGE.append(e)
            POOL.append(Li)

print(sum(POOL))
print(len(POOL), *POOL)

Solution (Python):

section = raw_input()


height = 0
heights = [0]
for p in xrange( len(section) ):
    if   section[p] == '/':
        height += 1
    elif section[p] == '\\':
        height -= 1
    heights.append(height)


pools = []
water = []
p = 0
while p < len(section):
    if section[p] == '\\':
        try:
            pr = heights[p+1:].index( heights[p] )
            pools.append([p, p+1+pr])
            p = p+1+pr
        except:
            p+=1
    else:
        p+=1


for pool in pools:
    tp = section[pool[0]:pool[1]]
    depth = 0
    s = 0
    for p in xrange( len(tp) ):
        if   tp[p] == '/':
            depth -= 1
        elif tp[p] == '\\':
            depth += 1
            s += 2*(depth-1)+1
        else:
            s += depth
    water.append(s)


print sum(water)
if len(water) == 0:
    print len(water)
else:
    print len(water), " ".join(map(str, water))

Solution (Python):
S = raw_input()
S1, S2 = [], []
 
ans = pool = 0
for i in xrange(len(S)):
    if S[i] == "/" and len(S1) > 0:
        j = S1.pop()
        ans += i - j
        a = i - j
        while (len(S2) > 0 and S2[-1][0] > j):
            a += S2.pop()[1]
        S2.append([j, a])
    if S[i] == "\\":
        S1.append(i)
print ans
if len(S2) > 0:
    print len(S2), " ".join(map(str, [a for j, a in S2]))
else:
    print 0

Solution (Python):

sample_input = list(range(3))




sample_input[0] = '''\\\\//'''
sample_input[1] = '''\\\\///\\_/\\/\\\\\\\\/_/\\\\///__\\\\\\_\\\\/_\\/_/\\'''
sample_input[2] = ''''''

give_sample_input = None
if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        




terrain_data = []

str_input = input()
list_input = list(str_input)

height = 0
left_max_height = 0
terrain_data.append([height,left_max_height, None])
for path in list_input:
    if path == '/':
        height += 1
    elif path == '\\':
        height -= 1
    left_max_height = max(left_max_height, height)
    terrain_data.append([height,left_max_height, None])

right_max_height = None 
for index in range(len(terrain_data)-1, -1, -1):
    height = terrain_data[index][0]
    if right_max_height is None:
        right_max_height = height
    else:
        right_max_height = max(right_max_height, height)
    terrain_data[index][2] = right_max_height

pool_left = -1
pool_right = -1
area = 0
areas = []
prev_depth = 0
for data in terrain_data:
    depth = min(data[1], data[2]) - data[0]
    area += (prev_depth + depth)/2
    if depth == 0:
        if pool_left < pool_right:
            areas.append(int(area))
            area = 0
        pool_right += 1
        pool_left = pool_right
    else:
        pool_right += 1
    prev_depth = depth

print(sum(areas))
output = str(len(areas)) + ' '
for a in areas:
    output += str(a) + ' '
print(output[:-1])

Solution (Python):
from collections import deque
area = input()
S1 = deque()
S2 = deque()
total = 0

for i, v in enumerate(area):
    if v == '\\':
        S1.append(i)
    elif v == '/' and S1:
        j = S1.pop()
        subtotal = i - j
        total += i - j
        while S2 and S2[-1][0] > j:
            subtotal += S2.pop()[1]
        S2.append([j, subtotal])

print(total)
print(len(S2), *(subtotal for j, subtotal in S2))

Solution (Python):
class Stack:
    def __init__(self):
        self.stack = []

    def push(self, value):
        self.stack.append(value)

    def pop(self):
        return self.stack.pop(-1)

    @property
    def isEmpty(self):
        if len(self.stack) == 0:
            return True
        else:
            return False

def main():
    s = input()
    stack_grad = Stack()
    stack_vol = Stack()
    sum_vol = 0

    for i, c in enumerate(s):
        if c == '\\':
            stack_grad.push(i)
        elif c == '/':
            if not stack_grad.isEmpty:
                previ = stack_grad.pop()
                vol = i - previ
                sum_vol += vol
                while not stack_vol.isEmpty:
                    tpl = stack_vol.pop()
                    if previ < tpl[0]:
                        vol += tpl[1]
                    else:
                        stack_vol.push(tpl)
                        break
                stack_vol.push((previ, vol))

    print(sum_vol)
    stack_vol.stack.reverse()
    out = str(len(stack_vol.stack))
    while not stack_vol.isEmpty:
        out += ' {}'.format(stack_vol.pop()[1])
    print(out)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

s1 = []
s2 = []
area = []

for i, c in enumerate(sys.stdin.readline()[:-1]):
    if c == '\\':
        s1.append(i)
    elif c == '/' and len(s1) > 0:
        j = s1.pop()
        a = i - j
        while s2:
            if s2[-1] > j:
                s2.pop()
                a += area.pop()
            else:
                break
        s2.append(j)
        area.append(a)

print(sum(area))
print(len(area), *area)

Solution (Python):


import sys

I = list(input())

A = 0
L = []
LH = []
k = 0
while len(I) > 0:
    i = I.pop()

    if i == '/':
        d = 1
        a = 0.5
        tmp_I = ['/']

        

        while len(I) > 0:
            i = I.pop()

            tmp_I.append(i)

            if i == '/':
                d += 1
                a += 0.5 + d - 1
            elif i == '\\':
                a += 0.5 + d - 1
                d -= 1
                if d == 0:
                    A += a
                    L.append(a)
                    k += 1
                    tmp_I = []
                    break
            elif i == '_':
                a += d

            

        

        if len(tmp_I) > 0:
            d = a = 0
            while len(tmp_I) > 0:
                i = tmp_I.pop()
                
                if i == '\\':
                    d += 1
                    a += 0.5 + d - 1
                elif i == '_':
                    a += d
                elif i == '/' and d > 0:
                    a += 0.5 + d - 1
                    d -= 1

                    if d == 0:
                        
                        A += a
                        LH.append(a)
                        k += 1
                        a = 0

print(int(A))
sys.stdout.write(str(k))
for l in LH:
    sys.stdout.write(" {}".format(int(l)))
while len(L) > 0:
    sys.stdout.write(" {}".format(int(L.pop())))
sys.stdout.write('\n')

Solution (Python):
import sys
from collections import deque

data = list(sys.stdin.readline()[:-1])

ponds = deque()
result = deque()
index = 0
amount = 0
lefs = deque()

for i in data:
    if i == "\\" or i == "_":
        ponds.append([i, index])
    else:
        count = 0
        while 1 <= len(ponds):
            pond, ind = ponds.pop()
            if pond == "\\":
                count += (index - ind)
                amount += count
                pre_pond = 0
                pre_left = 0
                while 0 < len(lefs) and ind < lefs[-1]:
                    pre_left = lefs.pop()
                    pre_pond += result.pop()
                lefs.append(ind)
                result.append(count + pre_pond)
                break
    index += 1

print(amount)
if 0 == len(result):
    print(0)
else:
    print(len(result), ' '.join(map(str, result)))

Solution (Python):
s = input()
s1, s2 = [], []

ans = 0
for i in range(len(s)):
    if s[i] == '\\':
        s1.append(i)
    if s[i] == '/' and s1:
        j = s1.pop()
        a = i - j
        ans += a
        while s2 and s2[-1][0] > j:
            a += s2.pop()[1]
        s2.append([j, a])
print(ans)
print(len(s2), *(a for j, a in s2))

Solution (Python):

def search_down_index(index, depthes, L):
    "index?????????????§??????????????????¨?????????????????????"
    for i in range(index, L - 1):
        if depthes[i] > depthes[i + 1]:
            return i
    return None

def search_down_index_reverse(index, depthes, L):
    "index?????????????§??????????????????¨?????????????????????"
    for i in range(index, -1, -1):
        if depthes[i] > depthes[i - 1]:
            return i
    return None

def search_up_index(index, depthes, L):
    "index?????????????§??????????????????¨?????????????????????"
    for i in range(index, L - 1):
        if depthes[i] < depthes[i + 1]:
            return i
    return None

def search_up_index(index, depthes, L):
    "index?????????????§??????????????????¨?????????????????????"
    for i in range(index, -1, -1):
        if depthes[i] < depthes[i + 1]:
            return i
    return None

def search_index_by_depth(index, depth, depthes, L):
    for i in range(index, L):
        if depthes[i] == depth:
            return i
    return None

def search_index_by_depth_reverse(index, depth, depthes, L):
    for i in range(index, -1, -1):
        if depthes[i] == depth:
            return i
    return None

def calc_area(begin_index, end_index, depthes):
    init_depth = depthes[begin_index]
    area = 0
    for i in range(begin_index, end_index):
        d1 = depthes[i]
        d2 = depthes[i + 1]
        if d1 > d2:
            area += (init_depth - d2 - 0.5)
        elif d1 == d2:
            area += init_depth - d1
        else:
            area += (init_depth - d1 - 0.5)
    return area

depthes = []
depth = 0
depthes.append(0)
for c in input():
    if c == "/":
        depth += 1
    elif c == "?\" or c == "\\":
        depth += -1
    depthes.append(depth)

L = len(depthes)
pond_ranges = []
stack = []
left_begin_index = search_down_index(0, depthes, L)
right_begin_index = search_down_index_reverse(L - 1, depthes, L)
while left_begin_index != None and right_begin_index != None and left_begin_index < right_begin_index:
    dl = depthes[left_begin_index]
    dr = depthes[right_begin_index]
    if dl < dr:
        left_end_index = search_index_by_depth(left_begin_index + 1, dl, depthes, L)
        pond_ranges.append((left_begin_index, left_end_index))
        left_begin_index = search_down_index(left_end_index, depthes, L)
    else:
        right_end_index = search_index_by_depth_reverse(right_begin_index - 1, dr, depthes, L)
        pond_ranges.append((right_end_index, right_begin_index))
        right_begin_index = search_down_index_reverse(right_end_index, depthes, L)

pond_ranges.sort()

k = len(pond_ranges)
areas = []
for r in pond_ranges:
    areas.append(int(calc_area(r[0], r[1], depthes)))
print(sum(areas))
if len(pond_ranges) == 0:
    print(0)
else:
    print(str(k), " ".join(map(str, areas)))

Solution (Python):
def cal_puddle(puddle):
    depth = 0
    score = 0
    for s in puddle:
        if s == "\\":
            score += depth + 0.5
            depth += 1
        elif s == "/":
            depth -= 1
            score += depth + 0.5
        elif s == "_":
            score += depth

        if depth == 0:
            return int(score)
    return int(score)

def get_puddles(diagram):
    hight = 0
    top_list = []
    in_puddle = True
    for index, s in enumerate(diagram + "\\"):
        if s == "/":
            in_puddle = True
            hight += 1
        if s == "\\":
            if in_puddle:
                in_puddle = False
                top_list.append([hight, index])
            hight -= 1

    puddles = []
    prev_hight = 0
    hight_list = [h for h, i in top_list]
    i = 0
    while i < len(top_list) - 1:
        cur_top = top_list[i]
        next_tops = list(filter(lambda top:cur_top[0] <= top[0], top_list[i + 1:]))
        
        if next_tops:
            next_top = next_tops[0]
            puddles.append((cur_top[1], next_top[1]))
            i = top_list.index(next_top)
        else:
            cur_top[0] -= 1
            cur_top[1] += diagram[cur_top[1] + 1:].index("\\") + 1
    
    
    
    return puddles

def main():
    diagram = input()
    result = []
    for s,e in get_puddles(diagram):
        result.append(cal_puddle(diagram[s:e]))
    print(sum(result))
    print(str(len(result)) + "".join([" " + str(i) for i in result]))
if __name__ == "__main__":
    main()

Solution (Python):
def analyze_map(data):
    S1 = []  
    S2 = []  

    area = 0
    for i, p in enumerate(data):
        if p == '\\':
            S1.append(i)  
        elif p == '/':
            if S1:
                pos = S1.pop()
                area = (i - pos)
                while S2 and S2[-1][0] > pos:
                    sub_pos, sub_area = S2.pop()
                    area += sub_area
                S2.append((pos, area))
    return S2


if __name__ == '__main__':
    
    
    
    data = input()

    
    results = analyze_map(data)

    
    
    total_area = sum([area for pos, area in results])
    if total_area:
        print(total_area)
        valid_areas = [area for start, area in results if area > 0]
        print('{0} {1}'.format(len(valid_areas), ' '.join(map(str, valid_areas))))
    else:
        print('0\n0')