Carnival Game

View as PDF

Submit solution


Points: 7
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

Annually, from July 27 to August 11, the local carnival opens for business! The main attraction of the carnival this year is a peculiar looking game that you and your friends have decided to try out. The game has N-1 layers of wooden boards arranged in a pyramid-like structure (layer i has i boards), with each board either pointing downwards and to the left or downwards and to the right.

You are told that a ball will be dropped on the topmost board.

There are N buckets at the bottom of the structure. Bucket i contains t_i dollars worth of coins (because carnivals are a scam, t_i can be negative). You take home all the money in the bucket that the ball eventually lands in.

A ball that lands on the j^{th} board on the i^{th} layer, b_{i, j}, will fall to the j^{th} board on the i+1^{th} layer if b_{i, j} is facing downwards and to the left, or to the j+1^{th} board on the i+1^{th} layer if b_{i, j} is facing downwards and to the right. On the N-1^{th} layer, the same rules are applied, however, the ball falls into a bucket on row N instead of another board.

Before you drop the ball on the topmost board, you are allowed to flip any board to face downwards and to the left if it was originally facing downwards and to the right, and vice versa. Doing so will cost you 1 dollar. Not wanting to hold up the line, quickly calculate the maximum profit P (even if it has to be negative) you can make, and what boards to flip to achieve P!

Constraints

2 \le N \le 2 \times 10^3

-10^9 \le t_i \le 10^9

Input Specification

The first line contains N, the number of levels in the carnival game and the number of buckets.

The next N-1 lines represent the layers of the pyramid, with the j^{th} character on the i^{th} line representing the orientation of the j^{th} board on the i^{th} layer, L for facing downwards and to the left or R for facing downwards and to the right.

The final line contains N space separated integers t_i.

Output Specification

First, output an integer P, the maximum profit you can make.

Next, output an integer K \left(0 \le K \le \frac{N(N-1)}{2}\right), the number of flips made to obtain P.

Lastly, output K lines each containing two space-separated integers i and j, representing that the j^{th} board on the i^{th} layer was flipped.

Any K steps that achieve P will be accepted.

Sample Input 1

3
L
RR
-1 0 1

Sample Output 1

0
1
1 1

Explanation for Sample 1

Flipping the first board on the first layer gets us to:

R
RR

We can see that the ball reaches the third bucket in row N and we get a profit of 1-1 = 0 dollars. Alternatively, we could flip no boards and have the ball land in the second bucket and make the same profit of 0-0 = 0 dollars.

Sample Input 2

6
L
LL
LLL
LLLL
LLLLL
-727 69 3735 -8210 420 3737

Sample Output 2

3733
2
5 2
4 1

Comments

There are no comments at the moment.