[Question]
  [
Given a non-negative integer skyline height list, answer how many uninterrupted 1-unit-high horizontal brush strokes are needed to cover it.
`[1,3,2,1,2,1,5,3,3,4,2]`, visualised as:
```
      5    
      5  4 
 3    5334 
 32 2 53342
13212153342
```
needs nine brush strokes:
```
      1    
      2  3 
 4    5555 
 66 7 88888
99999999999
```
### Examples
`[1,3,2,1,2,1,5,3,3,4,2]` → `9`
`[5,8]` → `8`
`[1,1,1,1]` → `1`
`[]` → `0`
`[0,0]` → `0`
`[2]` → `2`
`[2,0,2]` → `4`
`[10,9,8,9]` → `11`
  
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 38 bytes
```
a=>a.map(v=>(n+=v>p&&v-p,p=v),p=n=0)|n
```
[Try it online!](https://tio.run/##PY/BDoIwDIbve4qddGjBAZpAzPYAHvTgcZC44ECNjsWRnTz7AD6iL4JT1DZtvib9m78n6aStrkfThbrdq75mvWRcRhdpiGOc6Clz3IxGLjRgmAt804wGN913ynaVtAozvEMihhQSiD@18JzCHJISP@8PnCOxgGzg7L35yWGOkRiAIkGB/vkrTTwB/R2aezGFHDLIv@oY7dDPh7fcVQcyK4Qo9lBOinLWBMMfmHFctdq2ZxWd24Y4wOOQjwHXZLXdrCMjr1YRF/hY9i8 "JavaScript (Node.js) – Try It Online")
Simply a greedy algorithm which scan from left to right, only draw lines if needed, and draw it as long as possible.
Thanks Arnauld, save ~~2~~ 3 bytes
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~8 7~~ 5 bytes
*Saved 2 bytes thanks to @Adnan*
```
0š¥þO
```
[Try it online!](https://tio.run/##yy9OTMpM/f/f4OjCQ0sP7/P//z/a0EDHUsdCxzIWAA "05AB1E – Try It Online")
### How?
This is using the algorithm that was first found by [@tsh](https://codegolf.stackexchange.com/users/44718/tsh). If you like this answer, make sure to [upvote their answer](https://codegolf.stackexchange.com/a/179466/58563) as well!
Each time a skyscraper is lower than or as high as the previous one, it can be painted 'for free' by simply extending the brushstrokes.
For instance, painting skyscrapers \$B\$ and \$C\$ in the figure below costs nothing.
On the other hand, we need 2 new brushstrokes to paint skyscraper \$E\$, no matter if they're going to be reused after that or not.
[![buildings](https://i.stack.imgur.com/yFBN9.png)](https://i.stack.imgur.com/yFBN9.png)
For the first skyscraper, we always need as many brushstrokes as there are floors in it.
Turning this into maths:
$$S=h\_0+\sum\_{i=1}^n \max(h\_i-h\_{i-1},0)$$
If we prepend \$0\$ to the list, this can be simplified to:
$$S=\sum\_{i=1}^n \max(h\_i-h\_{i-1},0)$$
### Commented
```
0š¥þO     # expects a list of non-negative integers  e.g. [10, 9, 8, 9]
0š        # prepend 0 to the list                    -->  [0, 10, 9, 8, 9]
  ¥       # compute deltas                           -->  [10, -1, -1, 1]
   þ      # keep only values made of decimal digits
          # (i.e. without a minus sign)              -->  ["10", "1"]
    O     # sum                                      -->  11
```
[Answer]
# [Haskell](https://www.haskell.org/), 32 bytes
```
(0%)
p%(h:t)=max(h-p)0+h%t
p%_=0
```
[Try it online!](https://tio.run/##ZY1BCoMwFET3niIbIaEj/MQKWshJWinBNo00aqhZ9PRNxV1ThoE/j@GPM@vz7n2y@pI4laIIJXenKPRk3txVQdDBlXGjV01pMuPMNLstBWPhNc6RccvOEjUU5O5mu2scoXrx02nQZkRiV0azSKCM5I8V6G9MEjq06HqRPoP15rGmagjhCw "Haskell – Try It Online")
An improvement on [Lynn's solution](https://codegolf.stackexchange.com/a/179482/20260) that tracks the previous element `p` instead of looking at the next element. This makes the base case and recursive call shorter in exchange for needing to invoke `(0%)`.
`max(h-p)0` could be `max h p-p` for the same length.
[Answer]
# [Python 3](https://docs.python.org/3/), 37 bytes
```
lambda a:sum(a)-sum(map(min,a[1:],a))
```
[Try it online!](https://tio.run/##bY7NCsIwEITvfYqAlwSmkJ8WkoK@SO1hRYoFE0OtB58@ag9JDrIsu3yzw2x8b7dHMGk@ntOd/OVKjIbny3MS7W94itwvATSqYQIJkeK6hI3PfFQw0FB799/doIOehGAH1p6Ya/JhD5uxbSr/XllSRcpMFiYh/@GSqCsIWb3SVZkSDhauhKr0AQ "Python 3 – Try It Online")
-5 bytes by switching to Python 3, thanks to Sarien
---
# [Python 2](https://docs.python.org/2/), ~~47~~ ~~43~~ 42 bytes
```
lambda a:sum(a)-sum(map(min,a[1:],a[:-1]))
```
[Try it online!](https://tio.run/##bY7LCsIwEEX3/YqAmwQmkKQtJAH9kZrFiBQLJgatC78@1kIeCxnmwbkz3Imf9fYIKs3Hc7qjv1yRoH29PUXGf81jpH4JgJO0bquWS8dYis8lrGSmk4QeFMg9x23uYQDlGDkQfiKmK3sj6Ex111zvkRVZlYxERQLEH1rMVMNA1CeGxk6AAQ2m@Mn0BQ "Python 2 – Try It Online")
Alt:
```
lambda a:sum(a)-sum(map(min,zip(a[1:],a)))
```
[Try it online!](https://tio.run/##bY7BCsIwEETv/YqAlwSmkKQtJAX9kdrDihQLJgatB/35qIUmOciy7PJmh9nwWi43r@O0P8YrudOZGPWPp@Mk6t9wFLibPd5z4DSofgQJIWK4z35hEx8UGmiotbvv3qCFHgXbsfrAbJXuOpiNmqpwr7UpKisbkhlJyD80hemCQeYn2iJOwsLApjwVPw "Python 2 – Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 35 bytes
```
f(a:b:c)=max(a-b)0+f(b:c)
f a=sum a
```
[Try it online!](https://tio.run/##ZY3BCsIwEETv/Yo9JnSETWqhLeRLpIetNVhsqlgF/z5qb64MAzuPYecs6@U0zzlHI93QHW1I8jKyGyyX0XxBEUnC@kwkOcm0UKDxWhDd7tPyIBPp4FDBw22uP3eFPXxvfzo1GkUcNimqIoMV0Y89@G/MMVo0aHub3w "Haskell – Try It Online")
Line 2 could be `f[a]=a` if I didn't also have to handle the case `[]`.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
A port of my [05AB1E answer](https://codegolf.stackexchange.com/a/179471/58563), which itself is similar to [@tsh JS answer](https://codegolf.stackexchange.com/a/179466/58563).
```
ŻI0»S
```
[Try it online!](https://tio.run/##y0rNyan8///obk@DQ7uD////H22oY6xjpGMIxqZAtrGOiY5RLAA "Jelly – Try It Online")
### Commented
```
ŻI0»S    - main link, expecting a list of non-negative integers  e.g. [10, 9, 8, 9]
Ż        - prepend 0                                             -->  [0, 10, 9, 8, 9]
 I       - compute the deltas                                    -->  [10, -1, -1, 1]
  0»     - compute max(0, v) for each term v                     -->  [10, 0, 0, 1]
    S    - sum                                                   -->  11
```
[Answer]
# [K (oK)](https://github.com/JohnEarnest/ok), ~~12~~ 7 bytes
-5 bytes thanks to ngn!
A [k (oK)](https://github.com/JohnEarnest/ok) port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):
```
+/0|-':
```
[Try it online!](https://tio.run/##y9bNz/7/P81KW9@gRlfd6n@agqGCsYIRkARhUyDbWMFEwYjrPwA "K (oK) – Try It Online")
# [J](http://jsoftware.com/), 15 bytes
A J port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):
```
1#.0>./2-~/\0,]
```
[Try it online!](https://tio.run/##RYvBCoMwEETv@YqHLYiQrpuooIJ@SXsqDaWXHjzrr8e0YGTY4c3uzieGZRKUEY3uIjpL7W9bfVf7iJUphDJMUmJZR8JizOv5/hJwNPjkv@kSN7T449jRn39/HVGvmpGMNjd9WufglIGewcQd "J – Try It Online")
## My naive solution:
# [J](http://jsoftware.com/), 27 bytes
```
1#.2(1 0-:])\0,@|:@,~1$~"0]
```
[Try it online!](https://tio.run/##RYvBCsIwEETv@YpHLdRCDLuphTZQ6IfYkxjEi4detb8eo9DIsMOb3Z1HiuvkEAKS9OD8UZFTWNqL2PkVZrtpvVWypNZUjiZOrsHyDsTVmNv1/iSidPjs3@kzd5zx@7Fn@P/9tEeppSAFbWn6vC5BhZGB0aQP "J – Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), ~~34~~ 32 bytes
*2 bytes trimmed by Lynn*
```
g x=sum$max 0<$>zipWith(-)x(0:x)
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P12hwra4NFclN7FCwcBGxa4qsyA8syRDQ1ezQsPAqkLzf25iZp5tQVFmXolKerShjpGOsY4JmLSM/Q8A "Haskell – Try It Online")
So to start we have `zipWith(-)`. This takes two lists and produces a new list of their pairwise differences. We then combine it with `x` and `(0:x)`. `(0:)` is a function that adds zero to the front of a list and by combining it with `zipWith(-)` we get the differences between consecutive elements of that list with a zero at the front. Then we turn all the negative ones to zero with `(max 0<$>)`. This creates a new list where each element is the number of new strokes that has to be started at each tower. To get the total we just sum these with `sum`.
[Answer]
# Dyalog APL, 11 bytes
```
+/0⌈¯2-/0,⊢
```
`,` concatenate, so `0,` concatenates 0.
`¯2-/`  
or  
`¯2` length of window. (minus means the window is rotated)  
`-/` does a window operation of `-`(minus) with window size `2`
`0⌈` the operation is called ceiling, but when it gets an input from both sides it also does "get the larger of the two inputs" so `0⌈1 3 0 ¯2` will give `1 3 0 0`.
and finally, a `+/` which is a sum.
[Try it!](https://razetime.github.io/APLgolf/?h=AwA&c=09Y3eNTTYaT7qHeFvoHOo65FAA&f=S1d41DZBIe1R75JHfVO9gv39uNIV1KMNdYx1jHQMwdgUyDbWMdExilUHy5nqWEBZhjpgCOVBKQMdAygLpsFIxwCu2dBAx1LHQscyVh0A&i=AwA&r=tryapl&l=apl-dyalog&m=train&n=f#)
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 8 bytes
*-2 bytes from @Shaggy*
```
mîT Õ¸¸è
```
**Explanation**
```
mîT Õ¸¸è      Full program. Implicit input U
                e.g. U = [2,0,2]
mîT             Map each item X and repeat T(0) X times
                     U = ["00","","00"]
    Õ           Transpose rows with columns
                     U = ["0 0","0 0"]
     ¸¸         Join using space and then split in space
                     U = ["0","0","0","0"]
        è       Return the count of the truthy values
```
[Try it online!](https://tio.run/##y0osKPn/P/fwuhCFw1MP7Ti04/CK//@jow11jHWMdAzB2BTINtYx0TGK1eGKNtWxAFGGOmAIYoKwgY4BiAKrMNIxgCg1NNCx1LHQsYyNVdDNDQIA "Japt – Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), 8 bytes
```
0whd3Y%s
```
[Try it online!](https://tio.run/##y00syfn/36A8I8U4UrX4//9oQx1jHSMdQzA2BbKNdUx0jGIB "MATL – Try It Online")
Pretty much @Arnauld's algorithm. Saved one byte (thanks @LuisMendo) by casting to `uint64` rather than selecting `)` positive entries.
[Answer]
# [R](https://www.r-project.org/), 30 bytes
```
sum(pmax(0,diff(c(0,scan()))))
```
[Try it online!](https://tio.run/##TY3JCsMgEIbveYqBXBSmQbNAAu3DSKrFQ0xwaQulz26jpqWK8C8znzbW8LBiAzcLQyhoAwJUMLPXqwG1WpDCaWnBS@e1uVXq8m3Jnb7S1vn0S5qmoVb6YFMZXVjItognYXjVSpF5F@WbdOK7ShHHDlvk@Q277rDHllKoYcr9gGN24zGdb054TrJkVaGzP1cgbdHIDmhfMAwnHHEqHB4/ "R – Try It Online")
Porting of [@Arnauld solution](https://codegolf.stackexchange.com/users/58563/arnauld) which in turn derives from [@tsh great solution](https://codegolf.stackexchange.com/a/179466/41725)
[Answer]
# Common Lisp, ~~88~~ 87 bytes
```
(lambda(s)(let((o 0))(dolist(c s)(incf o(max 0 c))(mapl(lambda(y)(decf(car y)c))s))o))
```
## non-minified
```
(lambda (skyline)
  (let ((output 0))
    (dolist (current-skyscraper-height skyline)
      (incf output (max 0 current-skyscraper-height))
      (mapl (lambda (skyscraper)
              (decf (car skyscraper) current-skyscraper-height))
            skyline))
    output)))
```
[Test it](https://rextester.com/NMQDH22914)
When one tower is painted, it takes a number of brushstrokes equal to it's height. These brushstrokes translate to all the following ones, which is indicated here by subtracting the current tower's height from all other towers (and itself, but that doesn't matter). If a following tower is shorter, then it will be pushed to a negative number, and this negative number will subsequently subtracted from the towers that follow (indicating brushstrokes that could not be translated from a previous tower to the next ones). It actually just subtracts the number from *all* tower heights, including previous ones, but this doesn't matter because we don't look at the previous ones again.
[Answer]
# [Husk](https://github.com/barbuz/Husk), 4 bytes
```
ΣẊ>Θ
```
[Try it online!](https://tio.run/##yygtzv6vkHtouYaXkoKunYJSqmaxRvGjpkbN/@cWP9zVZXduxv///6OjDXWMdYx0DMHYFMg21jHRMYrViTbVsQCShjpgCGQBkYGOAZAESRrpGIAVGRroWOpY6FjGxgIA "Husk – Try It Online")
Uses the same algorithm as everybody else (see [here](https://codegolf.stackexchange.com/a/179471/62393) for an explanation), it just does so with fewer bytes than anybody else :D
### Explanation
```
ΣẊ>Θ
   Θ    Prepend a 0 to the values
 Ẋ      For each pair of adjacent values (a,b)
  >     Return b-a if b>a else 0
Σ       Sum all of these values
```
`>` in Husk does a great job for this task: it does return a truthy value iff `b>a`, but this truthy value also happens to be the difference `b-a`, accomplishing two tasks in one go!
[Answer]
# [Japt](http://github.com/ETHproductions/japt) [`-x`](https://codegolf.meta.stackexchange.com/a/14339/), ~~7~~ 6 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
änT f¬
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LXg&code=5G5UIGas&input=WzEsMywyLDEsMiwxLDUsMywzLDQsMl0)
1 byte saved thanks to [Oliver](https://codegolf.stackexchange.com/users/61613/oliver).
```
änT f¬     :Implicit input of integer array
ä          :Consecutive pairs
 n         :Reduced by inverse subtraction
  T        :After first prepending 0
    f      :Filter elements by
     ¬     :Square root (The square root of a negative being NaN, which is falsey)
           :Implicitly reduce by addition and output
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~13~~ 10 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
Z>Lε@γPO}O
```
[Try it online](https://tio.run/##yy9OTMpM/f8/ys7n3FaHc5sD/Gv9//@PNtQx1jHSMQRjUyDbWMdExygWAA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfaVL6P8oO59zWyOKHc5tDvCv9f@v8z862lDHWMdIxxCMTYFsYx0THaNYHYVoUx0LEGWoA4YgJggb6BiAKLAKIx0DiFJDAx1LHQsdy9hYAA).
**Explanation:**
```
Z            # Get the maximum of the (implicit) input-list
 >           # Increase it by 1 (if the list only contains 0s)
  L          # Create a list in the range [1, max]
   ε         # Map each value to:
    @        #  Check if this value is >= for each value in the (implicit) input
     γ       #  Split into chunks of adjacent equal digits
      P      #  Take the product of each inner list
       O     #  Take the sum
        }O   # And after the map: take the sum (which is output implicitly)
```
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), 21 bytes
```
\d+
$*
(1+)(?=,\1)
1
```
[Try it online!](https://tio.run/##HYmxCoAgFEX3@x0FmnfwaYIO0dhPOBTU0NIQ/f9L5HDgwHmv734OHc22az0dhglGnDXrwioWEFVhZKB0U@vImQGJGcIO4OkREOjbEM/CzPID "Retina 0.8.2 – Try It Online") Link includes test cases. Explanation:
```
\d+
$*
```
Convert to unary.
```
(1+)(?=,\1)
```
Delete all overlaps with the next tower, which don't need a new stroke.
```
1
```
Count the remaining strokes.
[Answer]
# [Nekomata](https://github.com/AlephAlpha/Nekomata), 5 bytes
```
ç-P‼∑
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m70iLzU7PzexJHFZtJJurlLsgqWlJWm6FqsOL9cNeNSw51HHxCXFScnFUOEFN92jDXWMdYx0DMHYFMg21jHRMYrlijbVsQCShjpgCGQBkYGOAZAESRrpGIAVGRroWOpY6FjGQswDAA)
A port of [@Arnauld's 05AB1E answer](https://codegolf.stackexchange.com/a/179471/9288).
```
ç-P‼∑
ç           Prepend zero
 -          Minus to get the delta
  P‼        Filter by positive
    ∑       Sum
```
[Answer]
# [Thunno 2](https://github.com/Thunno/Thunno2) `S`, ~~6~~ 5 [bytes](https://github.com/Thunno/Thunno2/blob/main/docs/codepage.md)
```
0ƤṢ0§
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m728JKM0Ly_faGm0UrBS7IKlpSVpuhYrDY4tebhzkcGh5UuKk5KLoaILVkUbGuhY6ljoWMZCRAA)
Port of @Arnauld's 05AB1E answer.   
*-1 thanks to @alephalpha*
#### Explanation
```
0ƤṢ0§   # Implicit input 
0Ƥ      # Prepend a 0
  Ṣ     # Compute deltas
   0§   # Maximum with 0
        # S flag sums the list
        # Implicit output
```
[Answer]
# Perl 5, 21 bytes
```
$\+=$_>$'&&$_-$';//}{
```
[TIO](https://tio.run/##K0gtyjH9/18lRttWJd5ORV1NTSVeV0XdWl@/tvr/f0MuYy4jLkMwNgWyjblMuIz@5ReUZObnFf/XLQAA)
How
* `-p` + `}{` + `$\` trick
* `//` matches empty string so that for next line postmatch `$'` will contain previous line
* `$\+=$_>$'&&$_-$'` to accumulate difference between current line and previous if current is greater than previous, (could also be written `$\+=$_-$' if$_>$'`, but perl doesn't parse `$\+=$_-$'if$_>$'` the same)
[Answer]
# [C# (Visual C# Interactive Compiler)](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#csc) with flag `/u:System.Math`, 47 bytes
```
n=>n.Select((a,i)=>i<1?a:Max(a-n[i-1],0)).Sum()
```
[Try it online!](https://tio.run/##ZZHfa8IwEMefzV8RZA8Jnl1TFdRa9yAMBvrUwR5KH0qNGtBE2rg5Sv71ddeObf4gJBc@3H2/d0le9vNS1c8nnc@UtkkKeM43Ua2jufZiuZe5ZSwDxaO5momnbLrKzizr60T1RQo@5158OjBeh@Q9K6iVpV1kpSxpRLX8oOxHk2LgSVoR1sCWVQIGEIBo9wjvAxhC4GDC4TJrBGMH42smoF0OxDV32M4V8MG/Y2gR3BDwG@PhjYkPExjDBF0EdyEhG1M001CFo/khhtn/tN5S6q3dIe31OKlIZ2F0afbSeyuUleyhWyk3pV0ekk7zSkX7Phv2V5@o1Hux8iB@M7bGrDHlLiEIb7SXSjf68nzEj5JrWjWV3quJbaH0lnEHqGVphZaXtGnF1V/maBWq1Y@nafxZor63yuzuGw "C# (Visual C# Interactive Compiler) – Try It Online")
## Old version, with flag `/u:System.Math`, 63 bytes
```
n=>n.Aggregate((0,0),(a,b)=>(a.Item1+Max(0,b-a.Item2),b)).Item1
```
I feel like this solution is more elegant than the first one. It goes through the array with a two-value tuple as a starting value, picking up values, and stores the value before it in the second part of the tuple.
[Try it online!](https://tio.run/##ZZBda8IwFIav7a8IsosEj11bFdRaYQiDgV4p7EK8iDXWwJZKEzdHyV9fd9KyzQ9Cvp5z8r45J9XdVMvq@aTSiVRmvQFcp/ukUslU@U9ZVoiMG0FpAAEDymHLkinl/osR72Fnwc8Y2Habe8QwyppQFXsfvCBGaDPjWmiSECU@CW08CG5svSk96mDNyhB6EEFYzwGee9CHyMKIwWXWAIYWhtcshHpYCK@5dV@@BFjDHUOL6IZA4Iz7NyYBjGAII3QJmY09b58XrhoisbQgxm3yX60/FyozB6SdDvNKrzXLlc7fhP9aSGzmQ7uUdkzaLPZarktF3Z89/Xu/lpumjb8ZWZ7vMOUuIYpvtOdSOX1xPorUiB0p3Ut/lS9NIVVGmQXUMqREy0vqvmKr7/xoJKpVj6fx8kujvr/g5vAD "C# (Visual C# Interactive Compiler) – Try It Online")
[Answer]
# Pyth, 8 bytes
```
s>#0.++0
```
Yet another port of [@tsh's marvellous answer](https://codegolf.stackexchange.com/a/179466/41020). Takes the sum (`s`) of the positive values (`>#0`) of the deltas (.+) of the input with 0 prepended (`+0Q`, trailing Q inferred).
Try it online [here](https://pyth.herokuapp.com/?code=s%3E%230.%2B%2B0&input=%5B1%2C3%2C2%2C1%2C2%2C1%2C5%2C3%2C3%2C4%2C2%5D&debug=0), or verify all the test cases at once [here](https://pyth.herokuapp.com/?code=s%3E%230.%2B%2B0&input=%5B%5D&test_suite=1&test_suite_input=%5B1%2C3%2C2%2C1%2C2%2C1%2C5%2C3%2C3%2C4%2C2%5D%0A%5B5%2C8%5D%0A%5B1%2C1%2C1%2C1%5D%0A%5B%5D%0A%5B0%2C0%5D%0A%5B2%5D%0A%5B2%2C0%2C2%5D%0A%5B10%2C9%2C8%2C9%5D&debug=0).
## String joining method, 10 bytes
This was the solution I wrote before browsing the other answers.
```
lcj.t+d*LN
```
[Test suite.](https://pyth.herokuapp.com/?code=lcj.t%2Bb%2ALN&input=%5B%5D&test_suite=1&test_suite_input=%5B1%2C3%2C2%2C1%2C2%2C1%2C5%2C3%2C3%2C4%2C2%5D%0A%5B5%2C8%5D%0A%5B1%2C1%2C1%2C1%5D%0A%5B%5D%0A%5B0%2C0%5D%0A%5B2%5D%0A%5B2%2C0%2C2%5D%0A%5B10%2C9%2C8%2C9%5D&debug=0)
```
lcj.t+d*LNQ   Implicit: Q=eval(input()), b=<newline>, N=<quote mark>
              Trailing Q inferred
        L Q   Map each element of Q...
       * N    ... to N repeated that many times
     +b       Prepend a newline
   .t         Transpose, padding with spaces
  j           Join on newlines
 c            Split on whitespace
l             Take the length, implicit print
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 34 bytes
A port of [@Arnauld](https://codegolf.stackexchange.com/users/58563/arnauld)'s [solution](https://codegolf.stackexchange.com/a/179471/9288).
```
Tr@Select[{##,0}-{0,##},#>0&]&@@#&
```
[Try it online!](https://tio.run/##JYxBCsMgFESv8uGDqwlV00CyaPAIhXYnLiQYGmi6CO7EsxtNGYY3i8fsPn7C7uO2@LLSg8r7MK/wDUu0iRkyd0mCOYNnKZwwhkV5HtsvWqZuptWycyToZiglhR4a6upQd487dAalAWODwpU2W2V9r7gMDflXlcSEEVPO5QQ "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# Clojure, 50 bytes
```
#((reduce(fn[[s n]i][(+(max(- i n)0)s)i])[0 0]%)0)
```
[Try it online! (Why doesn't this print anything?)](https://tio.run/##dY7bCoMwDIavzVMUxiBlGVSdoM8SejE8QIerUhX29lmtsO1mOZHkD@Frx@mxhV4Ehykodn7eVsiYcyqpoDxlFfuSblRYxRXVseaUPHYxDJlYd7Egk45yQw3V1FhrAbsJMpyD8@voVfqvfxYIcEIMfbe1PQ6eeVHeOst4wef9hVfllNdGL9pZzUYZe44TQJYdn6IBgIj8AZYELB9g4Rg7sPAuHsDyBX4D "Clojure – Try It Online")
```
#( ; begin anonymous function
    (reduce
        (fn [[s n] i] ; internal anonymous reducing function, destructures accumulator argument into a sum and the previous item
            [(+ (max (- i n) 0) s ; the sum part of the accumulator becomes the previous sum plus the larger of zero and the difference between the current number and the last one, which is how many new strokes need to be started at this point
            i]) ; ...and the previous item part becomes the current item
        [0 0] ; the initial value of the accumulator gives no strokes yet, and nothing for them to cover yet
        %) ; reduce over the argument to the function
    0) ; and get the sum element of the last value of the accumulator.
```
[Answer]
# [Java (JDK)](http://jdk.java.net/), 57 bytes
```
a->{int s=0,p=0;for(int x:a)s-=(x>p?p:x)-(p=x);return s;}
```
[Try it online!](https://tio.run/##bVDBboMwDL33K3wkk0FAV6mF0WmXSTvs1N2qHtIWqnQQosRUVIhvZ4GyimpTZMXPfnp@9plfuHs@fneiUKUmOFvsVSRy7yme/alllTyQKOW/TUM65UXfOuTcGPjkQkIzA1DVPhcHMMTJfpdSHKGwPWdDWsjTdgdcnwwbqABf5Yek93HOi5C03a0hSzrurhuLwCQ@qsSPs1I7Pa4jzoybOPVavaqoZq6jkprFOqVKSzBx28WD7qBkZ1FqyEAyTgNoIMA5hhgMsbD5HJ8xhBbvhAUupzDA4U1L09xHfwoflEL0H6UDH1e4xNVvrb2ZHZcb7UY30@zueXM1lBZeWZGn7Akpl07mcaXy65ux13N6OmM3qXbWR9v9AA "Java (JDK) – Try It Online")
Another port of [tsh](https://codegolf.stackexchange.com/users/44718/tsh)'s [Javascript answer](https://codegolf.stackexchange.com/a/179466/16236). So make sure you've upvoted them.
Note that I used subtraction instead of addition because it allowed me to write `(p=x)` as right operand in the same statement.
[Answer]
# [Factor](https://factorcode.org/) + `math.unicode`, 24 bytes
```
[ dup 0 prefix [v-] Σ ]
```
[Try it online!](https://tio.run/##TY5NDoJADIX3nOJdQMKPJKIHMG7cGFeExWQokaDDOAxEQziN9/FKY0nA0Kbt99qmaSmkbYy7Xk7n4x41GUV3tPTsSElq8RD25vc07cyiU5VsCoI2ZO1bm0pZHDxv8MA2IESMiPMUCXOMLfM4TxPs/hzOvuilBuwLRysKVioMkPKplBuj5zIUneY5v1RWL2T9Jsf3g9w9hEZrhax99wM "Factor – Try It Online")
## Explanation
```
         ! { 10 9 8 9 }
dup      ! { 10 9 8 9 } { 10 9 8 9 }
0        ! { 10 9 8 9 } { 10 9 8 9 } 0
prefix   ! { 10 9 8 9 } { 0 10 9 8 9 }
[v-]     ! { 10 0 0 1 }  (subtract two vectors, clamping negative numbers to 0)
Σ        ! 11
```
[Answer]
# [Kotlin](https://kotlinlang.org)
```
fun f(test:IntArray):Int{var s=0;var p=0;test.forEach{val f=(if(it>p)p else it);p=it;s-=f-p};return s}
```
[Try it yourself](https://pl.kotl.in/99_GhJAsJ?theme=darcula)
[Answer]
# [Arturo](https://arturo-lang.io), ~~35~~ 33 bytes
```
$=>[l:0sum map&'x[max@[0x-l]l:x]]
```
[Try it](http://arturo-lang.io/playground?FwZpXS)
[Answer]
# [Uiua](https://uiua.org), 11 [bytes](https://codegolf.stackexchange.com/a/265917/97916)
```
/+↥0-↘¯1⊂0.
```
[Try it!](https://uiua.org/pad?src=ZiDihpAgLyvihqUwLeKGmMKvMeKKgjAuCgpmIFsxIDMgMiAxIDIgMSA1IDMgMyA0IDJdICMg4oaSIDkKZiBbNSA4XSAjIOKGkiA4CmYgWzEgMSAxIDFdICMg4oaSIDEKZiBbXSAjIOKGkiAwCmYgWzAgMF0gIyDihpIgMApmIFsyXSAjIOKGkiAyCmYgWzIgMCAyXSAjIOKGkiA0CmYgWzEwIDkgOCA5XSAjIOKGkiAxMQ==)
Port of tsh's [JavaScript answer](https://codegolf.stackexchange.com/a/179466/97916).
```
/+↥0-↘¯1⊂0.
          .  # duplicate
        ⊂0   # prepend a zero
     ↘¯1     # drop last element
    -        # subtract the two arrays
  ↥0         # change negative numbers to zero
/+           # sum
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~15~~ ~~14~~ 13 bytes
```
ts:<~"@Y'x]vs
```
Input is a column vector, using `;` as separator.
[Try it online!](https://tio.run/##y00syfn/v6TYyqZOySFSvSK2rPj//2hDa2NrI2tDMDYFso2tTayNYgE) Or [verify all test cases](https://tio.run/##y00syfmf8L@k2MqmTskhUr0itqz4v0vI/2hDa2NrI2tDMDYFso2tTayNYrmiTa0tgKShNRgCWUBkYG0AJEGSRtYGYEWGBtaW1hbWlrEA).
### Explanation
```
t       % Implicit input: column vector. Duplicate
s       % Sum
:       % Range from 1 to that. Gives a row vector
<~      % Greater or equal? Element-wise with broadcast
"       % For each column
  @     %   Push current columnn
  Y'    %   Run-length encoding. Gives vector of values (0, 1) and vector of lengths
  x     %   Delete vector of lengths
]       % End
v       % Vertically concatenate. May give an empty array
s       % Sum. Implicit display
```
]