The age-old question - is it better to walk or run? There are two trains of thought. If you run, you cause the rain to effectively hit you from the front, which increases your surface area and basically causes you to run into the raindrops, getting hit with more water. However, if you walk, you spend longer in the rain.
Actually, there is no good answer, because if the rain is about to get worse, you'd better run to get out of it even if you get hit more immediately, because you don't want to get hit by the rain later on.
In fact, if you can't predict the future, you don't actually have a good way to know what the optimal strategy is. However, we are not subject to such constraints, and can take the future as input.
Specification
You are trying to get somewhere \$N\in\mathbb{Z}_{\geq1}\$ metres away (\$N\$ will be supplied as input). However, it has started raining. For the next \$N\$ seconds, a certain amount of rain will be falling per second, and will be constant within the span of a 1-second period of time. Specifically, during the \$i\$th second (\$1\leq i\leq N\$), there will be \$a_i\in\mathbb{Z}_{\geq0}\$ units of rain. You will also be given \$\{a_1,a_2,\dots,a_N\}\$ as input. It rains the same amount at all locations across that distance, but once you reach your destination, you will no longer get hit by rain.
Now, you want to know how to get rained on the least. During each second, you must either walk or run. On the \$i\$th second, if you walk, you will move 1 metre and get hit by \$a_i\$ units of rain. If you run, however, you will move 2 metres but get hit by \$2a_i\$ units of rain. Note that if you are 1 metre from the finish, if you run, it only makes sense to be hit by half of that because you're in the shelter after the first half-second, but since \$\frac{2a_i}2=a_i\$, you can just walk instead, so you don't really need to consider this case.
What is the least amount of rain you can get hit by in order to cross those \$N\$ metres?
I/O Format
You may take \$N\$ as an input if you wish. Since it's the same as the length of the list, you're allowed to exclude it.
You may take \$\{a_1,a_2,\dots,a_N\}\$ as an input if you wish. I'm not sure how you'd solve the problem without it.
You must output a single non-negative integer, the least amount of rain (in units) that you can be hit by in order to cross those \$N\$ metres.
Example
10 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The rain is progressively getting worse. Clearly, every second you spend outside makes the rain worse, so it's better to get hit by double the amount now than wait and get hit by the larger amount later. If you run for 5 seconds, you will move 10 metres and get hit by \$15\times2=30\$ units of rain. In comparison, if you walk for 10 seconds, you get hit by 55 units.
6 [1, 0, 1, 0, 1, 0]
The rain is acting weird. In this case, if you run for 3 seconds, you get hit by 4 units of rain. However, if you only run during the second and fourth seconds, and walk during the first and third, you still cover 6 metres, but here you only get hit by 2 units since you run during the time when the rain is stopped, and no rain doubled is still no rain.
Test Cases
7 [1, 0, 2, 5, 3, 0, 0] -> 11
1 [1] -> 1
6 [2, 5, 5, 2, 3, 1] -> 18
1 [5] -> 5
3 [3, 3, 5] -> 9
9 [1, 0, 3, 1, 1, 1, 3, 2, 3] -> 9
5 [3, 2, 0, 0, 0] -> 5
7 [3, 1, 4, 4, 3, 5, 3] -> 19
You can generate more test cases here; the input accepts the max \$N\$ and the max \$a_i\$. It's also an ungolfed reference implementation (the move
function) (I could have probably figured out a DP relation to solve this more efficiently, but I was too lazy, and since it's code-golf, I don't suppose anyone else will use a more efficient solution either...)
Scoring
- this is code-golf; since you are writing on your umbrella and have limited space, your code must be as short as possib- wait, you had an umbrella? [...]
- standard loopholes are forbidden as usual
- have fun and happy golfing!