Jimmy has planned a trip to the University of Waterloo for their March Break Open House. Now, he needs to convince his friends to go with him.
Jimmy has friends numbered
to
, none of whom wants to go on the trip with him right now. However, Jimmy can bribe the
th friend to go by buying them a gift worth
dollars. In addition, there are
uni-directional relationships between his friends, numbered
to
. The
th relationship allows him to deduce that if friend
decides to go on the trip (whether bribed or not), then friend
will decide to go as well of their own volition.
Jimmy would like to convince all of his friends to go to the University of Waterloo with him. Please find the minimum amount of money that he must spend in total to achieve this.
Input Specification
The first line will contain two space-separated integers, and
.
The next line will contain space-separated integers,
.
The next lines will each contain two space-separated integers,
and
.
Output Specification
Output one integer, the minimum total amount of money that Jimmy must spend.
Constraints
In all subtasks,
Subtask 1 [30%]
There will be no cyclic relationships. In other words, for all friends and
, if
going directly or indirectly leads to
going, then
going won't directly or indirectly lead to
going.
Subtask 2 [70%]
No further constraints.
Sample Input
5 4
4 6 5 10 3
1 2
2 3
3 1
4 5
Sample Output
14
Explanation for Sample Output
Jimmy can convince everyone to go by bribing friends and
.
Comments