-4

0

Jack is a little businessman. He found out a way to earn money by buying electricity on days when it's cheap and selling it when it's much more expensive. He stores the electricity in a battery he made by himself.

### Challenge

You are given `N`

(if required), the number of days Jack knows the cost of electricity for, and `X`

, the amount of money Jack has available to invest in electricity, and the value(buy/sell value) of the electricity for `N`

days. Your job is to determine when Jack should buy and when he should sell electricity in order to earn as much money as possible and simply print the largest possible sum of money he could have afterwards.

The value of the electricity is always a positive integer but depending on the amount of money Jack has, the amount of electricity and money he has may be floating point numbers.

### Examples

(I/O need not be exactly as shown):

1.

```
4 10
4 10 5 20
```

Output: `100`

- because he buys electricity on the 1st day and the sells it on the 2nd and buys it on the 3rd and sells it on the 4th day.

2.

```
3 21
10 8 3
```

Output: `21`

- because it's better if he doesn't buy/sell any electricity.

3.

```
3 10
8 10 14
```

Output: `17.5`

- because he buys electricity on the 1st day, but he sells it on the 3rd day.

4.

```
5 10
4 2 10 5 20
```

Output: `200`

- much like example 1, but this time Jack waits for the price to drop to `2`

before making a purchase.

The program with the shortest amount of code wins! This is a code golf problem. Note:You can shorten the input, as long as it still makes sense.

Is this code golf? – Dennis – 2018-02-10T22:06:27.563

Yes, yes it is. – McLinux – 2018-02-10T22:22:32.930

Do we have to take the input exactly as described (N X on line 1, everything else on line 2) or can we use any reasonable format? – Dennis – 2018-02-10T22:45:44.487

You can use any reasonable format. – McLinux – 2018-02-10T22:49:17.793

1Please edit that clarification into your question. – Dennis – 2018-02-10T22:50:10.627

1By "earn" do you mean his final amount of money? (that is I think

`4 10`

and`4 10 5 20`

only earns 90, not 100) – Jonathan Allan – 2018-02-10T22:58:48.347What's the point of taking

`N`

as input if it's simply the length of the list of days? – Shaggy – 2018-02-10T23:05:33.467Also, how does John buy at 8, sell at 14 and end up earning 17.5? Surely he's earned

6there? – Shaggy – 2018-02-10T23:10:24.8901@Shaggy He spends his 10 buying at 8 storing 1.25 units which he sells at 14 for 17.5 (14*1.25). (He nets 7.5, but "earns" 17.5. See my comment above) – Jonathan Allan – 2018-02-10T23:14:50.610

2If this is not your own problem please do provide its source. – Jonathan Allan – 2018-02-10T23:19:55.930

@JonathanAllan The price is never less than 1 because they're always integers. – McLinux – 2018-02-10T23:47:45.223

Yes, sorry for the imprecision! – McLinux – 2018-02-10T23:55:18.390

@JonathanAllan, It's all great! Thank you for your help and enthusiasm regarding this problem! – McLinux – 2018-02-11T00:09:58.093

1Can you give a reference implementation? I don't quite get the specs. – Rɪᴋᴇʀ – 2018-02-11T01:20:05.987

I suggest not accepting any answers. The +2 is not that important. – user202729 – 2018-02-20T04:16:25.120