Blowing Fuses
InterviewTime limit1sMemory limit128 MB
Simulate toggling devices on and off, tracking the running total power and whether it ever exceeds the fuse capacity.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation, Array, Brute force
- Solved
- No attempts yet
Problem
Maybe you are familiar with the following situation. You have plugged in a lot of electrical devices — toasters, refrigerators, microwave ovens, computers, stereos, and so on — and have them all running. But the moment you turn on the TV, the fuse blows, because the power drawn by all the machines together is greater than the capacity of the fuse. Of course this is a great safety feature: it keeps houses from burning down too often due to fires ignited by overheating wires. But it is also annoying to walk down to the basement (or some other inconvenient place) to replace the fuse or switch it back on.
What one would like to have is a program that checks, before turning on an electrical device, whether the combined power drawn by all running devices would exceed the fuse's capacity (so it blows), or whether it is safe to turn the device on.
Input
The input consists of several test cases. Each test case describes a set of electrical devices and gives a sequence of turn-on/turn-off operations for these devices.
The first line of each test case contains three integers , , and , where is the number of devices (), is the number of operations performed on these devices, and is the capacity of the fuse (in amperes). The following lines each contain one positive integer , the consumption (in amperes) of the -th device.
This is followed by lines, each containing one integer between and inclusive. They describe a sequence of turn-on/turn-off operations performed on the devices. For each number, the state of that particular device is toggled: if it is currently running, it is turned off, and if it is currently off, it is switched on. At the beginning all devices are turned off.
The input is terminated by a test case starting with . This test case must not be processed.
Output
For each test case, first output the number of the test case. Then output whether the fuse was blown during the sequence of operations. The fuse is blown if the sum of the power consumptions of the turned-on devices exceeds the capacity of the fuse at some point.
If the fuse is not blown, output the maximum power consumption by the turned-on devices that occurred during the sequence.
Output a blank line after each test case.