IOI '98 - Setúbal, Portugal
Polygon is a game for one player that starts on a polygon with
vertices, like the one in Figure 1, where . Each
vertex is labelled with an integer and each edge is labelled with either
the symbol +
(addition) or the symbol *
(product). The edges are
numbered from to .
Figure 1. Graphical representation of a polygon
On the first move, one of the edges is removed.
Subsequent moves involve the following steps:
- pick an edge and the two vertices and that are linked by ; and
- replace them by a new vertex, labelled with the result of performing the operation indicated in on the labels of and .
The game ends when there are no more edges, and its score is the label of the single vertex remaining.
Sample Game
Consider the polygon of Figure 1. The player started by removing edge 3. The effects are depicted in Figure 2.
Figure 2. Removing edge 3
After that, the player picked edge 1,
Figure 3. Picking edge 1
then edge 4,
Figure 4. Picking edge 4
and, finally, edge 2. The score is 0.
Figure 5. Picking edge 2
Write a program that, given a polygon, computes the highest possible score and lists all the edges that, if removed on the first move, can lead to a game with that score.
Input Specification
The input describes a polygon with vertices. It contains two lines.
- On the first line is the number .
- The second line contains the labels of edges , interleaved
with the vertices' labels (first that of the vertex between edges
and , then that of the vertex between edges and , and so on,
until that of the vertex between edges and ), all separated by
one space. An edge label is either the letter (representing
+
) or the letter (representing*
).
Output Specification
The first line of output should contain the highest score one can get
for the input polygon.
On the second line should be the list of all edges that, if removed on
the first move, can lead to a game with that score.
Edges must be written in increasing order, separated by one space.
Note: For any sequence of moves, vertex labels are in the range .
Sample Input
4
t -7 t 4 x 2 x 5
This is the input for the polygon of Figure 1. The second line starts with the label of edge 1.
Sample Output
33
1 2
This must be the output for the polygon of Figure 1.
Comments