Give everyone T-Shirts
Perl 6, 77 bytes



-&gt;$,\t,\p{flat(1..*Zxx t)[[p].&amp;{@(.sort)=0..*;$_}].&amp;{|$_,(p Z-$_)».abs.sum}}


Try it online!

Simply matches the n'th smallest T-Shirt to the person with the n'th smallest size.

I'm not absolutely sure if that is guaranteed to give the correct result for all possible inputs, but it works for the test-cases given in the task description.



Perl 6, 97 bytes



-&gt;\d,\t,\p{flat(1..*Zxx t).permutations.map({|$_,(p Z-$_)».&amp;{$_&gt;d??∞!!.abs}.sum}).min(*[*-1])}


Try it online!

Dumb brute-force solutions.
Takes around 15 minutes for the test-cases with 10 people, so the test-case with 20 people should only take, uh, millions of years... :D

(Memory usage should remain steady though, because permutations and map return lazy one-off sequences, and min also only needs to keep one element in memory.)



Both versions take three input arguments:


The number D.
The array for the T-Shirts.
The array for the people.