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):

strSlopeFlat = list(input())

stack_total = []
starck_areas = []

for i,x in enumerate(strSlopeFlat):
    if x == '\\':
        stack_total.append(i)
    elif x == '/' :
        onearea = 0
        if len(stack_total) > 0:
            pos = stack_total.pop()
            onearea = (i-pos)
            while len(starck_areas)>0 and starck_areas[-1][0] > pos:
                tmp_pop = starck_areas.pop()
                onearea += tmp_pop[1]
            starck_areas.append([pos,onearea])

print(sum([y for x,y in starck_areas]))
results = [x[1]  for x in starck_areas if x[1] != 0]
print(len(results),*results)

Solution (Python):
s1 = []   
s2 = []   
su = 0   

st = list(input())   

for i in range(len(st)):   
    if st[i] == '\\':
        s1.append(i)
    elif st[i] == '/' and len(s1) != 0:
        left = s1.pop()
        su += i - left
        area = 0
        while len(s2) != 0 and left < s2[-1][0]:
            area += s2.pop()[1]
        s2.append([left,area + i -left])

print(su)
print(' '.join([str(len(s2))] + [str(i[1]) for i in s2]))

Solution (Python):
s = input()
a = []
down = []
a_sum = 0
for i,line in enumerate(s):
    if line == "\\":
        down.append(i)
    elif line == "/" and down:
        j = down.pop()
        a_tmp = i-j
        a_sum += a_tmp
        while a and a[-1][0] > j:
            a_tmp += a.pop()[1]
        a.append([j,a_tmp])

print(a_sum)
print(len(a),*[s for i,s in a])

Solution (Python):
def ALDS1_3D():
    s0, s1 = [], []
    for i, e in enumerate(input()):
        if e == '\\':
                s0.append(i)
        elif e == '/':
            if len(s0) > 0:
                j = s0.pop()
                s = i - j
                while len(s1) > 0 and j < s1[-1][0]:
                    s += s1[-1][1]
                    s1.pop()
                s1.append((j, s))

    ss = [s[1] for s in s1]
    print(sum(ss))
    print(len(ss), *ss)





if __name__ == '__main__':
    ALDS1_3D()

Solution (Python):

from collections import deque

def main():
    s1 = deque()
    s2 = deque()
    total = 0

    string = input()

    for i in range(len(string)):
        if string[i] == '\\':
            s1.append(i)
        elif string[i] == '/' and len(s1) > 0:
            j = s1.pop()
            a = i - j
            total += a

            while (len(s2) > 0 and s2[-1][0] > j):
                a += s2[-1][1]
                s2.pop()

            s2.append((j, a))

    ans = []
    while (len(s2) > 0):
        ans.append(s2[-1][1])
        s2.pop()

    print(total)

    print(len(ans), *ans[::-1])

if __name__ == "__main__":
    main()

Solution (Python):

import sys


def cross_section_diagram(diagram):
    stack = []
    area = []
    for i, ch in enumerate(diagram):
        if ch == "\\":
            stack.append(i)
        elif ch == "/":
            if stack:
                point = stack.pop()
                area.insert(0, (point, i - point))
        else:
            pass
    result = []
    if area:
        p1, cnt1 = area.pop(0)
        for p2, cnt2 in area:
            if p1 < p2:
                cnt1 += cnt2
            else:
                result.insert(0, cnt1)
                p1 = p2
                cnt1 = cnt2
        result.insert(0, cnt1)

    print(sum(result))
    result.insert(0, len(result))
    print(" ".join([str(n)for n in result]))
    return result


if __name__ == "__main__":
    diagram = [val for val in sys.stdin.read().splitlines()]
    cross_section_diagram(diagram[0])

Solution (Python):
def main():
    line = input()
    s1 = []
    s2 = []
    sum = 0
    for i, s in enumerate(line):
        if s == "\\":
            
            s1.append(i)
        elif s == "/" and len(s1) > 0:
            
            j = s1.pop()
            sum += i - j  
            
            a = i - j
            while len(s2) > 0 and s2[-1][0] > j:
                a += s2.pop()[1]
            
            s2.append((j, a))

    print(sum)
    if len(s2) > 0:
        print(len(s2), end="")
        for x in s2:
            print(f" {x[1]}", end="")
        print()
    else:
        print(len(s2))


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    line = input()
    s1 = []
    s2 = []
    sum = 0
    for i, s in enumerate(line):
        if s == "\\":
            
            s1.append(i)
        elif s == "/" and len(s1) > 0:
            
            j = s1.pop()
            sum += i - j  
            
            a = i - j
            while len(s2) > 0 and s2[-1][0] > j:
                a += s2.pop()[1]
            
            s2.append((j, a))

    print(sum)
    print(len(s2), end="")
    for x in s2:
        print(f" {x[1]}", end="")
    print()


if __name__ == "__main__":
    main()

Solution (Python):

t=input()
s1=[]
s2=[]
pond=[]
totalsum=0

for i in range(len(t)):
    if t[i]=="\\" :
        s1.append(i)
    elif t[i]=="/" and len(s1)!=0:
        p1=s1.pop()
        sum=i-p1
        totalsum+=sum
        while len(s2)>0:
            if s2[-1]>p1:
                s2.pop()
                sum+=pond.pop()
            else:
                break
        pond.append(sum)
        s2.append(i)


print(totalsum)
print(len(pond),*pond)

Solution (Python):
li = []
for i, s in enumerate(input()):
    if s == "\\":
        li.append([i, 0])
    elif s == "/":
        if li:
            if li[-1][1] == 0:
                li[-1][1] = i - li[-1][0]
            else:
                for j in range(len(li) - 1, -1, -1):
                    if li[j][1] == 0:
                        li = li[:j] + [[li[j][0], sum(tuple(zip(*li[j + 1:]))[1]) + i - li[j][0]]]
                        break
ans = []
for a in li:
    if a[1] != 0:
        ans.append(a[1])
print(sum(ans))
print(len(ans), *ans)

Solution (Python):
import sys
readline = sys.stdin.readline
li = []
for i, s in enumerate(readline().strip()):
    if s == "\\":
        li.append([i, 0])
    elif s == "/":
        if li:
            if li[-1][1] == 0:
                li[-1][1] = i - li[-1][0]
            else:
                for j in range(len(li) - 1, -1, -1):
                    if li[j][1] == 0:
                        li = li[:j] + [[li[j][0], sum(tuple(zip(*li[j + 1:]))[1]) + i - li[j][0]]]
                        break
ans = []
for a in li:
    if a[1] != 0:
        ans.append(a[1])
print(sum(ans))
print(len(ans), *ans)

Solution (Python):



if __name__ == '__main__':

    s1 = []
    s2 = []
    all_area = 0
    
    for i, c in enumerate(input()):
        if c == "\\":
            s1.append([c, i])
        elif c == "/":
            if len(s1) > 0:
                _, j = s1.pop()
                area = i - j
                all_area += area

                while len(s2)>0 and s2[-1][0]>j:
                    area += s2.pop()[1]
                s2.append([j, area])
                
    print(all_area)
    ret = [len(s2)]
    for w in s2:
        ret.append(w[1])
    print(" ".join(map(str, ret)))

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

Solution (Python):
li1 = []
li2 = []
for i, s in enumerate(input()):
    if s == "\\":
        li1.append(i)
    elif s == "/" and li1:
        j = li1.pop()
        c = i - j
        while li2 and li2[-1][0] > j:
            c += li2[-1][1]
            li2.pop()
        li2.append((j, c))
if li2:
    li3 = list(zip(*li2))[1]
    print(sum(li3))
    print(len(li3), *li3)
else:
    print(0, 0, sep="\n")

Solution (Python):
def cross_section_area(data):
    area = 0;
    sections = [];
    stack = [];
    for i in range(len(data)):
        if data[i] == '\\':
            stack.append(i);
        elif data[i] == '/' and len(stack) > 0:
            j = stack.pop();
            section_area = i - j;
            area += section_area;
            while len(sections) > 0 and sections[-1][0] > j:
                section_area += sections[-1][1];
                sections.pop();
            sections.append([j, section_area]);
    print(area);
    print(len(sections), *[n[1] for n in sections]);

cross_section_area(input());

Solution (Python):
def check(aline):
    pools = []
    leftedge = []

    for i, c in enumerate(aline):
        if c == "\\":
            leftedge.append(i)
        elif c == "_":
            pass
        else:
            if leftedge:
                left = leftedge.pop()
                w = i - left

                while pools and left < pools[-1][0]:
                    w += pools.pop()[1]

                pools.append((left, w))

    return pools


if __name__ == '__main__':

    al = input().strip()
    ps = check(al)

    print(sum([x[1] for x in ps]))
    print(len(ps), *[x[1] for x in ps])

Solution (Python):
s=input()
slash=[]
area=0

slash_2=[]
area_2=[]

for i in range(len(s)):
    if s[i]=="\\":
        slash.append(i)
    elif s[i]=="/":
        try: 
            j=slash.pop()
            area=area+i-j
            a=i-j
            while len(area_2) > 0 and slash_2[len(slash_2)-1] >j:
                a=a+area_2[len(slash_2)-1]
                area_2.pop()
                slash_2.pop() 
            slash_2.append(j)     
            area_2.append(a)
        except: pass


if area==0:
    print(0)
    print(0)
else:
    print(area)
    print(str(len(area_2))+" "+" ".join(map(str, area_2)))

Solution (Python):
hoge1 = list()
hoge2 = list()
total = 0
for num, c in enumerate(input()):
    if c == '\\':
        hoge1.append(num)
    elif c == '/':
        if not hoge1:
            continue
        last_index = hoge1.pop()
        S = num - last_index
        if not hoge2:
            hoge2.append((last_index, S))
        else:
            if last_index >= hoge2[-1][0]:
                hoge2.append((last_index, S))
            else:
                new_S = S
                for i, j in hoge2[::-1]:
                    if last_index >= i:
                        break
                    else:
                        new_S += hoge2.pop()[1] 
                hoge2.append((last_index, new_S))

totals = [x[1] for x in hoge2]
print (sum(totals))
totals.insert(0, len(hoge2))
print (' '.join([str(x) for x in totals]))

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):
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):
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):
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):
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):
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):



import sys

class peak():
    def __init__(self, loc, elevation):
        self.loc = loc
        self.elevation = elevation
        self.next_high = None


def peak_append(peak_list, loc, elevation):
    p = peak(loc, elevation)
    peak_list.append(p)
    return

def peak_set_next_high(peak_list):
    next_high = peak_list[-1]   
    for i in range(len(peak_list)-2, -1, -1):
        peak_list[i].next_high = next_high
        if peak_list[i].elevation >= next_high.elevation:
            next_high = peak_list[i]


def find_flood(elev_list, peak_list):
    peak_p = peak_list.pop(0)
    next_high = peak_p.next_high
    if not next_high:
        return None, None
    q_elev = next_high.elevation
    p_elev = peak_p.elevation

    if p_elev > q_elev:
        q_offset = next_high.loc
        p_offset = peak_p.loc
        while p_offset < len(elev_list):
            if elev_list[p_offset] == elev_list[q_offset]:
                return p_offset, q_offset
            p_offset += 1
        else:
            return None, None
    else:
        p_offset = peak_p.loc
        q_offset = elev_list.index(p_elev, p_offset+1)
        return p_offset, q_offset
    raise "Logic Error"


def calc_area(p, q, elev_list):
    elev_0 = elev_list[p]
    area = 0

    prev_elev = 0
    for x in range(p, q+1):
        elev = elev_list[x] - elev_0
        area += elev

    return (abs(area))


def adjust_peak_list(q, peak_list):
    while peak_list:
        if peak_list[0].loc >= q:
            return
        peak_list.pop(0)


s = list(sys.stdin.readline().strip())

flood_area = list()
peak_list = list()

ascending = True
elevation = 0
elev_list = list()

for i in range(len(s)):
    elev_list.append(elevation)

    if s[i] == '_':
        pass
    elif s[i] == '/':
        ascending = True
        elevation += 1
    elif s[i] == '\\':
        if ascending:
            peak_append(peak_list, i, elevation)
        ascending = False
        elevation -= 1

elev_list.append(elevation)

if ascending:
    peak_append(peak_list, len(s), elevation)

peak_set_next_high(peak_list)

while peak_list:
    p, q = find_flood(elev_list, peak_list)
    if p == None:
        break
    flood_area.append(calc_area(p, q, elev_list))
    adjust_peak_list(q, peak_list)
    
print(sum(flood_area))
print(len(flood_area), end='')
for i in range(len(flood_area)):
    print(' ', flood_area[i], sep='', end='')
print()

Solution (Python):
s = input()
i = 0
S1 = []
S2 = []
water = []
wtot = 0
while i < len(s):
    if s[i] == "\\":
        S1.append(i)
    elif s[i] == '/' and len(S1) != 0:
        w0 = (i - S1[-1])
        wtot += w0
        if len(S2) == 0:
            S2.append((S1[-1], w0))
        else:
            while len(S2) != 0 and S2[-1][0] > S1[-1]:
                w0 += S2[-1][1]
                S2.pop()
            S2.append((S1[-1], w0))
        S1.pop()
    i += 1

ans = str(wtot) + '\n' + str(len(S2))
for p in S2:
    ans += ' ' + str(p[1])
print(ans)

Solution (Python):
def find_lakes(diagram):
    """Find lakes in diagram.

    >>> pat = str.maketrans('ufd', '/_\\\\')
    >>> find_lakes('ddfuu'.translate(pat))
    [6]
    >>> find_lakes('dduuudfududdddufudduuuffdddfddufdufud'.translate(pat))
    [4, 2, 1, 19, 9]
    >>> find_lakes('ff'.translate(pat))
    []
    """
    def calc_areas(ls):
        areas = []
        e1, e2 = 0, 0
        for n, m in reversed(ls):
            if e1 < n and e2 > m:
                area = areas.pop()
                areas.append(area + (m - n))
            else:
                areas.append(m - n)
                e1, e2 = n, m
        return areas[::-1]

    lakes = []
    slopes = []

    for i, c in enumerate(diagram):
        if c == '\\':
            slopes.append(i)
        elif c == '/':
            if len(slopes) > 0:
                lakes.append((slopes.pop(), i))

    return calc_areas(lakes)


def run():
    ls = find_lakes(input())

    summary = [str(len(ls))] + [str(i) for i in ls]
    print(sum(ls))
    print(" ".join(summary))


if __name__ == '__main__':
    run()

Solution (Python):
string = input()
height = 0
height_list = [[0, False]]
for s in string:
    if s == '\\':
        height += -1
    elif s == '/':
        height += 1
    else:
        pass
    height_list.append([height, False])

highest = 0
for i in range(1, len(height_list)):
    if height_list[i-1][0] < height_list[i][0] <= highest:
        height_list[i][1] = True
    highest = max(highest, height_list[i][0])

puddles = []
area = 0
surface_level = None

for i in range(len(height_list)-1, -1, -1):
    if surface_level != None:
        area += surface_level - height_list[i][0]
        if surface_level == height_list[i][0]:
            puddles += [area]
            surface_level = None
            area = 0
    if surface_level == None and height_list[i][1]:
        surface_level = height_list[i][0]

puddles = puddles[::-1]
print(sum(puddles))
print(len(puddles), *puddles)

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):
from collections import deque
s = input()
g = deque()
p = deque()
for i in range(len(s)):
    if s[i] == "\\":
        g.append(i)
    elif s[i] == "/":
        a = 0
        if len(g) > 0:
            while True:
                if len(p) > 0 and p[-1][0] > g[-1]:
                    a += p[-1][1]
                    p.pop()
                else:
                    break
            a += i - g[-1]
            p.append([g[-1], a])
            g.pop()
for i in range(len(p)):
    p[i] = p[i][1]
if len(p) > 0:
    print(sum(p))
    print(len(p), end = " ")
    print(" ".join(map(str, p)))
else:
    print(0)
    print(0)

Solution (Python):

"""
Created on Sun Apr 29 23:10:10 2018

@author: maezawa
"""

s = input()
n = len(s)

st = []
st2 = []
total_sum = 0

for i, c in enumerate(s):
    if c == '\\': 
        st.insert(0,i)
    elif c == '/' and len(st)>0: 
        j = st.pop(0)
        total_sum += i - j
        a = i - j
        while len(st2) > 0 and st2[0][0] > j:
            a += st2.pop(0)[1]
        st2.insert(0, [j, a])
        
print(total_sum)
print(len(st2), end='')
for i in reversed(range(len(st2))):
    print(" {}".format(st2[i][1]), end='')
print()

Solution (Python):
stack = []
res = []

area = input()

for i, terrain in enumerate(area):
    if terrain == '\\':
        stack.append(i)
    elif terrain == '/' and len(stack)>0:
        left = stack.pop()
        pond = i - left
        if len(res) == 0 or left >= res[-1][0]:
            res.append([left,pond])
        else:
            while len(res) > 0 and left < res[-1][0] :
                pond += res[-1][1]
                res.pop()
            res.append([left,pond])

total = 0
ponds = []
for i in res:
    ponds.append(i[1])
    total += i[1]

ans = [len(res)]
ans += ponds
ans_new = " ".join(map(str,ans))

print(total)
print(ans_new)

Solution (Python):
cross_section = input()

stack = []
areas_stack = []
area = 0
for i in range(len(cross_section)):
    c = cross_section[i]
    if c == '\\':
        stack.append(i)
    elif c == '/' and len(stack) != 0:
        j = stack.pop()
        a = i - j
        area += a
        while len(areas_stack) != 0 and areas_stack[-1][0] > j:
            a += areas_stack.pop()[1]
        areas_stack.append([j, a])

print(area)
if area == 0:
    print(0)
else:
    print(len(areas_stack), ' '.join([str(x[1]) for x in areas_stack]))

Solution (Python):
class Stack:
    def __init__(self, full_idx=10):
        self.stack = [None] * full_idx
        self.top = 0
        self.MAX = full_idx

    def is_empty(self):
        return self.top == 0

    def is_full(self):
        return self.top >= self.MAX - 1

    def push(self, x):
        if self.is_full():
            raise IndexError

        self.top += 1
        self.stack[self.top] = x

    def pop(self):
        if self.is_empty():
            raise IndexError

        self.top -= 1
        return self.stack[self.top+1]


def main():
    s = input()
    s1, s2 = Stack(20000), Stack(20000)
    pond = [0]
    area = 0

    for i in range(len(s)):
        if s[i] == "\\":
            s1.push(i)
            s2.push([i, 0])

        elif s[i] == "/":
            if not s1.is_empty():
                area += i - s1.pop()
            if not s2.is_empty():
                tup = s2.pop()
                area2 = 0

                while tup[1] != 0:
                    tup2 = s2.pop()
                    tup, tup2 = tup2, tup
                    area2 += tup2[1]

                tup[1] = i - tup[0] + area2

                if s2.is_empty():
                    pond.append(tup[1])
                else:
                    s2.push(tup)

    s3 = Stack(20000)
    while not s2.is_empty():
        ss = s2.pop()
        if ss[1] != 0:
            s3.push(ss)

    while not s3.is_empty():
        pond.append(s3.pop()[1])

    pond[0] = len(pond) - 1
    print(area)
    print(" ".join([str(i) for i in pond]))


if __name__ == '__main__':
    main()

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):
A=[]
p=[]
a=i=0
for c in input():
 if"\\"==c:A+=[i]
 elif"/"==c and A:
  j=A.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):
A=[]
B=[]
a=i=0
for c in input():
 if"\\"==c:A+=[i]
 elif"/"==c and A:
  j=A.pop()
  t=i-j
  a+=t
  while B and B[-1][0]>j:t+=B[-1][1];B.pop()
  B+=[(j,t)]
 i+=1
print(a)
if B:print(len(B),*list(zip(*B))[1])
else:print(0)

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

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

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

Solution (Python):
k=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;k+=c
        while b and b[-1]>j:c+=d.pop();b.pop()
        b+=[j];d+=[c]
print(k)
print(len(b),*(d))

Solution (Python):
inputs = list(input())
stack = []
L = []
tmp_total = 0
counter = 0
upper = 0
for i,x in enumerate(inputs):
    if(x == '\\'):
        stack.append(i)
    elif(x == '/') and stack:
        tmp = stack.pop()
        total_layer = i-tmp
        if L:
            while L and L[-1][0] > tmp:
                cal = L.pop()
                total_layer += cal[1]
        L.append((i, total_layer))

print(sum([j[1] for j in L]))
if(len(L) != 0):
    print(str(len(L))+ ' ' + ' '.join([str(i[1]) for i in L]))
else:
    print(str(len(L)))

Solution (Python):
ary = list(input())
s1 = []
s = 0

s2 = []

cnt = 0
for _ in ary:

    if _ == '\\':
        s1.append(cnt)
        cnt += 1

    elif _ == '/':
        if s1:
            old_cnt = s1.pop()
            area = cnt - old_cnt
            s += area

            while s2:
                if old_cnt < s2[-1][0]:
                    area += s2.pop()[1]
                else:
                    break

            s2.append((old_cnt, area))

        cnt += 1

    else:
        cnt += 1

print(s)
print(len(s2), *[_[1] for _ in s2])

Solution (Python):


def show(a):
    
    _str = ""
    for i in range(len(a) - 1):
        _str += str(a[i]) + " "
    _str += str(a[len(a) - 1])
    print(_str)

def main():
    data = list(input())
    a = []  
    b = []  
    ponds = []  
    Area = 0  
    N = len(data)
    for i in range(N):
        if data[i] == '\\':
            a.append(i); b.append(i)
        elif data[i] == '/':
            if len(a) == 0: continue
            else:
                left = a.pop()  
                Area += i - left
                ponds.append(i - left)
                
                
                while left < b[len(b) - 1]:
                    b.pop()
                    x = ponds.pop(); y = ponds.pop(); ponds.append(x + y)
    print(Area)
    ponds.insert(0, len(ponds))
    show(ponds)
    
if __name__ == "__main__":
    main()

Solution (Python):

data = input()
xmax = len(data)
data_list = list(data)
data_list.reverse()
reverse_data = []
for sig in data_list:
    if sig == "\\":
        reverse_data.append("/")
    elif sig == "/":
        reverse_data.append("\\")
    else:
        reverse_data.append(sig)

reverse_data = "".join(reverse_data)


def partialSqu(h, sig):
    if sig == "\\":
        squ = h + 1/2
        h += 1
    elif sig == "/":
        squ = h - 1/2
        h -= 1
    elif sig == "_":
        squ = h
    return squ, h


x_squ_dict = {}
cnt = 0
sw, x, h, totalSqu = 0, 0, 0, 0
for sig in data:
    x += 1
    if sw == 0 and sig == "\\":
        sw = 1
    if sw == 1 :
        squ, h = partialSqu(h, sig)
        totalSqu += squ
        
        if h == 0:
            x_squ_dict[x] = totalSqu
            totalSqu = 0
            sw = 0



keys = x_squ_dict.keys()
sw, x, h, totalSqu = 0, 0, 0, 0
for sig in reverse_data:
    x += 1
    if sw == 0 and sig == "\\" :
        sw = 1
        x_p = xmax - x +1
    if sw == 1 :
        squ, h = partialSqu(h, sig)
        totalSqu += squ            
        if h == 0:
            x_squ_dict[x_p] = totalSqu
            totalSqu = 0
            sw = 0


keys = x_squ_dict.keys()
keys = list(keys)
keys.sort()
squ_list = []
for key in keys:
    squ_list.append(x_squ_dict[key])
    
a = int(sum(squ_list))
print(a)    
squ_list.insert(0,len(keys))

squ_list = " ".join([str(int(num)) for num in squ_list])
print(squ_list)

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):
def print_list_split_whitespace(a):
    for x in a[:-1]:
        print(x, end=' ')
    print(a[-1])
    
s = list(input())
st1 = []
st2 = []

for index, c in enumerate(s):
    if c == "\\":
        st1.append(index)
    elif c == "/":
        if len(st1) >= 1:
            pop_index = st1.pop()
            area = index - pop_index
            while True:
                if len(st2) >= 1:
                    ind, pop_area = st2.pop()
                else:
                    st2.append((index, area))
                    break
                if ind < pop_index:
                    st2.append((ind, pop_area))
                    st2.append((index, area))
                    break
                else:
                    area += pop_area

a = 0
k = len(st2)
ls = [k]
for _, s in st2:
    a += s
    ls.append(s)

print(a)
print_list_split_whitespace(ls)

Solution (Python):
from sys import stdin
def stdinput():
    return stdin.readline().strip()

def main():
    altitutes = load_data()

    edges = create_edges(altitutes)

    

    areas = calc_areas(altitutes, edges)

    print(int(sum(areas)))
    print(*([len(areas)] + [*map(int, areas)]))

def load_data():
    A = [0]
    for s in stdinput():
        if s == '\\':
            A.append(A[-1] - 1)
        elif s == '_':
            A.append(A[-1])
        elif s == '/':
            A.append(A[-1] + 1) 
    return A

def create_edges(A):
    st = [(0, None, 0)]
    highest = 0
    for i, a in enumerate(A[1:], 1):
        if st[-1][0] >= a:
            st.append((a, st[-1][2], i))
        else:
            begin = i - 1
            
            
            
            while st[-1][1] != None and st[-1][0] == a - 1 and highest >= a:
                lev, begin, j = st.pop()
            highest = max(highest, a)
            st.append((a, begin, i))
        
    return [*map(lambda e: e[1:], st[1:])]

def calc_areas(A, edges):
    areas = []
    for begin, end in edges:
        area = calc_area(A, begin, end)
        if area > 0:
            areas.append(area)
    return areas    

def calc_area(A, begin, end):
    if A[begin] != A[end]:
        return 0
    b_level = A[begin]
    area = 0
    for l1, l2 in zip(range(begin, end), range(begin+1, end + 1)):
        a1 = A[l1]
        a2 = A[l2]
        area += (b_level - a1 + b_level - a2) * 1 / 2
    return area

if __name__ == '__main__':
    main()

Solution (Python):


S = input()


v = {'\\': -1, '_': 0, '/': 1}
h = 0  
H = [h]
for s in S:
    h += v[s]
    H.append(h)


R = [0] * len(H)
R[-1] = -float('inf')
for i in range(len(R) - 2, -1, -1):
    R[i] = max(R[i+1], H[i+1])

A = []
i = 0
while True:
    
    while i < len(S) and S[i] != '\\':
        i += 1

    
    while i < len(S) and (H[i] > R[i] or S[i] == '_'):
        i += 1

    if i >= len(S):
        break

    
    h = H[i]  
    a = 0     
    while i < len(S):
        a += (h - H[i]) + (H[i] - H[i+1])/2
        i += 1
        if H[i] == h: break
    A.append(int(a))

print(sum(A))
print(' '.join(map(str, [len(A)] + A)))

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):
x = input()
y = []
z = []
n = 0
for i in range(len(x)):
    if x[i] == '\\':
        if n:
            y.append(n)
            n = 0
            if z:
                z.append(-1)
        z.append(i)
    elif x[i] == '/':
        if z:
            while True:
                p = z.pop()
                if p == -1:
                    n += y.pop()
                else:
                    break
            n += i-p
    else:
        pass
if n:
    y.append(n)
for j in range(y.count(0)):
    y.remove(0)
print(sum(y))
print(len(y), *y)