[Question]
  [
The gravitational force is a force that attracts any two objects with mass. In this challenge our objects will be Numbers and their mass will be their value. To do so, we don't care about the strength of the force but the direction of it.
Imagine this set of numbers 
```
[1 6 9 4 6 9 7 6 4 4 9 8 7]
```
Each of them creates a force between itself and it's adjacent numbers. Under some conditions, this will cause another number to be attracted (moved) toward a number. When the number is bigger than the adjacent, it attracts it. Lets take a look at our previous example:
```
[1 → 6 → 9 ← 4 6 → 9 ← 7 ← 6 ← 4 4 → 9 ← 8 ← 7]
```
The number `1` is not big enough to move `6`, but the number `6` is, etc... Basically, numbers are moved to the biggest adjacent number (also bigger than the number itself). If both of the adjacent numbers are equal it is not attracted then. It also happens when the number and it adjacent are equal.
This is only to show the attraction, but what happens after? Numbers that collide due to attraction are summed up:
```
[20 32 28]
```
So basically the challenge is, Given a set of numbers, output the result of the attracted set of numbers.
---
**Example 1**
```
Input  => [10 15 20 10 20 10 10]
          [10 → 15 → 20 10 20 ← 10 10]
Output => [45 10 30 10]
```
**Example 2**
```
Input  => [9 9 9 9 8 1 8]
          [9 9 9 9 ← 8 1 8]
Output => [9 9 9 17 1 8]
```
**Example 3**
```
Input  => [1 6 9 4 6 9 7 6 4 4 9 8 7]
          [1 → 6 → 9 ← 4 6 → 9 ← 7 ← 6 ← 4 4 → 9 ← 8 ← 7]
Output => [20 32 28]
```
**Example 4**
```
Input  => [1 2 3 2 1]
          [1 → 2 → 3 ← 2 ← 1]
Output => [9]
```
**Example 5**
```
Input  => [1]
Output => [1]
```
**Example 6**
```
Input  => [1 1]
Output => [1 1]
```
**Example 7**
```
Input  => [2 1 4]
Output => [2 5]
```
---
## Notes
* Attraction only happens once
* Numbers are not attracted to non-adjacent Numbers
* The set of numbers will only contain positive integers
  
[Answer]
# JavaScript (ES6), ~~106 104~~ 100 bytes
*Saved 2 bytes thanks to @Shaggy*
```
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/##fY9Rb4MgFIXf9yt4WiDeUq7VaZfifgjhgXS6sDokbWP61L/urm7plurGCSckh@8eeHe9O@2PPp5XoXuth0YPTldONr4910cedBXASCmdlR8uct6DF5QbZ2LVP1J@vTrjE7RiF18Ozz7hB@0hUBZ3QdhEXzTlFqIm0q70hU5KiGHfhVPX1rLt3njDDSrAHFJy9e2orBBsWus1M1nOULGNIrcPd/AWvlQCQnmj2A@8ZaOwYMjKGY3wRGw2eUGekcZZxThppFPqTVm6RKawoY33nbfeObJ09TeCCy3/QBPC5hA9CrK/sOlTLLfDJw "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 turned into \$[0,\color{red}9,6]\$ and then \$[0,0,\color{red}{15}]\$.
But when several values in a row are attracted by their left neighbor, we need to add \$a\_i\$ to the first attractor \$a\_k\$ of this sequence (with \$k<i\$) rather than simply \$a\_{i-1}\$.
Example: \$6\leftarrow5\leftarrow4\$ is turned into \$[\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 (attraction to the left):
        k              //         use k (k is initially undefined, but this code cannot
                       //         be triggered during the first iteration)
      :                //       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 (attraction to the right)
        )              //
    ] += 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)
```
[Answer]
# [Stax](https://github.com/tomtheisen/stax), ~~27~~ ~~25~~ ~~23~~ 18 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
«╥╦.ê§┘h!@ÆEÿ☺╜╫♥B
```
[Run and debug it](https://staxlang.xyz/#p=aed2cb2e8815d968214092459801bdd70342&i=[1+6+9+4+6+9+7+6+4+4+9+8+7]%0A[10+15+20+10+20+10+10]%0A[9+9+9+9+8+1+8]%0A[1+6+9+4+6+9+7+6+4+4+9+8+7]%0A[1+2+3+2+1]%0A[1]%0A[1+1]%0A[2+1+4]&a=1&m=2)
Output is separated by newlines. 
This program operates on adjacent pairs in the array, and determines whether there should be a split between them using this procedure.
Consider some arbitrary input `[... w x y z ...]`. Here is how to determine if there should be a split between `x` and `y`.
* If `x == y`, then yes.
* If `x > y`, then iff `z >= x`.
* If `y > x`, then iff `w >= y`.
The summing is left as an exercise.
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), 64 bytes
```
\d+
$*
(?<=(1+)) ((?=(1+\1))(?<!\3 \1 )|(?!\1)(?!1+ \1))
1+
$.&
```
[Try it online!](https://tio.run/##fYuxCgIxEET7/Yo5UNk1IJm700tASelPpFDQwsZCLP33uKfW8tjHMMs8rs/b/dyWejy1egmyWIuW/UEZzKBa5lRp5mVXB1TCXlo6r9wMmH8i9OFm1RqxQ8b48eQenYyESRjBLXp3/JlRMr4kEEn@jNFj8KM44Bs "Retina 0.8.2 – Try It Online") Link includes test suite. Explanation:
```
\d+
$*
```
Convert to unary.
```
(?<=(1+)) ((?=(1+\1))(?<!\3 \1 )|(?!\1)(?!1+ \1))
```
Remove the separators between attracted numbers. `(?<=(1+))` sets `\1` to the number before the separator. After the separator, there are then two cases:
* The number after the separator is greater than both of the numbers before the separator
* The number before the the separator is greater than both of the numbers after the separator
In these cases there is an attraction between the two numbers and deleting the separator causes the numbers to collide, adding them together.
```
1+
$.&
```
Convert to decimal.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 23 bytes
```
Ø0jMÆmær0Ʋ3Ƥ×=4$o<ʋƝk⁸§
```
[Try it online!](https://tio.run/##y0rNyan8///wDIMs38NtuYeXFRkc22R8bMnh6bYmKvk2p7qPzc1@1Ljj0PL/1o8a5ijo2ik8aphr/XDHprDD7d7HNnE93L3lcPujpjWR//8bGigYmioYAUkDKGlowGWpAIEWCoYKFlyGCmZAtgmYNAeSJkAIkjMHyhgpGAOxIZAFxAA "Jelly – Try It Online")
A monadic link which takes a list of integers as its argument and returns a list of integers.
### Explanation
```
Ø0j                     | Join [0, 0] with input list
         Ʋ3Ƥ            | For each length 3 infix, do the following as a monad:
   M                    | - Indices of maximum
    Æm                  | - Mean
      ær0               | - Round to even (so the means of [1, 2, 3], [1, 2], [2, 3] and [1, 3] will all round to 2
                  ʋƝ    | For each neighbouring pair, do the following as a dyad:
            ×           | - Multiply
             =4$        | - Check if equal to 4
                o       | - Or
                 <      | - First number less than second
                    k⁸  | Split input after truthy values of the above
                      § | Sum, vectorised
```
Some inspiration taken from [@recursive’s Stax answer](https://codegolf.stackexchange.com/a/185858/42248).
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 111 bytes
```
a,b,c,s;P(){s=!printf("%d ",s);}f(int*v){for(b=s=0,c=*v;a=b,b=c;a<b|b<a&c<a||P(),s+=b,b<c&c<=a|!c&&P())c=*++v;}
```
[Try it online!](https://tio.run/##dU1LTsMwEF3TU0wjEdnNICUlUJDjO7AHFva0QVmQojryJvHZw9hVVAkRaX5@P9PDF9E8G7RI6NSbkKPT259L1w@tyO6PkKGTKrSCgZ2XY3u@CKudLpH0ziujLVpNyjR2so3JqTHTxCHoisg0xIg205bynFHJnqLwKsz@3B1hOLkh5gIHwwit8FLB8vVHn/ErbJLy23S9YM3mLnmi6f1TjlCVCNUTwj7u8rZjlxCk@mt4xVu9sC7Nf5VMPSdZvRyHdNSprvbDuneP8Jhmta5Zw9eoa1q9sGH@BQ "C (gcc) – Try It Online")
Takes a zero-terminated array of integers.
### Explanation
```
a,b,c,  // Three consecutive elements of input array
s;      // Accumulator for sum
P(){s=!printf("%d ",s);}  // Print and clear s
f(int*v){
    for(
        // Init
        b=s=0,
        c=*v;
        // Loop test
        a=b,  // Shift b into a
        b=c;  // Shift c into b, exit if zero
        // Post loop
        a<b|b<a&c<a||P(),  // Print if a==b || (b<a && a<=c)
        s+=b,  // Accumulate
        b<c&c<=a|!c&&P()   // Print if c==0 || (b<c && c<=a)
    )
        // Loop body
        c=*++v;  // Read next number into c
}
```
[Answer]
# [Python 2](https://docs.python.org/2/), 162 bytes
```
l=input()
a=[(L<R>C)-(R<L>C)for L,C,R in zip([0]+l,l,l[1:]+[0])]
while any(a):
 i=0
 while a[i]==0:i+=1
 m=a.pop(i);x,y=[i,i+m][::m];l[x:y+1]=l[i]+l[i+m],
print l
```
[Try it online!](https://tio.run/##NYu9DoMwDIT3PIXHRHErqFB/Au7CysQaZWBohaUQIkRV6MvTCLXyyffdWY7r3I/htG2eOMTXLJXoyMqmau@1Osi2apI/xwkarLEFDvDhKG3mtMc0NjdOp6ScePfsH9CFVXbKCGDKBPw6y44oM6wpFzBQd4xjlKzKBVeyjKwHZ40ZXOntYladO/LpRaeVLijixGEGv202Rzgj3BCKP1x2KHaleE2N@wI "Python 2 – Try It Online")
[Answer]
# [J](http://jsoftware.com/), 45 bytes
```
+//.~0,[:+/\2(<+.1=*)/\3(>./1:/@I.@E.])\0,,&0
```
[Try it online!](https://tio.run/##NYzBDoIwEETvfsXEg4CUttuCQCOGxGhi4smreDIS48U/8NfrlkImO5tO387Hr2UyonNIIKDheAqJ4@169rlS8qfF3eVqMOk@l9RtMzXY9CAVOdVfZH@Sj2zQQmy0z1av5/uLsgJpWB28cBjDpgpmCqKTjmg7iWoQmsi2s5oQRYhPrIGZAcKOv8vJa/aSFfB6aewmyMDyUAwphsuDlqY5Mahiwgfc5v8 "J – Try It Online")
Inspired by recursive's original Stax answer
[Answer]
# [R](https://www.r-project.org/), ~~222~~ ~~196~~ 173 bytes
Here is a solution with some help from [Robin Ryder](https://codegolf.stackexchange.com/users/86301/robin-ryder)
```
n=length(d<-diff(y<-x<-scan()));l=c(1,sign(d[-n]+d[-1]),-1);m=!!l*n&c(d[1]>0,d[-1]>0|d[-n]<0,d[n]<0);for(t in 1:n){for(i in which(m))y[a]=y[a<-i+l[i]]+x[i];y=x=y-x*m};x[!m]
```
[Try it online!](https://tio.run/##LYvLCsMgEEX3/QqzKWMSQQtdqfkRcRHMSzAWkkKVtt9uJ6Gbc5l77mylRB3GOD8XGBQb/DRBViwptrs@AqVUBu1AtLufIwyGRdsghaUtE1SuuqpCHa8OlbAdb0/X8c@5VMd9BJWvxYcR1j5BqldK39NjA098JCjcAlhl01uNUMw3wXhrm4SUWSedGT59ZTKp47YITsSd3JD8T8HL5Qc "R – Try It Online")
A short set of comments
```
n=length(d<-diff(y<-x<-scan()));  #read input and compute pairwise differences
                    #d[-n]+d[-1]: compare left and right differences
l=c(1,sign(d[-n]+d[-1]),-1)                 #direction of attraction
m=!!l*n&                          #indices of attracted numbers
  c(d[1]>0,d[-1]>0|d[-n]<0,d[n]<0)  
                                   #!!l*n eliminates zeroes in l & the case n==0
for(t in 1:n){                   #excessive loop on transfers
 for(i in which(m))
   y[a]=y[a<-i+l[i]]+x[i]         #transfer right vs. left
 y=x=y-m*x}                        #complete transfer
x[!m]                             #output
```
[Answer]
## Python, ~~114~~ 112 bytes
```
lambda a:eval('['+'+'.join(str(c)+',0'*((e<c>d)==(c<d>b))for b,c,d,e in zip([0]+a,a,a[1:]+[0],a[2:]+[0,0]))+']')
```
This uses the fact that the direction of the arrow doesn't actually matter, and that the presence of an arrow between a[i] and a[i+1] can be determined by looking at the range of four elements a[i-1:i+3].
Edit: Thank you to Jo King for the rule clarification
[Answer]
# [Perl 5](https://www.perl.org/), ~~156~~ 147 bytes
```
$q='$F[$i';map{eval"\$i++while$q]$\_"}"<$q+1]",">$q+1]&&$q]>$q+2]&&\$i<\@F"if eval"$q-1]-$q+1]||$q]>$q+1]";$\.=$".sum@F[$p..$i];($p=++$i)<@F&&redo}{
```
[Try it online!](https://tio.run/##LY3LDoIwEEV/hTSToqltWhM2PAwrVrp0JcSQWOMkIOWhLoBft1bC5s5J5twZo7sqsBbaxIfsAuhHdWlG/S4rkgMy9nlgpaEt4EpmEkPLVEF25LAApW7xx71DZ8d5mhG8e0sbWq4KvnjTtHquG0EuEiCif9Wp@2eEACyiDZiEMcBtnGaUdvrWzKO1Snoq8PYu5ZpKfhszYPPsLT8FQirp5hH7IQzPA1aJu2p5aX4 "Perl 5 – Try It Online")
[Answer]
# [Clojure](https://clojure.org/), ~~299~~ 252 bytes
```
(fn[l](loop[o[0]m(vec(map-indexed(fn[i v](def f #(max(nth l(+ % i)0)v))(-(f -1)(f 1)))l))i 0](defn f[x](update o(-(count o)x)#(+(l i)%)))(cond(<=(count m)i)(pop o)(>(m i)0)(recur(f 2)m(inc i))(<(m i)0)(recur(f 1)m(inc i))1(recur(conj(f 1)0)m(inc i)))))
```
[Try it online!](https://tio.run/##5ZBBasMwEEX3OcVACMxQApLjNClNe4kujRfBlomCLAljBd/eHcuBQOT0ApXwLOb/efqeyrhr6NQ4YmMLU6JxzheuEGWLN1Vhe/ZbbWs1qHoyaLiVWKsGGlizNqDtL2DwDTagSdCNCLfYwFYSV0lEhkiDiDMWmmIoMfj63CtwbKxcsD04GmiNb2gYseERbtsaT193uSVN6J1nH35jG9/BTlWh4ycyalHbipuEp2dRPkR5bzL6GhXx0PiM8z8FC/95C7RardB32vZmWgRvo5AC5B4yruJepSiJYD6f8HNxwdTgQu9DD0W@nyy76HpGfcB8jyDh@GC8Qs1meYjuJBa8s5jHeuCa853Ih8hNWJx8l0G2xMlgx59M8ixnSgGLgy8BciHBX4gUACmC40P@EpIuA/bl@As "Clojure – Try It Online")
---
### Explanation:
```
(fn [l]
  (loop [o [0]
         m (vec(map-indexed (fn [i v] ; This maps each element l[i] of l to max(l[i-1], l[i]) - max(l[i+1], l[i])
                              (def f #(max (nth l (+ % i) 0) v))
                              (- (f -1) (f 1)))
                            l))       ; l[x] is zero when l[x] is out of bounds of the input vector l
         i 0]
    (defn f [x] (update o (- (count o) x) #(+ (l i) %)))
    ; Defines a function f(x) that returns the result of mapping the (x-1)th to last element of o over the function g(y) = l[i] + y
    (cond
      (<= (count m) i) (pop o) ; If the length of m is less than or equal to i, there are no more elements in m, so return all but the last element of o
      (> (m i) 0) (recur (f 2) m (inc i)) ; If m[i] is positive, l[i] is pulled toward to the previous element, so add l[i] to the 2nd to last element of o
      (< (m i) 0) (recur (f 1) m (inc i)) ; If m[i] is negative, l[i] is pulled toward the next element, so add l[i] to the last element of o
      1 (recur (conj (f 1) 0) m (inc i))))) ; 1 is truthy
      ; If the length of l is less than or equal to i, and m[i] is not positive or negative, we have m[i] = 0, so l[i] is not pulled toward any other element
```
[Answer]
# [K (ngn/k)](https://codeberg.org/ngn/k), 47 bytes
```
{+/'x@.={x x}/(!#x)+{-/2=+/x<\:x 2 0}'3':0,x,0}
```
[Try it online!](https://ngn.bitbucket.io/k#eJwtjN0KgkAQhe/3KSYKTNzaX1N3E3oPW7AbIQoC8WJEtmdvVmM4h+8wZ2ZwSyEyvJ3bBQGjOO72mBfLSei2EHi9OwQNMmYmc5Ijl5GxyS2Hbv6ObgD0/eflj/3weL49+tmPeYhs6pQEVYIml39XMtgygUlMlQa2qUFBHTZWVQrpHi4U7eoVuaVJ1SrQN6NBbyUNhqQ8bwJlTqDCulCBRERLsEFDyX5mtzXC)
`0,x,0` surround the argument with 0s
`3':` triplets of consecutive items
`{` `}'` for each do
`x 2 0` get last and first of the current triplet - `x[2]` and `x[0]`. they are the neighbours of `x[1]`, on which the triplet is centred
`x<\:` compare using less-than against each of the current triplet
`+/` sum. the result is a pair corresponding to `x[2]` and `x[0]`
`2=` check if either neighbour is greater than the other 2 elements from `x`, return a pair of 0-or-1 booleans
`-/` subtract them. a result of -1 means `x[1]` is attracted to the left, 1 to the right, and 0 means it stays in place
`(!#x)+` add 0 to the first item, 1 to the second, etc. this computes the indices towards which items are attracted
`{x x}/` index with itself until convergence. the result are the effective indices to which each item is ultimately attracted
`x@.=` group `x` (the original argument) by those. the result is a list of lists
`+/'` sum each
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), ~~52~~ 51 bytes
```
{+/(⊃∘⍵¨⊢)⌸{⍵[⍵]}⍣≡(⍳≢⍵)+{-/2=+⌿⍵∘.<1↓1⌽⍵}¨3,/∊0⍵0}
```
[Try it online!](https://tio.run/##LU1NS4VAFN3Pr5idipozjmYv6pdECzEsSTDUTYirQt4zfRQRtO21ebsWEUQQgT/l/hE7U48798w9536c@Cp3z67jvDh3kzyuqiyZaf2UFdTdi7mxPZP6G1o@0/gxbanfWDR8NiAnyNOWxldavZg0vtNqA8WyG9fzj20afsCwtnckqXuUNHyDt9NWOR4tewEi2jmFB41rGExviroH@FZlAqwvsmqu0dVOuFyiTGn8OjTSOMsNVNBLurs1ikujZVJwGXIfKHaIV/Mg1L/SjC34fxxwiax3TEaaMsn3QYI/jIABQo9GGMQ95XNfD/lcISVEZ8FMR1ooJHStAJnuBXqFh8z9BQ "APL (Dyalog Classic) – Try It Online")
translation of [my k answer](https://codegolf.stackexchange.com/a/185980/24908)
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 21 bytes
```
0.øŒ3ùεZQXsÂU‚ÆZ≠}Å¡O
```
[Try it online!](https://tio.run/##yy9OTMpM/f/fQO/wjqOTjA/vPLc1KjCi@HBT6KOGWYfboh51Lqg93Hpoof///9GGBjqGpjpGQNIAShoaxP7X1c3L181JrKoEAA "05AB1E – Try It Online")
]