#JavaScript (ES6), <s> 106 104 </s> 100 bytes _Saved 2 bytes thanks to @Shaggy_ <!-- language-all: lang-javascript --> a=>a.filter(n=>n,[...a].map((v,i)=>a[a[p>v&(n=~~a[i+1])<p?k:i+(k=i,n>v&p<n)]+=x=a[i],p=v,i]-=x,p=0)) [Try it online!](https://tio.run/##fY3BbsIwEETv/QqfKltZjNeEJlQ4/RDLB4smyCV1LEARJ3493aQVrSCqRx6tNPt2PnzvT7tjSOdF7N7roTGDN5WXTWjP9ZFHU0WwUkrv5KdPnPcQBOXW21T1z5Rfr96GDJ3YprfDa8j4wQSIlKVtFC4zF0O5g2SIdAtzoUkJMey6eOraWrbdnjfcogJcgyZXP47KCcGmt1wym68ZKrZS5O7pDt7At0pAKG8U@4U3bBQWDFn5QCO8EJtPXpDnpPFWMV4aaU29muk5UsOKPt533nofkbnVvwjOtPwDTQhDN3wB "JavaScript (Node.js) – Try It Online") ###Commented We first update the original input array `a[]` by iterating on a copy of it. During this step, all values 'attracted' by other ones are set to \$0\$. Because the array is parsed from left to right, we can just add \$a_i\$ to \$a_{i+1}\$ whenever a value is attracted by its right neighbor. Example: \$4\rightarrow5\rightarrow6\$ is transformed as \$[0,\color{red}9,6]\$ and then \$[0,0,\color{red}{15}]\$. But we need to add \$a_i\$ to the highest previous value \$a_k\$ (with \$k<i\$) when several values in a row are attracted by their left neighbor. Example: \$6\leftarrow5\leftarrow4\$ is transformed as \$[\color{red}{11},0,4]\$ and then \$[\color{red}{15},0,0]\$. [...a] // create a copy of a[] .map((v, i) => // for each value v in a[] at position i: a[ // this statement updates a[i]: a[ // this statement updates either a[i] or an adjacent value: p > v & // if the previous value p is greater than v (n = ~~a[i + 1]) // and the next value n < p ? // is less than p: k // use k : // else: i + ( // use either i or i + 1: k = i, // set k to i n > v & // use i + 1 if n is greater than v p < n // and p is less than n ) // ] += x = a[i], // add x = a[i] to the entry defined above p = v, // update the previous value to v i // actual index to update a[i] ] -= x, // subtract x from a[i] p = 0 // start with p = 0 ) // end of map() We then filter out all entries equal to \$0\$. a.filter(n => n)