Comments

Try thinking about $$$dp_{i,s} := \textrm{the number of possible suffixes of the array starting at } i\textrm{ having } a_{i+1} \textrm{ equal to } s$$$.

The transition will be something like $$$dp_{i,s} = dp_{i+1,a_{i+1}+s} + dp_{i+1, a_{i+1}-s}$$$ depending on whether $$$s = 0$$$.

I did it using binary search on the number of people we rank at least as low as. So to beat $$$k$$$ people, I pick the $$$k$$$ weakest opponents (in terms of $$$a_i$$$), if their sum of $$$a_i$$$ is less than $$$m$$$, we can rank $$$n-k+1$$$, there's also another small detail but this is mostly the idea.

There are $$$n^2-1$$$ possible differences $$$(1\dots n^2-1)$$$. And the chain $$$1, n^2, 2, n^2-1, \dots, \left\lfloor\dfrac{n^2}{2}\right\rfloor, \left\lfloor\dfrac{n^2}{2}\right\rfloor+1$$$ (chain ending correct modulo parity), contains all differences and has $$$n^2$$$ terms, for $$$n^2-1$$$ differences.

Or just use pairs of pairs of pairs, and negative numbers to find a way to use the default comparator.

For D, we can consider only prime divisors of $$$y-x$$$ because to minimize the answer, if it's possible to get $$$\gcd(x,y) \neq 1$$$ for a certain composite $$$d$$$ s.t. $$$d|(y-x)$$$, it's definitely possible for some prime $$$p \lt d$$$ and we can reach multiples of $$$p$$$ at least as soon as we reach a multiple of $$$d$$$.