Problem:


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

Areas on the Cross-Section Diagram

  Your task is to simulate a flood damage.

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

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

Input

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

output

  Report the areas of floods in the following format:

$A$

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

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

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

Constraints

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

Sample Input 1

\\//

Sample Output 1

4
1 4

Sample Input 2

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

Sample Output 2

35
5 4 2 1 19 9



Sample input:
\\//


Sample output:
4
1 4

Submission status: Wrong Answer

Solution code:
class Cell(object):
    def __init__(self, direction, x, y):
        self.direction = direction
        self.x = x
        self.y = y
        self.is_pond = False

    def get_area(self, pond_height):
        h = pond_height - self.y + 1
        if self.direction == 'f':
            return h
        else:
            return h - 0.5

    @property
    def char(self):
        if self.direction == 'd':
            return '/'
        elif self.direction == 'u':
            return '\\'
        elif self.direction == 'f':
            return '|'

class Area(object):
    def __init__(self):
        self.cells = []
        self.x = 0
        self.height = 0
        self.pond_idx = []
        self.area_list = []

    @property
    def top(self):
        return max([c.y for c in self.cells])
    
    def put(self, c):
        if c == '\\':
            self.height -= 1
            self.cells.append(Cell('d', self.x, self.height))
        elif c == '/':
            self.cells.append(Cell('u', self.x, self.height))
            self.height += 1
        elif c == '_':
            self.cells.append(Cell('f', self.x, self.height))
        self.x += 1

    def adjust_height(self):
        lowest = min([c.y for c in self.cells])
        for c in self.cells:
            c.y -= lowest
    
    def is_pond(self):
        for i_start in range(len(self.cells)):
            c_start = self.cells[i_start]
            if c_start.direction == 'u':
                continue
            if c_start.direction == 'f' and 0 == i_start:
                continue

            for i_end in range(i_start+1, len(self.cells)):
                c_end = self.cells[i_end]
                if c_start.y == c_end.y:
                    if c_end.direction == 'u':
                        c_start.is_pond = True
                        c_end.is_pond = True
                    elif c_end.direction == 'f':
                        continue
                    break

    def collect_pond_idx(self):
        start = None
        start_c = None
        for c in self.cells:
            if c.is_pond and start == None:
                start = c.x
                start_c = c
            elif not c.is_pond and start != None:
                self.pond_idx.append((start, c.x - 1))
                start = None
                start_c = None
            elif c.is_pond and c.direction == 'd' and c.y == start_c.y:
                self.pond_idx.append((start, c.x - 1))
                start = c.x
                start_c = c
        if start != None:
            self.pond_idx.append((start, self.cells[-1].x))

    def get_area(self, idx):
        area = 0
        pond_height = self.cells[idx[0]].y
        for i in range(idx[0], idx[1]+1):
            area += self.cells[i].get_area(pond_height)
        return int(area)

    def get_area_list(self):
        for pond in self.pond_idx:
            self.area_list.append(self.get_area(pond))

    def show(self):
        for i, c in enumerate(self.cells):
            p = 'p' if c.is_pond else ' '
            print('{0:02d}'.format(i) + p + c.char * c.y)

def run():
    area = Area()
    for l in input():
        area.put(l)
    area.adjust_height()
    area.is_pond()
    area.collect_pond_idx()
    area.get_area_list()
    area.show()
    print(sum(area.area_list))
    print(len(area.area_list), ' '.join([str(a) for a in area.area_list]))

if __name__ == '__main__':
    run()

Submission status: Wrong Answer

Solution code:
class Cell(object):
    def __init__(self, direction, x, y):
        self.direction = direction
        self.x = x
        self.y = y
        self.is_pond = False

    def get_area(self, pond_height):
        h = pond_height - self.y + 1
        if self.direction == 'f':
            return h
        else:
            return h - 0.5

    @property
    def char(self):
        if self.direction == 'd':
            return '/'
        elif self.direction == 'u':
            return '\\'
        elif self.direction == 'f':
            return '|'

class Area(object):
    def __init__(self):
        self.cells = []
        self.x = 0
        self.height = 0
        self.pond_idx = []
        self.area_list = []

    @property
    def top(self):
        return max([c.y for c in self.cells])
    
    def put(self, c):
        if c == '\\':
            self.height -= 1
            self.cells.append(Cell('d', self.x, self.height))
        elif c == '/':
            self.cells.append(Cell('u', self.x, self.height))
            self.height += 1
        elif c == '_':
            self.cells.append(Cell('f', self.x, self.height))
        self.x += 1

    def adjust_height(self):
        lowest = min([c.y for c in self.cells])
        for c in self.cells:
            c.y -= lowest
    
    def is_pond(self):
        for i_start in range(len(self.cells)):
            c_start = self.cells[i_start]
            if c_start.direction == 'u':
                continue
            if c_start.direction == 'f' and 0 == i_start:
                continue

            for i_end in range(i_start+1, len(self.cells)):
                c_end = self.cells[i_end]
                if c_start.y == c_end.y:
                    if c_end.direction == 'u':
                        c_start.is_pond = True
                        c_end.is_pond = True
                    elif c_end.direction == 'f':
                        continue
                    break

    def collect_pond_idx(self):
        start = None
        start_c = None
        for c in self.cells:
            if c.is_pond and start == None:
                start = c.x
                start_c = c
            elif not c.is_pond and start != None:
                self.pond_idx.append((start, c.x - 1))
                start = None
                start_c = None
            elif c.is_pond and c.direction == 'd' and c.y == start_c.y:
                self.pond_idx.append((start, c.x - 1))
                start = c.x
                start_c = c
        if start != None:
            self.pond_idx.append((start, self.cells[-1].x))

    def get_area(self, idx):
        area = 0
        pond_height = self.cells[idx[0]].y
        for i in range(idx[0], idx[1]+1):
            area += self.cells[i].get_area(pond_height)
        return int(area)

    def get_area_list(self):
        for pond in self.pond_idx:
            self.area_list.append(self.get_area(pond))

    def show(self):
        for i, c in enumerate(self.cells):
            p = 'p' if c.is_pond else ' '
            print('{0:02d}'.format(i) + p + c.char * c.y)

def run():
    area = Area()
    for l in input():
        area.put(l)
    area.adjust_height()
    area.is_pond()
    area.collect_pond_idx()
    area.get_area_list()
    print(sum(area.area_list))
    if area.area_list:
        print(len(area.area_list), ' '.join([str(a) for a in area.area_list]))
    else:
        print(0)

if __name__ == '__main__':
    run()

Submission status: Wrong Answer

Solution code:
class Cell(object):
    def __init__(self, direction, x, y):
        self.direction = direction
        self.x = x
        self.y = y
        self.is_pond = False

    def get_area(self, pond_height):
        h = pond_height - self.y + 1
        if self.direction == 'f':
            return h
        else:
            return h - 0.5

    @property
    def char(self):
        if self.direction == 'd':
            return '/'
        elif self.direction == 'u':
            return '\\'
        elif self.direction == 'f':
            return '|'

class Area(object):
    def __init__(self):
        self.cells = []
        self.x = 0
        self.height = 0
        self.pond_idx = []
        self.area_list = []

    @property
    def top(self):
        return max([c.y for c in self.cells])
    
    def put(self, c):
        if c == '\\':
            self.height -= 1
            self.cells.append(Cell('d', self.x, self.height))
        elif c == '/':
            self.cells.append(Cell('u', self.x, self.height))
            self.height += 1
        elif c == '_':
            self.cells.append(Cell('f', self.x, self.height))
        self.x += 1

    def adjust_height(self):
        lowest = min([c.y for c in self.cells])
        for c in self.cells:
            c.y -= lowest
    
    def is_pond(self):
        for i_start in range(len(self.cells)):
            c_start = self.cells[i_start]
            if c_start.direction == 'u':
                continue
            if c_start.direction == 'f' and 0 == i_start:
                continue
            if c_start.direction == 'f' and not self.cells[i_start - 1].is_pond:
                continue

            for i_end in range(i_start+1, len(self.cells)):
                c_end = self.cells[i_end]
                if c_start.y == c_end.y:
                    if c_end.direction == 'u':
                        c_start.is_pond = True
                        c_end.is_pond = True
                    elif c_end.direction == 'f':
                        continue
                    break

    def collect_pond_idx(self):
        start = None
        start_c = None
        for c in self.cells:
            if c.is_pond and start == None:
                start = c.x
                start_c = c
            elif not c.is_pond and start != None:
                self.pond_idx.append((start, c.x - 1))
                start = None
                start_c = None
            elif c.is_pond and c.direction == 'd' and c.y == start_c.y:
                self.pond_idx.append((start, c.x - 1))
                start = c.x
                start_c = c
        if start != None:
            self.pond_idx.append((start, self.cells[-1].x))

    def get_area(self, idx):
        area = 0
        pond_height = self.cells[idx[0]].y
        for i in range(idx[0], idx[1]+1):
            area += self.cells[i].get_area(pond_height)
        return int(area)

    def get_area_list(self):
        for pond in self.pond_idx:
            self.area_list.append(self.get_area(pond))

    def show(self):
        for i, c in enumerate(self.cells):
            p = 'p' if c.is_pond else ' '
            print('{0:02d}'.format(i) + p + c.char * (c.y+1))

def run():
    area = Area()
    for l in input():
        area.put(l)
    area.adjust_height()
    area.is_pond()
    area.collect_pond_idx()
    area.get_area_list()
    # area.show()
    print(sum(area.area_list))
    if area.area_list:
        print(len(area.area_list), ' '.join([str(a) for a in area.area_list]))
    else:
        print(0)

if __name__ == '__main__':
    run()

Submission status: Wrong Answer

Solution code:
class Cell(object):
    def __init__(self, direction, x, y):
        self.direction = direction
        self.x = x
        self.y = y
        self.is_pond = False

    def get_area(self, pond_height):
        h = pond_height - self.y + 1
        if self.direction == 'f':
            return h - 1
        else:
            return h - 0.5

    @property
    def char(self):
        if self.direction == 'd':
            return '/'
        elif self.direction == 'u':
            return '\\'
        elif self.direction == 'f':
            return '|'

class Area(object):
    def __init__(self):
        self.cells = []
        self.before_c = ''
        self.x = 0
        self.height = 0
        self.pond_idx = []
        self.area_list = []
        
    @property
    def top(self):
        return max([c.y for c in self.cells])
    
    def put(self, c):
        if c == '\\':
            if self.before_c == '\\':
                self.height -= 1
            self.cells.append(Cell('d', self.x, self.height))
        elif c == '/':
            if self.before_c == '/' or self.before_c == '_':
                self.height += 1
            self.cells.append(Cell('u', self.x, self.height))
        elif c == '_':
            if self.before_c == '\\':
                self.height -= 1
            self.cells.append(Cell('f', self.x, self.height))
        self.x += 1
        self.before_c = c

    def adjust_height(self):
        lowest = min([c.y for c in self.cells])
        for c in self.cells:
            c.y -= lowest
    
    def is_pond(self):
        for i_start in range(len(self.cells)):
            c_start = self.cells[i_start]
            if c_start.direction == 'u':
                continue
            if c_start.direction == 'f' and 0 == i_start:
                continue
            if c_start.direction == 'f' and not self.cells[i_start - 1].is_pond:
                continue

            for i_end in range(i_start+1, len(self.cells)):
                c_end = self.cells[i_end]
                if c_start.y <= c_end.y:
                    if c_end.direction == 'u':
                        c_start.is_pond = True
                        c_end.is_pond = True
                    elif c_end.direction == 'd':
                        continue
                    elif c_end.direction == 'f':
                        continue
                    break

    def collect_pond_idx(self):
        start = None
        start_c = None
        for c in self.cells:
            if c.is_pond and start == None:
                start = c.x
                start_c = c
            elif not c.is_pond and start != None:
                self.pond_idx.append((start, c.x - 1))
                start = None
                start_c = None
            elif c.is_pond and c.direction == 'd' and c.y == start_c.y:
                self.pond_idx.append((start, c.x - 1))
                start = c.x
                start_c = c
        if start != None:
            self.pond_idx.append((start, self.cells[-1].x))

    def get_area(self, idx):
        area = 0
        pond_height = self.cells[idx[0]].y
        for i in range(idx[0], idx[1]+1):
            area += self.cells[i].get_area(pond_height)
        return int(area)

    def get_area_list(self):
        for pond in self.pond_idx:
            self.area_list.append(self.get_area(pond))

    def show(self):
        for i, c in enumerate(self.cells):
            p = 'p' if c.is_pond else ' '
            print('{0:02d}'.format(i) + p + c.char * (c.y+1))

def run():
    area = Area()
    for l in input():
        area.put(l)
    area.adjust_height()
    area.is_pond()
    area.collect_pond_idx()
    area.get_area_list()
    area.show()
    print(sum(area.area_list))
    if area.area_list:
        print(len(area.area_list), ' '.join([str(a) for a in area.area_list]))
    else:
        print(0)

if __name__ == '__main__':
    run()

Submission status: Wrong Answer

Solution code:
class Cell(object):
    def __init__(self, direction, x, y):
        self.direction = direction
        self.x = x
        self.y = y
        self.is_pond = False

    def get_area(self, pond_height):
        h = pond_height - self.y + 1
        if self.direction == 'f':
            return h - 1
        else:
            return h - 0.5

    @property
    def char(self):
        if self.direction == 'd':
            return '/'
        elif self.direction == 'u':
            return '\\'
        elif self.direction == 'f':
            return '|'

class Area(object):
    def __init__(self):
        self.cells = []
        self.before_c = ''
        self.x = 0
        self.height = 0
        self.pond_idx = []
        self.area_list = []
        
    @property
    def top(self):
        return max([c.y for c in self.cells])
    
    def put(self, c):
        if c == '\\':
            if self.before_c == '\\':
                self.height -= 1
            self.cells.append(Cell('d', self.x, self.height))
        elif c == '/':
            if self.before_c == '/' or self.before_c == '_':
                self.height += 1
            self.cells.append(Cell('u', self.x, self.height))
        elif c == '_':
            if self.before_c == '\\':
                self.height -= 1
            self.cells.append(Cell('f', self.x, self.height))
        self.x += 1
        self.before_c = c

    def adjust_height(self):
        lowest = min([c.y for c in self.cells])
        for c in self.cells:
            c.y -= lowest
    
    def is_pond(self):
        for i_start in range(len(self.cells)):
            c_start = self.cells[i_start]
            if c_start.direction == 'u':
                continue
            if c_start.direction == 'f' and 0 == i_start:
                continue
            if c_start.direction == 'f' and not self.cells[i_start - 1].is_pond:
                continue

            for i_end in range(i_start+1, len(self.cells)):
                c_end = self.cells[i_end]
                if c_start.y <= c_end.y:
                    if c_end.direction == 'u':
                        c_start.is_pond = True
                        c_end.is_pond = True
                    elif c_end.direction == 'd':
                        continue
                    elif c_end.direction == 'f':
                        continue
                    break

    def collect_pond_idx(self):
        start = None
        start_c = None
        for c in self.cells:
            if c.is_pond and start == None:
                start = c.x
                start_c = c
            elif not c.is_pond and start != None:
                self.pond_idx.append((start, c.x - 1))
                start = None
                start_c = None
            elif c.is_pond and c.direction == 'd' and c.y == start_c.y:
                self.pond_idx.append((start, c.x - 1))
                start = c.x
                start_c = c
        if start != None:
            self.pond_idx.append((start, self.cells[-1].x))

    def get_area(self, idx):
        area = 0
        pond_height = self.cells[idx[0]].y
        for i in range(idx[0], idx[1]+1):
            area += self.cells[i].get_area(pond_height)
        return int(area)

    def get_area_list(self):
        for pond in self.pond_idx:
            self.area_list.append(self.get_area(pond))

    def show(self):
        for i, c in enumerate(self.cells):
            p = 'p' if c.is_pond else ' '
            print('{0:02d}'.format(i) + p + c.char * (c.y+1))

def run():
    area = Area()
    for l in input():
        area.put(l)
    area.adjust_height()
    area.is_pond()
    area.collect_pond_idx()
    area.get_area_list()
    # area.show()
    print(sum(area.area_list))
    if area.area_list:
        print(len(area.area_list), ' '.join([str(a) for a in area.area_list]))
    else:
        print(0)

if __name__ == '__main__':
    run()

Submission status: Wrong Answer

Solution code:
class Cell(object):
    def __init__(self, direction, x, y):
        self.direction = direction
        self.x = x
        self.y = y
        self.is_pond = False
        self.start_of_pond = False
        self.end_of_pond = False

    def get_area(self, pond_height):
        h = pond_height - self.y + 1
        if self.direction == 'f':
            return h - 1
        else:
            return h - 0.5

    @property
    def char(self):
        if self.direction == 'd':
            return '/'
        elif self.direction == 'u':
            return '\\'
        elif self.direction == 'f':
            return '|'

class Area(object):
    def __init__(self):
        self.cells = []
        self.before_c = ''
        self.x = 0
        self.height = 0
        self.pond_idx = []
        self.area_list = []
        
    @property
    def top(self):
        return max([c.y for c in self.cells])
    
    def put(self, c):
        if c == '\\':
            if self.before_c == '\\':
                self.height -= 1
            self.cells.append(Cell('d', self.x, self.height))
        elif c == '/':
            if self.before_c == '/' or self.before_c == '_':
                self.height += 1
            self.cells.append(Cell('u', self.x, self.height))
        elif c == '_':
            if self.before_c == '\\':
                self.height -= 1
            self.cells.append(Cell('f', self.x, self.height))
        self.x += 1
        self.before_c = c

    def adjust_height(self):
        lowest = min([c.y for c in self.cells])
        for c in self.cells:
            c.y -= lowest
    
    def is_pond(self):
        start_of_pond = None
        for i_start in range(len(self.cells)):
            c_start = self.cells[i_start]
            if c_start.direction == 'u':
                if start_of_pond:
                    if start_of_pond.y == c_start.y:
                        start_of_pond = None
                        c_start.end_of_pond = True
                continue
            if c_start.direction == 'f' and 0 == i_start:
                continue
            if c_start.direction == 'f' and not self.cells[i_start - 1].is_pond:
                continue

            for i_end in range(i_start+1, len(self.cells)):
                c_end = self.cells[i_end]
                if c_start.y <= c_end.y:
                    if c_end.direction == 'u':
                        c_start.is_pond = True
                        c_end.is_pond = True
                    elif c_end.direction == 'd':
                        continue
                    elif c_end.direction == 'f':
                        continue
                    break
            if not start_of_pond and c_start.is_pond:
                c_start.start_of_pond = True
                start_of_pond = c_start

    def get_pond_area(self):
        area = []
        _area = 0
        _height = 0
        for c in self.cells:
            if c.start_of_pond:
                _height = c.y
            if c.is_pond:
                _area += c.get_area(_height)
            if c.end_of_pond:
                area.append(int(_area))
                _area = 0
                _height = 0
        return area

    def collect_pond_idx(self):
        start = None
        start_c = None
        for c in self.cells:
            if c.is_pond and start == None:
                start = c.x
                start_c = c
            elif not c.is_pond and start != None:
                self.pond_idx.append((start, c.x - 1))
                start = None
                start_c = None
            elif c.is_pond and c.direction == 'd' and c.y == start_c.y:
                self.pond_idx.append((start, c.x - 1))
                start = c.x
                start_c = c
        if start != None:
            self.pond_idx.append((start, self.cells[-1].x))

    def get_area(self, idx):
        area = 0
        pond_height = self.cells[idx[0]].y
        for i in range(idx[0], idx[1]+1):
            area += self.cells[i].get_area(pond_height)
        return int(area)

    def get_area_list(self):
        for pond in self.pond_idx:
            self.area_list.append(self.get_area(pond))

    def show(self):
        for i, c in enumerate(self.cells):
            p = 'p' if c.is_pond else ' '
            e = 'E' if c.end_of_pond else ''
            s = 'S' if c.start_of_pond else ''
            print('{0:02d}'.format(i) + p + c.char * (c.y+1) + e + s)

def run():
    area = Area()
    for l in input():
        area.put(l)
    area.adjust_height()
    area.is_pond()
    # area.show()
    pond_area = area.get_pond_area()
    print(sum(pond_area))
    if pond_area:
        print(len(pond_area), ' '.join([str(a) for a in pond_area]))
    else:
        print(0)

if __name__ == '__main__':
    run()

Submission status: Wrong Answer

Solution code:
class Cell(object):
    def __init__(self, direction, x, y):
        self.direction = direction
        self.x = x
        self.y = y
        self.is_pond = False
        self.never_pond = False
        self.start_of_pond = False
        self.end_of_pond = False

    def get_area(self, pond_height):
        h = pond_height - self.y + 1
        if self.direction == 'f':
            return h - 1
        else:
            return h - 0.5

    @property
    def char(self):
        if self.direction == 'd':
            return '/'
        elif self.direction == 'u':
            return '\\'
        elif self.direction == 'f':
            return '|'

class Area(object):
    def __init__(self):
        self.cells = {}
        self.before_c = ''
        self.x = -1
        self.height = 0
        self.lowest = 0
        self.highest = 0
        self.higher = 0
        self.pond_area = []
        self.reverse = False

    def put(self, c):
        self.x += 1
        if c == '\\':
            if self.before_c == '\\':
                self.height -= 1
            self.cells[self.x] = Cell('d', self.x, self.height)
        elif c == '/':
            if self.before_c == '/' or self.before_c == '_':
                self.height += 1
            self.cells[self.x] = Cell('u', self.x, self.height)
        elif c == '_':
            if self.before_c == '\\':
                self.height -= 1
            self.cells[self.x] = Cell('f', self.x, self.height)
        
        if self.cells[self.x].y < self.lowest:
            self.lowest = self.cells[self.x].y
        if self.highest < self.cells[self.x].y:
            self.highest = self.cells[self.x].y
        self.before_c = c

    def adjust_height(self):
        self.highest -= self.lowest
        for i, c in self.cells.items():
            c.y -= self.lowest
            if self.higher < c.y and c.y < self.highest:
                if c.direction != 'd':
                    self.higher = c.y
    
    def is_pond(self):
        start_of_pond = None
        ponds = []
        for i_start in range(len(self.cells)):
            c_start = self.cells[i_start]
            if c_start.is_pond:
                ponds.append(c_start)
            if c_start.direction == 'u':
                if start_of_pond:
                    if start_of_pond.y == c_start.y:
                        start_of_pond = None
                        c_start.end_of_pond = True
                        self.pond_area.append(self.calc_area(ponds))
                        ponds = []
                continue
            if c_start.direction == 'f' and 0 == i_start:
                continue
            if c_start.direction == 'f' and not self.cells[i_start - 1].is_pond:
                continue
            if c_start.direction == 'f' and self.cells[i_start - 1].end_of_pond:
                continue
            if c_start.direction == 'd' or c_start.direction == 'f':
                if c_start.is_pond:
                    continue
            if self.higher < c_start.y:
                continue
            
            for i_end in range(i_start+1, len(self.cells)):
                c_end = self.cells[i_end]
                if c_start.y <= c_end.y:
                    if c_end.direction == 'u':
                        for i in range(c_start.x, c_end.x+1):
                            self.cells[i].is_pond = True
                    elif c_end.direction == 'd':
                        continue
                    elif c_end.direction == 'f':
                        continue
                    break

            if not start_of_pond and c_start.is_pond:
                c_start.start_of_pond = True
                start_of_pond = c_start
            if c_start.is_pond and not c_start in ponds:
                ponds.append(c_start)

    def calc_area(self, ponds):
        area = 0
        height = ponds[0].y
        for c in ponds:
            area += c.get_area(height)
        return int(area)

    def show(self):
        for i, c in self.cells.items():
            p = 'p' if c.is_pond else ' '
            e = 'E' if c.end_of_pond else ''
            s = 'S' if c.start_of_pond else ''
            print('{0:02d}'.format(i) + p + c.char * (c.y+1) + e + s)

def run():
    area = Area()
    for l in input():
        area.put(l)
    area.adjust_height()
    area.is_pond()
    # area.show()
    print(sum(area.pond_area))
    if area.pond_area:
        print(len(area.pond_area), ' '.join([str(a) for a in area.pond_area]))
    else:
        print(0)

if __name__ == '__main__':
    run()