In a sequence of positive integers, an inversion is a pair of positions where the element in the leftmost position is greater than the element in the rightmost position. For example, the sequence 2,5,3,1,3 has 5 inversions: between the first and the fourth position, between the second and all subsequent positions, and finally, between the third and the fourth. Among all sequences of positive integers whose sum of elements is $n$, what is the maximum number of inversions if

a) $n=7$?

b) $n=2019$?

Note: The sequences of positive integers considered in this problem can have more than 5 elements.
Solution

a) First, we need to show that any sequence that maximizes the number of inversions must be non-increasing. Indeed, if there exists a pair of consecutive numbers \(a\) and \(b\), with \(a < b\), we can swap them. This swap increases the number of inversions by at least one, since the pair \((a, b)\) itself is an inversion. Therefore, any sequence that maximizes the number of inversions must be non-increasing.

b) Now, consider a sequence that maximizes the number of inversions and has the sum of its elements equal to 2019. We can assume that there are \(a\) elements equal to 2 and \(2019 - 2a\) elements equal to 1. The number of inversions is

\[
\begin{aligned}
a(2019 - 2a) & = 2019a - 2a^2 \\
& = \frac{2019^2}{8} - 2\left(a - \frac{2019}{4}\right)^2
\end{aligned}
\]

To maximize the expression above, we need to minimize \(|a - 2019 / 4|\), which occurs for \(a = 505\). Therefore, the maximum number of inversions is \(505 \cdot 1009\).