#FW0000077. Quà lưu niệm (Souvenir)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Quà lưu niệm (Souvenir)
Souvenir
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Each city offers a souvenir of value . Directed flights are described by a Y/N matrix. For each query , minimize the number of flights, and among those optimal itineraries maximize total souvenir value including both endpoints. Print Impossible if unreachable.
Input
First , then souvenir values, then Y/N strings of length , then queries using 1-based indices.
Output
For each query print Impossible or number_of_flights souvenir_value.
Subtasks
-
Subtask 1 (40 points): . ; , ma trận chỉ có
Y/N. -
Subtask 2 (60 points): . ; , ma trận chỉ có
Y/N.
Examples
Input
3
5 10 20
NYN
NNY
NNN
3
1 3
3 1
1 1
Output
2 35
Impossible
0 5
Explanation
To reach 3 from 1 takes two flights via 2, collecting 5+10+20=35. City 3 cannot reach city 1; staying in city 1 uses zero flights and collects value 5.