A country is represented as a matrix of the size N×M and each cell is considered to be a city.
There are Q missions and you are required to accomplish K missions to receive the tourist certification. In each mission, you are given a source and destination and you need to travel from source to destination.
Each city contains a development cost that depicts the state of development of the city. The development cost of a mission is considered to be the maximum development cost in the path between source and destination.The overall development cost for the certification is the maximum development cost of all the accomplished missions. Your task is to minimize the overall development cost to achieve the certificate.
Note:
Input format
Note: It is guaranteed that the source city and destination city do not coincide for a mission.
Output format
Print the minimum value of the overall development cost in accomplishing exactly K missions.
Input Constraints
1≤N,M≤500
1≤K≤Q≤5×105
1≤Xsource,Xdestination≤N
1≤Ysource,Ydestination≤M
1≤Aij≤109
Here, 3 missions have been given and we need to complete 2 of them. We can accomplish missions 1 and 3 to minimize the overall risk.
For mission 1: We can travel like this :- (1,1) -> (1,2) and riskiness of this mission will be 3.
For mission 3: We can travel like this :- (3,1) -> (3,2) -> (3,3)-> (2,3) and riskiness of this mission will be 6.
Hence, minimum possible overall riskiness for the certification will be max(3,6) that is 6.