# Codingame Solution: Blowing Fuse

Original Problem

You are going to write a program to predict whether a specific usage pattern of electrical appliances will cause the main fuse to blow.

You have three pieces of data.
1. There are n appliances in a room, each of them has an electrical current consumption value.
2. A usage pattern: you will click the power buttons of a list of appliances in a sequence, totally m clicks. Each click on a button will toggle the power status - when the power is OFF, a click will turn it ON. The next click will turn it OFF.
3. The capacity of the main fuse c in amperes [A].

The fuse will be blown if the sum of the consumed current of turned-on devices at some point exceeds the capacity of the main fuse c.

At the beginning, all appliances are OFF.
Input
Line 1: Integers n m c, separated by a space
n is the number of devices, assume the devices have IDs from 1 to n
m is the number of button-clicking going to happen
c is the capacity of the main fuse in amperes [A]

Line 2: n integers, space separated, representing the electrical current consumption value of each appliance, listed from ID 1 to n

Line 3: m integers, space separated - a sequence of ID# you are going to click power buttons, that will toggle the device status in that exact sequence.
Output
If the fuse was blown during the operation sequence, output one line:
Fuse was blown.

If the fuse did not blow, find the maximal consumed power by turned-on devices that occurred during the sequence. Output two lines:
Fuse was not blown.
Maximal consumed current was ?? A.

Follow examples of test cases for the expected format.
Constraints
n and m are below 100
c is below 10000

## Solution

When we walk down the sequence of button presses, we need to know if the device is already on. When it is on, we need to subtract the power consumption from a running sum and when it is off, we have to add it. Either we remember the state of the appliance in a hash-table or we remember it with the sign of the costs. Since there are only positive power consumptions, this results in a very small implementation, since as an additional bonus: We don't need an if/else-statement.

After we have calculated the running sum, we remember the maximum of each iteration, which we need at the end to decide if the fuse blew up during the process. An implementation can then be stated analogously:

```const [n, m, c] = readline().split(' ').map(Number);

let sum = 0;
let max = 0;

seq.forEach(id => {
sum+= cst[id - 1];
cst[id - 1]*= -1;
max = Math.max(sum, max);
});

if (max > c) {
print('Fuse was blown.');
} else {
print('Fuse was not blown.');
print('Maximal consumed current was ' + max + ' A.');
}
```

« Back to problem overview