LAN Cable Connection
InterviewTime limit1.5sMemory limit1024 MB
Choose a nonempty set of switches with minimum total cost so their ports exactly fit M computers plus one uplink cable, or report -1 if impossible.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Math
- Solved
- No attempts yet
Problem
Eunkyu and the lab members recently moved to a new lab, and they hit a big snag: the new lab has no internet connection.
Luckily, they got one LAN cable with internet access from the neighboring lab. They can connect this cable to a switch in the lab to bring the internet in.
The lab has switches (network switches) and computers. Each switch has ports and costs to install. To supply internet from a switch, one cable carrying internet must be connected, and it can supply internet to other devices (switches or computers). Switches cannot form a cycle, because that creates a fire hazard.
Eunkyu likes neat wiring, so he wants no unused ports on the switches. He also cares about efficiency, so if several wiring plans are possible, he picks the one with the lowest cost that still supplies every computer. If such a wiring is possible, print the minimum cost of it. If not, print -1.
Input
The first line contains the number of switches ().
Each of the next lines contains two integers () and (), separated by a space. They are the number of ports and the installation cost of the -th switch.
The line after those lines contains the number of computers ().
Output
On the first line, print the minimum cost among the wiring plans that satisfy the conditions. If no such plan exists, print -1.