Under construction

Still being built. Things may change

Blowing Fuses

Interview

Time limit1sMemory limit128 MB

Summary
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 nn, mm, and cc, where nn is the number of devices (n<20n < 20), mm is the number of operations performed on these devices, and cc is the capacity of the fuse (in amperes). The following nn lines each contain one positive integer cic_i, the consumption (in amperes) of the ii-th device.

This is followed by mm lines, each containing one integer between 11 and nn 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 n=m=c=0n = m = c = 0. 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 cic_i of the turned-on devices exceeds the capacity cc 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.

Examples1

  1. Example 1

    Input
    2 2 10
    5
    7
    1
    2
    3 6 10
    2
    5
    7
    2
    1
    2
    3
    1
    3
    0 0 0
    
    Expected output
    Sequence 1
    Fuse was blown.
    
    Sequence 2
    Fuse was not blown.
    Maximal power consumption was 9 amperes.