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):
from queue import LifoQueue

MAP = input()

que = LifoQueue()
res = LifoQueue()

for i, m in enumerate(MAP):
    if m=='\\':
        que.put(i)
    elif m=='/':
        if not que.empty():
            j = que.get(False)
            v = i - j
            t = (j, v)
            while not res.empty():
                pre = res.get(False)
                if (pre[0] > j):
                    t = (t[0], t[1] + pre[1])
                else:
                    res.put(pre)
                    res.put(t)
                    break
            else:
                res.put(t)

summaly = 0
lakes = []
while not res.empty():
    v = res.get()
    lakes.append(v[1])
    summaly += v[1]

print(summaly)
print(len(lakes), *(reversed(lakes)))

Solution (Python):
import sys
import math
import copy
from typing import Tuple, List, Union
input = sys.stdin.readline

class Area:
    def __init__(self, startidx: int, area: int):
        self.startidx = startidx
        self.area = area
    def __repr__(self):
        return "{}".format(self.area)


def main():
    edgeStack = []
    areaStack = []
    line = input()
    for idx, item in enumerate(line):
        if item is "\\":
            edgeStack.append(idx)
        elif item is "_":
            pass
        elif item is "/":
            if len(edgeStack) < 1:
                continue
            prevarea = 0
            previdx = edgeStack[-1]
            while (len(areaStack) > 0
                   and len(edgeStack) > 0
                   and areaStack[-1].startidx > edgeStack[-1]):
                area = areaStack.pop(-1)
                prevarea += area.area
            areaStack.append(Area(edgeStack.pop(-1), prevarea + idx - previdx))
    return areaStack


if __name__ == '__main__':
    areaStack = main()
    print(sum([area.area for area in areaStack]))
    if len(areaStack):
        print(len(areaStack), end=" ")
    else:
        print(len(areaStack), end="")
    print(" ".join([str(area) for area in areaStack]))

Solution (Python):
strs = input()
h = 0
heights = [0]
max_h = 0
k = []
sum = 0
for i, s in enumerate(strs, 1):
    if s=='\\':
        h -= 1
        heights.append(h)
    elif s=='/':
        h += 1
        heights.append(h)
        if h>max_h:
          max_h = h
          continue
        t = -2
        while t+i+1>=0:
            if heights[t]==h:
                sum += (-t-3)+1
                tmp = (-t-3)+1
                if len(k)>0:
                  c = len(k)
                  for ts, te, tm in k[::-1]:
                    c -= 1
                    if t+i+1<ts and te<i:
                        k.pop(c)
                        tmp += tm
                k.append([t+i+1, i, tmp])
                break
            else:
                t -= 1
                continue
    else:
        heights.append(h)
        continue
print(sum)
ans = [str(k[-1]) for k in k]
print(len(ans), *ans)

Solution (Python):

from collections import deque

d_stack=deque()
res_stack=deque()
s = input()

for i in range(len(s)):
    
    if s[i]=="\\":
        d_stack.append(i)
    elif s[i]=="/":
        if len(d_stack)==0:
            continue
        left = d_stack.pop()
        area = i-left
        
        if len(res_stack)>0:
            flag=True
            
            mergeareasum=0
            while flag:
                if len(res_stack)>0 and left<res_stack[-1][0]:
                    mc = res_stack.pop()
                    mergeareasum += mc[1]
                    
                else:
                    flag = False
                    res_stack.append((left,area+mergeareasum))
        else:
                res_stack.append((left,area))

ans=0
v_devided=[]
for pair in res_stack:
    ans += pair[1]
    v_devided.append(str(pair[1]))
print(ans)
if len(v_devided)>0:
    print(len(v_devided)," ".join(v_devided))
else:
    print(0)

Solution (Python):
S = input()
st = [(0, 0, -1)]
cur = 0; ma = 0
for i, c in enumerate(S):
    if c == '/':
        cur += 1
        s = 0
        while st and st[-1][0] == cur-1 and cur <= ma:
            lev, su, j = st.pop()
            s += su
        if st and st[-1][0] == cur:
            s += i - st[-1][2] - 1
        ma = max(ma, cur)
        st.append((cur, s, i))
    elif c == '\\':
        cur -= 1
        st.append((cur, 0, i))
    elif c == '_':
        st.append((cur, 0, i))
ans = []
for lev, su, j in st:
    if su > 0:
        ans.append(su)
print(sum(ans))
print(len(ans), *ans)

Solution (Python):
class Pond:
    def __init__(self, down_x, area):
        super().__init__()
        self.down_x = down_x
        self.area = area


s = []
ponds = []
all_area = 0
x = 0
for c in input():
    if "\\" == c:
        s.append(x)
    elif "/" == c and s:
        down_x = s.pop()
        area = x - down_x
        all_area += area
        while ponds and ponds[-1].down_x > down_x:
            area += ponds[-1].area
            ponds.pop()
        ponds.append(Pond(down_x, area))
    x += 1
print(all_area)
if ponds:
    print(len(ponds), ' '.join(map(lambda pond: str(pond.area), ponds)))
else:
    print(0)

Solution (Python):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def main():
    t = input().strip()
    s = []
    
    ans = []
    prevA = 0
    for i in range(len(t)):
        if t[i] == '\\':
            s.append(i)
        elif len(s) > 0 and t[i] == '/':
            p = s.pop()
            k = i - p
            ans.append((p, k))
            q = 0
            while len(ans) > 0:
                
                g, h = ans[-1]
                if g >= p:
                    q += h
                    ans.pop()
                else:
                    break
            
            
            
            
            
                
            if q != 0:
                ans.append((p, q))
        
        

    v = 0
    for i in range(len(ans)):
        v += ans[i][1]
    print(v)
    print(len(ans), end='')
    for i in range(len(ans)):
        print(' {}'.format(ans[i][1]), end='')
    print('')

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

def area(map):
    total = 0
    s1 = deque()
    s2 = deque()
    for i in range(len(map)):
        if map[i] == "\\":
            s1.append(i)    
        elif len(s1) > 0 and map[i] == "/":
            old = s1.pop()  
            tmp = 0
            while len(s2) > 0 and old < s2[-1][0]:
                tmp += s2.pop()[1]  
            s2.append([old, tmp + i - old]) 

    
    for i in range(len(s2)):
        total += s2[i][1]
    print(total)

    if s2:
        print(len(s2), end=" ")
        for i in range(len(s2)):
            if i != len(s2)-1:
                print(s2[i][1], end = " ")
            else:
                print(s2[i][1])
    else:
        print(len(s2))


if "__main__" == __name__:
    map = input().rstrip()
    area(map)

Solution (Python):
def main():
    inputs = str(input())
    stack = []
    pond = [Pond(-1, 0, 0)]

    for i, string in enumerate(inputs):
        if string == "\\":
            stack.append(i)
        elif string == "/":
            try:
                down = stack.pop()
            except  IndexError as err:
                continue
            pond.append(Pond(down, i, i-down))
            while len(pond) > 1:
                newPond = pond.pop()
                prevPond = pond.pop()
                if prevPond.includedBy(newPond):
                    pond.append(prevPond.merge(newPond))
                else:
                    break
            pond.append(prevPond)
            pond.append(newPond)

    printPonds(pond)

def printPonds(pond):
    size = 0
    eachSize = []
    for p in pond:
        size += p.size
        eachSize.append(p.size)
    eachSize[0] = len(pond)-1
    print(size)
    print(*eachSize)

class Pond:
    def __init__(self, x1, x2, size):
        self.x1 = x1
        self.x2 = x2
        self.size = size

    def merge(self, newPond):
        self.x1 = newPond.x1
        self.x2 = newPond.x2
        self.size += newPond.size
        return self

    def includedBy(self, newPond):
        return self.x1 >= newPond.x1 and self.x2 <= newPond.x2

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
def calculate_area(lines):
    q = deque()
    partial_area = 0
    areas = []
    total_area = 0
    for i, line in enumerate(lines):
        if line == "\\":
            q.append(i)
        elif line == "/":
            if q:
                num = q.pop()
                partial_area = i - num
                total_area += partial_area
                for p_area in areas[::-1]:
                    if p_area[0] > num:
                        partial_area += areas.pop()[1]
                    else:
                        break
                areas.append([num, partial_area])
    areas_strigified = [str(v) for i, v in areas]
    print(total_area)
    if total_area:
        print("{} {}".format(len(areas), ' '.join(areas_strigified)))
    else:
        print(0)
                    

if __name__ == "__main__":
    data = input()
    calculate_area(data)

Solution (Python):
import math
import string
import itertools
import fractions
import heapq
import collections
import re
import array
import bisect
import sys
import random
import time
inf = 10**9


def main():
    s = input()

    stack = []
    flat_pools = []
    for (i, c) in enumerate(s):
        if c == '\\':
            stack.append(i)
        elif c == '/' and len(stack) > 0:
            flat_pools.append((stack.pop(), i))

    flat_pools.sort(key=lambda pool: pool[0])

    last_index = 0
    pools = []
    area = 0

    for pool in flat_pools:
        area += pool[1] - pool[0]
        if last_index <= pool[0]:
            pools.append(pool[1] - pool[0])
            last_index = pool[1]
        else:
            pools[-1] = pools[-1] + pool[1] - pool[0]

    pools.insert(0, len(pools))
    print(area)
    print(" ".join(map(str, pools)))


if __name__ == '__main__':
    main()

Solution (Python):

input_data = input()


stack_list = list()
area_list = list()
area_sum = 0


for i in range(len(input_data)):
    if input_data[i] == "\\":
        stack_list.append(i)
    elif len(stack_list) > 0 and input_data[i] == "/":
        temp_area = i - stack_list[-1]
        area_sum += temp_area
        if len(area_list) == 0 or area_list[-1][0] < stack_list[-1]:
            area_list.append([stack_list[-1], temp_area])
        else:
            area_list[-1][0] = stack_list[-1]
            area_list[-1][1] += temp_area
            temp_len = len(area_list)
            for j in range(temp_len-1, 0, -1):
                if area_list[j-1][0] > area_list[j][0]:
                    area_list[j-1][0] = area_list[j][0]
                    area_list[j-1][1] += area_list[j][1]
                    del area_list[j]
                else:
                    break
        del stack_list[-1]


print(area_sum)
print(len(area_list), end="")
for i in range(len(area_list)):
    print(f" {area_list[i][1]}", end="")
print("")

Solution (Python):
down = []
edge = []
pool = []
for i,c in enumerate(input()):
    if c == "\\":
        down.append(i)
    elif c == "/" and down:
        left = down.pop()
        area = i - left
        
        while edge:
            if edge[-1] > left:
                edge.pop()
                area += pool.pop()
            else:
                break
        
        edge.append(left)
        pool.append(area)
print(sum(pool))
print(" ".join(map(str,[len(pool)]+pool)))

Solution (Python):
from collections import deque
def calculate_area(lines):
    q = deque()
    partial_area = 0
    areas = []
    total_area = 0
    for i, line in enumerate(lines):
        if line == "\\":
            q.append(i)
        elif line == "/":
            if q:
                num = q.pop()
                partial_area = i - num
                total_area += partial_area
                for p_area in areas[::-1]:
                    if p_area[0] > num:
                        partial_area += areas.pop()[1]
                    else:
                        break
                areas.append([num, partial_area])
    areas_strigified = [str(v) for i, v in areas]
    print(total_area)
    if total_area:
        print("{} {}".format(len(areas), ' '.join(areas_strigified)))
    else:
        print(0)
                    

if __name__ == "__main__":
    data = input()
    calculate_area(data)

Solution (Python):
lst = list(input())
v_lakes = []
idx_stack = []
for i, c in enumerate(lst):
    if (c == '\\'):
        idx_stack.append(i)
    elif (idx_stack and c == '/'):
        
        j = idx_stack.pop()
        v = i - j
        while (v_lakes and v_lakes[-1][0] > j):
            v += v_lakes.pop()[1]
        v_lakes.append((j, v))
ans = [len(v_lakes)]
v = [v[1] for v in v_lakes]
ans.extend(v)

print(sum(v))
print(*ans)

Solution (Python):


class depth_Char:
    ch = ""
    depth = 0

    def __init__(self,char,depth):
        self.ch = char
        self.depth = depth

    def __str__(self):

        return f"{self.ch}:{self.depth}"


string = input()

depth = 0
d_list = []
for ch in string:
    if ch == "\\":
        depth -= 1
        d_list.append(depth_Char(ch,depth))

    elif ch == "_":
        d_list.append(depth_Char(ch,depth))

    elif ch == "/":
        d_list.append(depth_Char(ch,depth))
        depth += 1



lake_list = []  
bslash_dict = dict()
for i,d_ch in enumerate(d_list):

    if d_ch.ch == "\\":
        bslash_dict[d_ch.depth] = i
    elif d_ch.ch == "/" and d_ch.depth in bslash_dict:
        j = bslash_dict[d_ch.depth]

        is_need_append = True
        
        remove_list = []  

        for lake in lake_list:
            if is_need_append and lake[0] < j and i < lake[1]:
                is_need_append = False

            if j < lake[0] and lake[1] < i:
                
                remove_list.append(lake)
        
        for rem in remove_list:
            lake_list.remove(rem)

        if is_need_append:
            lake_list.append([j,i])
        

lake_area_list = []
for i,j in lake_list:
    lake = string[i:j+1]
    t_depth = 0
    area = 0
    for ch in lake:
        if ch == "\\":
            t_depth += 1
            area += t_depth
        elif ch == "_":
            area += t_depth
        elif ch == "/":
            t_depth -= 1
            area += t_depth
    
    lake_area_list.append(area)

print(sum(lake_area_list))
print(len(lake_area_list),*lake_area_list)

Solution (Python):
a = input().strip()
stack = []
area = 0
area_list = []

for i, slope in enumerate(a):
    if slope == '\\':
        stack.append(i)
    elif slope == '/' and len(stack) != 0:
        ip = stack.pop(len(stack)-1)
        area += (i - ip)
        a = i - ip
        while len(area_list) != 0 and area_list[len(area_list)-1][0] > ip:
            temp = area_list.pop(len(area_list)-1)
            a += temp[1]
        area_list.append([ip, a])
    
print(area)
print('{}'.format(len(area_list)), end='')
if len(area_list) != 0:
    print(' ', end='')
print(' '.join([str(i[1]) for i in area_list]))

Solution (Python):
s=[]
p=[]
a=i=0
for c in input():
     if"\\"==c:
         s+=[i]
     elif"/"==c and s:
         j=s.pop()
         t=i-j
         a+=t
         while p and p[-1][0]>j:
             t+=p[-1][1];
             p.pop()
         p+=[(j,t)]
     i+=1
print(a)
if p:
    print(len(p),*list(zip(*p))[1])
else:
    print(0)

Solution (Python):
downhill_stack = []
idx_area_stack = []
for idx, c in enumerate(input()):
    if c == '\\':
        downhill_stack.append(idx)
    elif c == '/' and len(downhill_stack) > 0:
        downhill_idx = downhill_stack.pop()
        current_level_area = idx - downhill_idx
        if len(idx_area_stack) == 0:
            idx_area_stack.append((downhill_idx, current_level_area))
            continue
        last_idx = idx_area_stack[-1][0]
        if downhill_idx < last_idx:
            prov_area = current_level_area
            while downhill_idx < last_idx:
                idx_area = idx_area_stack.pop()
                prov_area += idx_area[1]
                if len(idx_area_stack) == 0:
                    break
                last_idx = idx_area_stack[-1][0]
            prov_idx_area = (downhill_idx, prov_area)
            idx_area_stack.append(prov_idx_area)
        else:
            idx_area_stack.append((downhill_idx, current_level_area))
puddle_num = len(idx_area_stack)
areas = list(map(lambda x: x[1], idx_area_stack))
print(sum(areas))
if puddle_num > 0:
    print(puddle_num, " ".join(map(str, areas)))
else:
    print(puddle_num)

Solution (Python):
tochi=input()
s=[]
I=[]

sum=0
n=len(tochi)
for i in range(n):
    if tochi[i] == "\\":
        s.append(i)
    elif tochi[i] == "/" and s:
        j = s.pop()
        a = i - j
        sum += a
	
        while I and I[-1][0] > j:
            a += I.pop()[1]
        I.append([j, a])

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

Solution (Python):
N = input()
s1 = []
s2 = []
res = []
ans = []
cnt = 0
for i in range(len(N)):
    if N[i] == "\\":
        s1.append(i)
    elif N[i] == "/" and s1:
        idx = s1.pop()
        tmp = i - idx
        cnt += tmp

        while s2 and idx < s2[-1][0]:
            tmp += s2.pop()[1]
        s2.append((idx, tmp))

res.append(len(s2))
for i in range(len(s2)):
    ans.append(s2[i][1])
res.extend(ans)
print(cnt)
print(" ".join(map(str, res)))

Solution (Python):
import collections


def main():
    s = input()
    downs = collections.deque()
    pools = collections.deque()
    total_area = 0
    for now, char in enumerate(s):
        if char == "\\":
            downs.append(now)
        elif downs and char == "/":
            down = downs.pop()
            sub_area = now - down
            total_area += sub_area
            if not pools:
                pools.append((down, sub_area))
            else:
                while pools and down < pools[-1][0]:
                    sub_area += pools.pop()[1]
                pools.append((down, sub_area))
    print(total_area)
    pools = list(zip(*pools))[1] if pools else []
    print(len(pools), *pools)


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
highest, high, h, mh = [0], [0], 0, 0
for i in range(len(s)):
    if s[-i-1] == '\\':     h += 1
    elif s[-i-1] == "/":    h -= 1
    
    if mh < h:  mh = h
    
    high.append(h)
    highest.append(mh)

high, highest = high[::-1], highest[::-1]
puddle, h = [], high[0]
v, vsum = 0, 0
for i in range(1, len(s) + 1):
    h = min(h, highest[i])
    if high[i] < h:
        if s[i-1] != "_":
            v -= 0.5
        v += h - min(high[i-1], high[i])
    elif v != 0:
        v = int(v + 0.5)
        puddle.append(v)
        vsum += v
        v, h = 0, high[i]
    else:
        h = high[i]
        
print(vsum)
if len(puddle) != 0:
    print(len(puddle), ' '.join(list(map(str, puddle))))
else:
    print(0)

Solution (Python):
s=j=0
a=[];b=[];d=[]
for i,x in enumerate(input()):
    if x=='\\':a+=[i]
    elif x=='/' and a:
        j=a.pop()
        c=i-j;s+=c
        while b and b[-1]>j:c+=d.pop();b.pop()
        b+=[j];d+=[c]
print(s)
print(len(b),*(d))

Solution (Python):
s = 0   




a = []
b = []
d = []

for i, x in enumerate(input()):
    if x == '\\':
        a.append(i)
    elif x == '/' and a:
        j = a.pop()
        c = i - j
        s += c
        while b and b[-1] > j:
            b.pop()
            c += d.pop()
        b.append(j)
        d.append(c)
print(s)
print(len(b), *d)

Solution (Python):
w = input()

h = [0]

for i in range(len(w)):
  if w[i] == "/":
    h.append(h[i]+1)
  elif w[i] == "_":
    h.append(h[i])
  else:
    h.append(h[i]-1)

L = []

lstart = []

for i in range(len(w)):
  if w[i] == "\\":
    lstart.append(i)
    L.append(0)
  elif w[i] == "/" and not lstart == []:
    j = lstart.pop()
    s = i-j
    for k in range(1, i-j):
      if L[i-k]>0:
        s += L.pop(i-k)
        L.append(0)
    L.append(s)
  else:
    L.append(0)

L = [i for i in L if i > 0]

print(sum(L))
L.insert(0,len(L))
print(" ".join(map(str, L)))

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):
def main():
    cross_sections = input()
    s1 = []
    s2 = []
    total_surface = 0
    for i, cross_section in enumerate(cross_sections):
        if cross_section == '\\':
            s1.append(i)
        elif cross_section == '/' and s1:
            ip = s1.pop()
            
            sub_surface = i - ip
            total_surface += sub_surface
            
            for s in reversed(s2):
                if s[0] > ip:
                    sub_surface += s2.pop()[1]
                else:
                    break
            s2.append((ip, sub_surface))

    print(total_surface)
    if s2:
        print(len(s2), ' '.join(map(lambda x: str(x[1]), s2)))
    else:
        print(len(s2))


if __name__ == "__main__":
    main()

Solution (Python):
areaStr = list(input())
N = len(areaStr)
tmpArea = []
totalArea = 0
eachArea = []
"""
for i in range(N):
    if areaStr[i] == "\\":
        tmpArea.append(i)
    elif areaStr[i] == "/":
        if len(tmpArea) != 0:
            totalArea += (i - tmpArea.pop())
    else:
        pass
    
"""


tmpArea = []
totalArea = 0
for i in range(N):
    if areaStr[i] == "\\":
        tmpArea.append(i)
    elif areaStr[i] == "/":
        if len(tmpArea) != 0:
            j = tmpArea.pop()
            
            totalArea += (i - j)
            a = i - j
            while(len(eachArea) > 0 and eachArea[len(eachArea) - 1][0] > j):
                a +=  eachArea.pop()[1]
               
            eachArea.append([j, a])
    else:
        pass
    

print(totalArea)
print(len(eachArea), end = "")
if len(eachArea) > 1:
    for i in range(len(eachArea)-1):
        print("", end = " ")
        print(eachArea[i][1], end = "")
if len(eachArea) >= 1:
    print("", end = " ")
    print(eachArea[len(eachArea)-1][1])
    
else:
    print("")

Solution (Python):
S = input()
t = 0
H = [0]
for c in S:
    if c == '\\':
        t -= 1
        H.append(t)
    elif c == '_':
        H.append(t)
    elif c == '/':
        t += 1
        H.append(t)
L = [0 for _ in range(len(H))] 
for i in range(1, len(H)):
    L[i] = max(L[i-1], H[i])
R = [H[len(H)-1]]
for i in reversed(range(len(H)-1)):
    R.append(max(R[len(R)-1], H[i]))
R.reverse()
C = [min(L[i], R[i]) for i in range(len(H))]
D = 0
ans = []
for i in range(len(H)-1):
    D += (C[i] - H[i]) + (C[i+1] - H[i+1])
    if C[i+1] == H[i+1] and D != 0:
        ans.append(D // 2)
        D = 0
print(sum(ans))
ans.insert(0, len(ans))
print(' '.join(map(str, ans)))

Solution (Python):
s=input()



    

stack=[]
i=0
result=0

mizutamari=[[-1,0]]
for item in s:
    
    if item=="\\":
        stack.append(i)
    elif item=="/":
        if len(stack)>0:
            temp=stack.pop()
            result+=i-temp
            left=mizutamari.pop()
            if left[0]>temp:
                vol=i-temp
                while 1:
                    vol=left[1]+vol
                    mizutamari.append([temp,vol])
                    if len(mizutamari)<=2:
                        break
                    bk=mizutamari.pop()
                    bkbk=mizutamari.pop()
                    if bkbk<bk:
                        mizutamari.append(bkbk)
                        mizutamari.append(bk)
                        break
                    else:
                        left=bkbk




                
            else:
                
                mizutamari.append(left)
                mizutamari.append([temp,i-temp])
                


    i+=1
print(result)
s=""
for item in mizutamari[1:]:
    s+=str(item[1])+" "

if result==0:
    print(0)
else:
    print(len(mizutamari)-1,s[:-1])

Solution (Python):
def cal_water(floods):
    fstack = []
    astack = []
    i = 0
    for flood in floods:
        if flood == '\\':
            fstack.append(i)
        elif flood == '/' and len(fstack) > 0:
            s = fstack.pop(-1)
            area = (i - s) * 1
            while len(astack) > 0 and astack[-1][0] > s:
                _, a = astack.pop(-1)
                area += a
            astack.append((s, area))   
        else:
            pass
        i += 1
    
    tot = 0
    tot_area = 0
    out = ''
    while len(astack) > 0:
        _, area = astack.pop()
        tot += 1
        tot_area += area
        out = ' ' + str(area) + out
    
    print(tot_area)
    print(str(tot) +  out)

if __name__ == '__main__':
    floods = input()
    cal_water(floods)

Solution (Python):
from collections import deque
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    inf = 10 ** 6
    s = input()
    n = len(s)
    land = [0] * (n + 1)
    for i, c in enumerate(s):
        if c == "/":
            land[i + 1] = land[i] + 1
        elif c == "_":
            land[i + 1] = land[i]
        else:
            land[i + 1] = land[i] - 1
    

    water = [0] * (n + 1)
    w = -inf
    i = n
    for l in land[::-1]:
        if l > w: w = l
        water[i] = w
        i -= 1
    w = -inf
    for i, l in enumerate(land):
        if l > w: w = l
        water[i] = min(water[i], w)
    

    mm = []
    sum_d = 0
    for w, l in zip(water, land):
        d = w - l
        if d == 0 and sum_d:
            mm.append(sum_d)
            sum_d = 0
        else:
            sum_d += d

    print(sum(mm))
    print(len(mm), *mm)

main()

Solution (Python):
from enum import Enum
import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

class Info:
    def __init__(self,arg_start,arg_end,arg_S):
        self.start = arg_start
        self.end = arg_end
        self.S = arg_S


LOC = []
POOL = []

line = input()
loc = 0
sum_S = 0

for ch in line:

    if ch == '\\':

        LOC.append(loc)

    elif ch == '/':

        if len(LOC) == 0:
            continue

        tmp_start = int(LOC.pop())
        tmp_end = loc
        tmp_S = tmp_end-tmp_start;
        sum_S += tmp_S;

        
        while len(POOL) > 0:
            if POOL[-1].start > tmp_start and POOL[-1].end < tmp_end:
                tmp_S += POOL[-1].S
                POOL.pop()
            else:
                break

        POOL.append(Info(tmp_start,tmp_end,tmp_S))

    else:
        pass

    loc += 1

print("%d"%(sum_S))
print("%d"%(len(POOL)),end = "")

while len(POOL) > 0:
    print(" %d"%(POOL[0].S),end = "") 
    POOL.pop(0)

print()

Solution (Python):
from collections import deque


def calc_area(chars):
    s1 = deque()
    s2 = deque()
    area = 0

    for idx, char in enumerate(chars):
        if char == "\\":
            s1.append(idx)
        elif char == "/":
            if s1:
                left_idx = s1.pop()
                sub_area = idx - left_idx
                area = area + sub_area
                while s2 and left_idx < s2[-1][0]:
                    sub_area += s2.pop()[1]
                s2.append((left_idx, sub_area))
    return area, s2


def main():
    chars = input()
    area, s2 = calc_area(chars)
    print(area)
    text = ""
    count = 0
    while s2:
        text += " " + str(s2.popleft()[1])
        count += 1
    print(str(count) + text)


if __name__ == "__main__":
    main()

Solution (Python):

class Stack:
    def __init__(self, data=None):
        
        
        
        
        if data is None:
            self.data = []
        else:
            self.data = data

    def push(self, x):
        self.data.append(x)

    def pop(self):
        if self.is_empty() is True:
            return None
        else:
            return self.data.pop(-1)

    def is_empty(self):
        if len(self.data) == 0:
            return True
        else:
            return False

    def get_length(self):
        return len(self.data)

    def reverse(self):
        self.data.reverse()





diagram = input()
A = 0  
k = 0  
L = []  

s1 = Stack()
s2 = Stack()

new_flood = False






for i, vector in enumerate(diagram):
    
    

    if vector == '\\':
        
        
        s1.push(i)

        
        
        
        

        new_flood = True

    elif vector == '/':
        
        j = s1.pop()
        if j is not None:
            
            A += i - j

            
            
            
            
            

            size = i - j

            if new_flood is True:
                s2.push({'first_pos': j, 'size': size})
                new_flood = False

            
            

            else:
                current_flood = s2.pop()
                current_flood['first_pos'] = j
                current_flood['size'] += size

                while True:
                    prev_flood = s2.pop()
                    if prev_flood is not None:
                        if prev_flood['first_pos'] > j:
                            current_flood['size'] += prev_flood['size']
                        else:
                            s2.push(prev_flood)
                            s2.push(current_flood)
                            break
                    else:
                        s2.push(current_flood)
                        break

    
    
    

print(A)

output = str(s2.get_length())
for elem in s2.data:
    output += ' ' + str(elem['size'])

print(output)

Solution (Python):
from collections import deque
input_list = list(input())

left_idx_stack = deque()
puddle_stack = deque()

for idx, s in enumerate(input_list):
    if s == "\\":
        left_idx_stack.append(idx)
    
    elif s == '/':
        if left_idx_stack:
            latest_left_idx = left_idx_stack.pop()
            area = idx - latest_left_idx
        
            if not puddle_stack:
                puddle_stack.append((latest_left_idx, area))
            
            elif latest_left_idx > puddle_stack[-1][0]:
                puddle_stack.append((latest_left_idx, area))
            else:
                
                while len(puddle_stack) != 0 and (latest_left_idx < puddle_stack[-1][0]):
                    latest_puddle = puddle_stack.pop()
                    area += latest_puddle[1]
                    
                puddle_stack.append((latest_left_idx, area))
        else:
            pass
areas = [p[1] for p in puddle_stack]
print(sum(areas))
print(len(areas), *areas)