27. [17] Suppose that $\left(a_{1}, \ldots, a_{20}\right)$ and $\left(b_{1}, \ldots, b_{20}\right)$ are two sequences of integers such that the sequence $\left(a_{1}, \ldots, a_{20}, b_{1}, \ldots, b_{20}\right)$ contains each of the numbers $1, \ldots, 40$ exactly once. What is the maximum possible value of the sum
$$
\sum_{i=1}^{20} \sum_{j=1}^{20} \min \left(a_{i}, b_{j}\right) ?
$$
Answer: 5530 Let $x_{k}$, for $1 \leq k \leq 40$, be the number of integers $i$ with $1 \leq i \leq 20$ such that $a_{i} \geq k$. Let $y_{k}$, for $1 \leq k \leq 40$, be the number of integers $j$ with $1 \leq j \leq 20$ such that $b_{j} \geq k$. It follows from the problem statement that $x_{k}+y_{k}$ is the number of elements of the set $\{1, \ldots, 40\}$ which are greater than or equal to 40 , which is just $41-k$.
Note that if $1 \leq i, j \leq 20$, and $1 \leq k \leq 40$, then $\min \left(a_{i}, b_{j}\right) \geq k$ if and only if $a_{i} \geq k$ and $b_{j} \geq k$. So for a fixed $k$ with $1 \leq k \leq 40$, the number of pairs $(i, j)$ with $1 \leq i, j \leq 20$ such that $\min \left(a_{i}, b_{j}\right) \geq k$ is equal to $x_{k} y_{k}$. So we can rewrite
$$
\sum_{i=1}^{20} \sum_{j=1}^{20} \min \left(a_{i}, b_{j}\right)=\sum_{k=1}^{40} x_{k} y_{k} .
$$

Since $x_{k}+y_{k}=41-k$ for $1 \leq k \leq 40$, we have
$$
x_{k} y_{k} \leq\left\lfloor\frac{41-k}{2}\right\rfloor\left\lceil\frac{41-k}{2}\right\rceil
$$
by a convexity argument. So
$$
\sum_{i=1}^{20} \sum_{j=1}^{20} \min \left(a_{i}, b_{j}\right) \leq \sum_{k=1}^{40}\left\lfloor\frac{41-k}{2}\right\rfloor\left\lceil\frac{41-k}{2}\right\rceil=5530 .
$$

Equality holds when $\left(a_{1}, \ldots, a_{20}\right)=(2,4, \ldots, 38,40)$ and $\left(b_{1}, \ldots, b_{20}\right)=(1,3, \ldots, 37,39)$.