Question: Let's consider the following sequence:

$$9,8,7,6,5,4,3,2,1,0,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71...$$
This is the sequence of \$9\$'s complement of a number: that is, \$ a(x) = 10^d - 1 - x \$ where \$ d \$ is the number of digits in \$ x \$. ([A061601](https://oeis.org/A061601) in the OEIS).Your task is to add the first \$n\$ elements.

### Input

A number \$n∈[0,10000]\$.

### Output

The sum of the first \$n\$ elements of the sequence.

### Test cases

```
0 -> 0
1 -> 9
10 -> 45
100 -> 4050
1000 -> 408600
10000 -> 40904100

```

Standard [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") rules apply. The shortest code in bytes wins.

Answer: 
JavaScript, 47 bytes
====================

```javascript
f=n=>(t=10**`${n}`.length)*n+n*~n/2+~-t*~t/11+9

```

[Try it online!](https://tio.run/##LYxNDoIwFAav8i1c0PKAFn9Z1IsYEwhS0DSvBho2Rq5eJbqZWUwyj2Zupna8P0PG/tbFaA2bcxKMVlLWmxe/69x13IdBSE5ZLlyU6ZIFuYRC67SK1o9I5sbBW1wUQRNKwpawI@wJB8IRdCJU37Rm9cOf6irQep6863Ln@3VEsKuEiB8 "JavaScript (Node.js) – Try It Online")

There isn't any non-recursive implementation here. So maybe it is worth to include one. But it is longer than current answers...

If your language trunk divide result to int automatically, `+~-t*~t/11` could be changed into `-t*t/11`.

$$ t = 10^{1+\lfloor\log\_{10}n\rfloor} $$
$$ t\cdot n-\frac{n\cdot (n+1)}2-\frac{t^2-100}{11} $$

with special case where \$n=0\$
[Raku](https://raku.org/), 32 bytes
===================================

```
0,|[\+] {{S:g/./9/-$_}($++)}...*

```

[Try it online!](https://tio.run/##K0gtyjH7n1up4FCsYPvfQKcmOkY7VqG6OtgqXV9P31JfVyW@VkNFW1uzVk9PT@u/tUJxYqWCkkq8gq0dUEu0SnyskkJafpGCjYGCoYIhkDAAYwhhYPcfAA "Perl 6 – Try It Online")

This is an expression for the infinite sequence of sums.

* `{ ... } ... *` is a lazy, infinite sequence, where the brackets enclose an expression that generates each sequence element.
* `$++` postincrements an anonymous state variable each time the generating function is called, producing the numbers 0, 1, 2, .... Each time, that number is passed to the anonymous function enclosed in the inner brackets. (That saves us having to declare a variable to hold the counter value.)
* `S:g/./9/` converts every digit in the counter to `9`.
* `- $_` subtracts the counter from the previous value.
* `[\+]` produces the sequence of partial sums from the original sequence.
* `0, |` pastes a zero onto the front of the sequence.