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 N1 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 ti dollars worth of coins (because carnivals are a scam, ti can be negative). You take home all the money in the bucket that the ball eventually lands in.

A ball that lands on the jth board on the ith layer, bi,j, will fall to the jth board on the i+1th layer if bi,j is facing downwards and to the left, or to the j+1th board on the i+1th layer if bi,j is facing downwards and to the right. On the N1th 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

2N2×103

109ti109

Input Specification

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

The next N1 lines represent the layers of the pyramid, with the jth character on the ith line representing the orientation of the jth board on the ith 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 ti.

Output Specification

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

Next, output an integer K (0KN(N1)2), the number of flips made to obtain P.

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

Any K steps that achieve P will be accepted.

Sample Input 1

Copy
3
L
RR
-1 0 1

Sample Output 1

Copy
0
1
1 1

Explanation for Sample 1

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

Copy
R
RR

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

Sample Input 2

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

Sample Output 2

Copy
3733
2
5 2
4 1

Comments

There are no comments at the moment.