Friendship Fee
InterviewTime limit2sMemory limit512 MB
Find the cheapest way to become friends with everyone by paying each student a friend fee and using the friend-of-a-friend rule; output the minimum cost or "Oh no" when it exceeds k.
- Level
Medium5 of 10
- Topics
- Union-find, Graph, Greedy
- Solved
- No attempts yet
Problem
Lee Junseok, admitted in the class of 2019, has entered a school with students. Wanting to mark his admission, Junseok wants to become friends with every student. But Junseok has spent his whole life talking only to computers, so he does not know how to speak with people. Even for Junseok there is hope. It is the friendship fee!
If you give student an amount of money, that student will be your friend for one month. Junseok has a total of won, and he decided to use that money to make friends. As he actually went about making friends, he began to think that he would run out of money. So Junseok decided to use the rule "a friend of a friend is a friend".
Now Junseok does not have to pay every friend!
Using this logic, find the way to become friends with everyone at the lowest cost.
Input
The first line gives the number of students (), the number of friend relations (), and the money Junseok has, ().
The second line gives the friendship fee that each of the students wants. (, )
The next lines give numbers . This means student and student are friends with each other. A student may be friends with themselves, and the same friend relation may be given multiple times.
Output
If Junseok can make every student his friend, print the minimum cost of making them friends. If he cannot make friends with all of them, print “Oh no” (without the quotation marks).