atcoder#ABC286E. [ABC286E] Souvenir

[ABC286E] Souvenir

Score : 500500 points

Problem Statement

There are NN cities. There are also one-way direct flights that connect different cities. The availability of direct flights is represented by NN strings S1,S2,,SNS_1,S_2,\ldots,S_N of length NN each. If the jj-th character of SiS_i is Y, there is a direct flight from city ii to city jj; if it is N, there is not. Each city sells a souvenir; city ii sells a souvenir of value AiA_i.

Consider the following problem:

Takahashi is currently at city SS and wants to get to city TT (that is different from city SS) using some direct flights. Every time he visits a city (including SS and TT), he buys a souvenir there. If there are multiple routes from city SS to city TT, Takahashi decides the route as follows:

  • He tries to minimize the number of direct flights in the route from city SS to city TT.
  • Then he tries to maximize the total value of the souvenirs he buys.

Determine if he can travel from city SS to city TT using the direct flights. If he can, find the "number of direct flights" and "total value of souvenirs" in the route that satisfies the conditions above.

You are given QQ pairs (Ui,Vi)(U_i,V_i) of distinct cities. For each 1iQ1\leq i\leq Q, print the answer to the problem above when S=UiS=U_i and T=ViT=V_i.

Constraints

  • 2N3002 \leq N \leq 300
  • 1Ai1091\leq A_i\leq 10^9
  • SiS_i is a string of length NN consisting of Y and N.
  • The ii-th character of SiS_i is N.
  • 1QN(N1)1\leq Q\leq N(N-1)
  • 1Ui,ViN1\leq U_i,V_i\leq N
  • UiViU_i\neq V_i
  • If iji \neq j, then (Ui,Vi)(Uj,VJ)(U_i,V_i)\neq (U_j,V_J).
  • N,Ai,Q,UiN,A_i,Q,U_i, and ViV_i are all integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

S1S_1

S2S_2

\vdots

SNS_N

QQ

U1U_1 V1V_1

U2U_2 V2V_2

\vdots

UQU_Q VQV_Q

Output

Print QQ lines. The ii-th (1iQ)(1\leq i\leq Q) line should contain Impossible if he cannot travel from city UiU_i to city ViV_i; if he can, the line should contain "the number of direct flights" and "the total value of souvenirs" in the route chosen as described above, in this order, separated by a space.

5
30 50 70 20 60
NYYNN
NNYNN
NNNYY
YNNNN
YNNNN
3
1 3
3 1
4 5
1 100
2 160
3 180

For (S,T)=(U1,V1)=(1,3)(S,T)=(U_1,V_1)=(1,3), there is a direct flight from city 11 to city 33, so the minimum possible number of direct flights is 11, which is achieved when he uses that direct flight. In this case, the total value of the souvenirs is A1+A3=30+70=100A_1+A_3=30+70=100.

For (S,T)=(U2,V2)=(3,1)(S,T)=(U_2,V_2)=(3,1), the minimum possible number of direct flights is 22. The following two routes achieve the minimum: cities 3413\to 4\to 1, and cities 3513\to 5\to 1. Since the total value of souvenirs in the two routes are 70+20+30=12070+20+30=120 and 70+60+30=16070+60+30=160, respectively, so he chooses the latter route, and the total value of the souvenirs is 160160.

For (S,T)=(U3,V3)=(4,5)(S,T)=(U_3,V_3)=(4,5), the number of direct flights is minimum when he travels cities 41354\to 1\to 3\to 5, where the total value of the souvenirs is 20+30+70+60=18020+30+70+60=180.

2
100 100
NN
NN
1
1 2
Impossible

There may be no direct flight at all.