Zonderfm,
function LoadGrain(levels) {
return levels.reduce((sum, el, i) => {
let max = Math.min(Math.max(...levels.slice(0, i)), Math.max(...levels.slice(++i)));
if (max > el) sum += max - el;
return sum;
}, 0)
}
console.log(LoadGrain([4, 1, 3])) // 2
console.log(LoadGrain([2, 1, 5, 2, 7, 4, 10])) // 7
console.log(LoadGrain([2, 0, 1, 5, 2, 7])) // 6
console.log(LoadGrain([2, 4, 2])) // 0
console.log(LoadGrain([7, 4])) // 0
console.log(LoadGrain([])) // 0
console.log(LoadGrain([4, 0, 1, 3])) // 5