Under construction

Still being built. Things may change

Polygon

Time limit1sMemory limit128 MB

Summary
Remove one edge of a polygon, then repeatedly merge adjacent vertices by the intervening + or *, and report the maximum final value plus every edge whose removal reaches it.
Level

Hard8 of 10

Topics
Dynamic programming, Intervals, Implementation, Brute force
Solved
No attempts yet

Problem

Polygon is a one-player game played on a polygon with NN vertices, like the one in Figure 1 where N=4N = 4. Each vertex is labelled with an integer, and each edge is labelled with either + (addition) or * (multiplication). The edges are numbered from 11 to NN.

Figure 1

Figure 1. A polygon.

On the first move, one edge is removed. Every later move consists of these two steps:

  • pick an edge EE together with the two vertices V1V_1 and V2V_2 that it links, and
  • replace them with a single new vertex whose label is the result of applying EE's operation to the labels of V1V_1 and V2V_2.

The game ends when no edges remain, and the score is the label of the single vertex that is left.

For example, consider the polygon in Figure 1. Suppose the player first removes edge 3:

Figure 2

Figure 2. Removing edge 3.

then picks edge 1:

Figure 3

Figure 3. Picking edge 1.

then edge 4:

Figure 4

Figure 4. Picking edge 4.

and finally edge 2, ending with a score of 0:

Figure 5

Figure 5. Picking edge 2.

Write a program that, given a polygon, computes the highest score that can be reached and lists every edge that, when removed on the first move, allows a game achieving that highest score.

Input

The input describes a polygon with NN vertices and consists of two lines.

The first line contains the integer NN.

The second line lists the edge labels 1,…,N1, \ldots, N interleaved with the vertex labels, all separated by single spaces, in this order: the label of edge 1, then the label of the vertex between edges 1 and 2, then the label of edge 2, then the label of the vertex between edges 2 and 3, and so on, ending with the label of edge NN followed by the label of the vertex between edges NN and 1.

Each edge label is the letter t (meaning +) or the letter x (meaning *).

Output

Print the highest score obtainable for the given polygon on the first line.

On the second line, print every edge that, if removed on the first move, can lead to a game achieving that highest score. List the edges in increasing order, separated by single spaces.

Examples2

  1. Example 1

    Input
    4
    t -7 t 4 x 2 x 5
    
    Expected output
    33
    1 2
    
  2. Example 2

    Input
    3
    t 1 t 2 t 3
    
    Expected output
    6
    1 2 3