I'm trying to maximise the following value:
$\left( \sum_{i=1}^{n} p_i \cdot i \right) - \left( \max_{j=1}^{n} p_j \cdot j \right)$
where $p$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order (a permutation of $\{1, 2, \dots, n\}$). I have to find an optimal permutation $p$.
A brute force approach in Python can allow us detect a pattern:
from __future__ import annotations # noqa
from itertools import permutations # noqa
if __name__ == "__main__":
for n in range(1, 12):
elements = range(1, n+1)
perms = permutations(elements, n)
best: int = 0
best_perm: list[int] | None = None
for perm in perms:
perm: list[int] = list(perm)
s = 0
max = 0
for i in range(0, n):
s += (i+1) * perm[i]
if (i+1) * perm[i] > max:
max = (i+1) * perm[i]
s -= max
if s >= best:
best = s
best_perm = perm
print(f"{n=}, {best_perm=}, {best=}")
Outputs:
n=1, best_perm=[1], best=0
n=2, best_perm=[2, 1], best=2
n=3, best_perm=[1, 3, 2], best=7
n=4, best_perm=[1, 4, 3, 2], best=17
n=5, best_perm=[1, 2, 5, 4, 3], best=35
n=6, best_perm=[1, 2, 3, 6, 5, 4], best=62
n=7, best_perm=[1, 2, 3, 7, 6, 5, 4], best=100
n=8, best_perm=[1, 2, 3, 4, 8, 7, 6, 5], best=152
n=9, best_perm=[1, 2, 3, 4, 5, 9, 8, 7, 6], best=219
n=10, best_perm=[1, 2, 3, 4, 5, 6, 10, 9, 8, 7], best=303
n=11, best_perm=[1, 2, 3, 4, 5, 6, 7, 11, 10, 9, 8], best=406
We can notice that the numbers in the permutation "go up", then "go down starting from $n$". It makes sense (small numbers in the beginning, and we can't put the largest number at the end since there is the $\max_{j=1}^{n} p_j \cdot j$ term subtracted in the function we are trying to maximise). Could it potentially fail for large values of $n$? However, I have thoroughly tested all computationally-feasible cases and have not discovered a counterexample.
I'm struggling to find a proof. This post is adapted from the Problem - C competition from codeforces that ended on the 12th of August 2023.
EDIT: I'm looking for a proof of the pattern, but there might be a proof to find an permutation that maximises the value we are trying to maximise, which would be even more satisfying.
This is not a full proof but I hope it provides some insight
If we assume the optimal solution is in form of
$p_i = \begin{cases} i & \text{if }~~i=1,2,...,m\\ n+m+1-i & \text{if}~~i=m+1,...,n \end{cases}$
for $i>m$, it we can show that $a_i = ip_i$ has maximum at $i=\lceil \frac{m+n}{2} \rceil$. This can be shown considering the fact that $a_i = ip_i$ is a concave function of $i$ and
$i^* = \min_{i>m} {a_{i+1}\le a_{i}}$.
$a_{i+1}\le a_i \Rightarrow (i+1)(n+m-i)\le i(n+m+1-i) \Rightarrow i\ge \frac{n+m}{2} \Rightarrow i^* = \lceil \frac{n+m}{2} \rceil$.
Also it is clear that $a_i>a_j$ if $i>m$ and $j\le m$. Therefore, $\max_{i \in \{1,2,..,n\}}{a_i} =\lceil \frac{n+m}{2} \rceil (n+m+1-\lceil \frac{n+m}{2} \rceil) =\begin{cases} (\frac{n+m}{2})(\frac{n+m}{2}+1) & \text{if }~~n+m~~\text{even}\\ (\frac{n+m+1}{2})^2 & \text{if}~~~~n+m~~\text{odd} \end{cases}$
Now, we try to find the the value of $m$ that maximizes our objective function
$f(m)=\sum_{i=1}^m i^2 + \sum_{i=m+1}^n a_i - a_{\lceil \frac{n+m}{2} \rceil}$. We further simplify as $f(m)=\sum_{i=1}^m i^2 - \sum_{i=m+1}^n i^2+ (n+m+1)\sum_{i=m+1}^n i - a_{\lceil \frac{n+m}{2} \rceil} = 2\sum_{i=1}^m i^2 - \sum_{i=1}^n i^2+ (n+m+1)\sum_{i=m+1}^n i - a_{\lceil \frac{n+m}{2} \rceil}$ $=\frac{m(m+1)(2m+1)}{3}-\frac{n(n+1)(2n+1)}{6}+\frac{(n+m+1)^2(n-m)}{2}-a_{\lceil \frac{n+m}{2} \rceil}$. It is easy to show that this function is also concave as
$\frac{\partial^2 f}{\partial m^2} = (4m+2)-(n+3m+2)-\frac{1}{2}=-(n-m+\frac{1}{2})<0$. So,
$m^* = min_m {f(m+1)\le f(m)}$.
$f(m+1)\le f(m) \Rightarrow 2\sum_{i=1}^{m+1} i^2 - \sum_{i=1}^n i^2+ (n+m+2)\sum_{i=m+2}^n i - a_{\lceil \frac{n+m+1}{2} \rceil} \le 2\sum_{i=1}^m i^2 - \sum_{i=1}^n i^2+ (n+m+1)\sum_{i=m+1}^n i - a_{\lceil \frac{n+m}{2} \rceil}$
$\Rightarrow 2(m+1)^2 - (n+m+1)(m+1) + \sum_{i=m+2}^n i + a_{\lceil \frac{n+m}{2} \rceil}-a_{\lceil \frac{n+m+1}{2} \rceil}\le 0$
$\Rightarrow -(n-m-1)(m+1) + \frac{(n+m+2)(n-m-1)}{2} - \lceil \frac{n+m+1}{2} \rceil\le 0$
$\text{if }~~n+m~~\text{even} \Rightarrow {(n-m)^2 -n+m} - 2 -n -m \le 0 \Rightarrow (n-m)^2 \le 2n+2) \rightarrow m\ge n-\sqrt{2n+2}$
$\text{if }~~n+m~~\text{odd} \Rightarrow {(n-m)^2 -n+m} - 1 -n -m \le 0 \Rightarrow (n-m)^2 \le 2n+1 \rightarrow m\ge n-\sqrt{2n+1}$
It can be shown that $m^* = n- \lfloor \sqrt{2n+1}\rfloor$ is the solution for any case. Hence, the optimal solution of proposed form is:
$p_i = \begin{cases} i & \text{if }~~i=1,2,...,n- \lfloor \sqrt{2n+1}\rfloor\\ 2n- \lfloor \sqrt{2n+1}\rfloor+1-i & \text{if}~~i=n- \lfloor \sqrt{2n+1}\rfloor+1,...,n \end{cases}$