Wise Business
Practice
2.5 (2 votes)
Topological sort
Algorithms
Graphs
Problem
54% Success 548 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You just graduated from the business school of MagicLand and are about to start your own business. You will be buying some magical potions from the great wizard of MagicLand and selling them at your hometown HackerLand.
The great wizard sells \(N\) different types of potions where the \(i'th\) \((1 <= i <= N)\) potion is of the price \(A[i]\). You can convert a potion of type \(X[i]\) to a potion of type \(Y[i]\) \((1 <= i <= M)\).
The conversion of potion from one type to another is unidirectional.
What is the maximum amount of profit you can make from buying and selling a single potion?

Input Format:

  • The first line contains \(T\), denoting the number of test cases.
  • The first line of each test case has two space-separated integers, \(N\) & \(M\), representing the number of different types of potions and the number of different kinds of conversions you can make, respectively.
  • The following line contains \(N\) space-separated integers representing the array \(A\), where \(A[i]\) is the price of a potion of the \(i'th\) type.
  • The \(i'th\) of the next \(M\) lines contains two space-separated integers, \(X[i]\) & \(Y[i]\), denoting that you can convert a potion of type \(X[i]\) to a potion of type \(Y[i]\).


Output Format:
For each test case, print a single integer representing the maximum possible profit.

Constraints:
\(1 <= T <= 5\)
\(2 <= N <= 10^5\)
\(1 <= M <= min(N*(N-1)/2, 10^5)\)
\(1 <= A[i] <= 10^5\)
\((X[i], Y[i]) != (X[j], Y[j]), i != j\)
There is no way to convert any potion to a potion of the same type with at least one conversion.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
65 votes
Tags:
AlgorithmsApprovedData StructuresDepth First SearchGraphsMediumOpenSorting
Points:30
12 votes
Tags:
AlgorithmsC++GraphsSorting
Points:30
1 votes
Tags:
GraphTreeAlgorithmsGraphsTopological Sort