var sum = 0; for(v : vals) {sum += 2*v;} sum -= (vals.first + vals.last);
(Proof: when you sum the neighbours of all the elements then you add each element twice except the first and the last one.)
Even simpler and even faster:
var s = sum(vals) * 2 - vals.first - vals.last;
(Proof: when you sum the neighbours of all the elements then you add each element twice except the first and the last one.)
Even simpler and even faster: