Let us denote by Ai={a∈A∣a≡i(mod2k)} and by ai=∣Ai∣ for each i=0,…,2k−1. Observe that A=⋃i=02k−1Ai, where Ai∩Aj=∅ for i=j, and therefore 2n=∑i=02k−1ai.
For any a∈Ai, we have
Pa={b∈A∣b−a≡k(mod2k)}={b∈A∣b≡k+i(mod2k)}=Ak+i,
where the index k+i is considered modulo 2k. Hence, pa=ak+i, for every a∈Ai.
Thus,
EA=a∈A∑pa2=i=0∑2k−1a∈Ai∑pa2=i=0∑2k−1aiak+i2=i=0∑k−1aiak+i2+i=k∑2k−1aiak+i2.
Since the indices are modulo 2k,
i=k∑2k−1aiak+i2=i=0∑k−1ak+iai2,
so we get
EA=i=0∑k−1(aiak+i2+ak+iai2)=i=0∑k−1aiak+i(ai+ak+i).
We want to maximize EA under the constraint ∑i=02k−1ai=2n.
Note that replacing any two distinct pairs (ai,ai+k) and (aj,aj+k) by (ai+aj,ai+k+aj+k) and (0,0) does not decrease the value of EA, because
Therefore, the maximum is achieved when only one pair (ai,ai+k) is nonzero, with ai+ai+k=2n.
Using the inequality between arithmetic and geometric means, we have EA=aiai+k(ai+ai+k)≤4(ai+ai+k)3=2n3.
The maximum value 2n3 is attained, for example, by any set A having exactly n multiples of 2k and n numbers congruent to k(mod2k).