atcoder#ABC286E. [ABC286E] Souvenir
[ABC286E] Souvenir
Score : points
Problem Statement
There are cities. There are also one-way direct flights that connect different cities.
The availability of direct flights is represented by strings of length each.
If the -th character of is Y
, there is a direct flight from city to city ;
if it is N
, there is not.
Each city sells a souvenir; city sells a souvenir of value .
Consider the following problem:
Takahashi is currently at city and wants to get to city (that is different from city ) using some direct flights. Every time he visits a city (including and ), he buys a souvenir there. If there are multiple routes from city to city , Takahashi decides the route as follows:
- He tries to minimize the number of direct flights in the route from city to city .
- Then he tries to maximize the total value of the souvenirs he buys.
Determine if he can travel from city to city 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 pairs of distinct cities. For each , print the answer to the problem above when and .
Constraints
- is a string of length consisting of
Y
andN
. - The -th character of is
N
. - If , then .
- , and are all integers.
Input
The input is given from Standard Input in the following format:
Output
Print lines.
The -th line should contain
Impossible
if he cannot travel from city to city ;
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 , there is a direct flight from city to city , so the minimum possible number of direct flights is , which is achieved when he uses that direct flight. In this case, the total value of the souvenirs is .
For , the minimum possible number of direct flights is . The following two routes achieve the minimum: cities , and cities . Since the total value of souvenirs in the two routes are and , respectively, so he chooses the latter route, and the total value of the souvenirs is .
For , the number of direct flights is minimum when he travels cities , where the total value of the souvenirs is .
2
100 100
NN
NN
1
1 2
Impossible
There may be no direct flight at all.