Since the weight can be put on either pan, any given weight can contribute 3 values +w, 0, -w. The weights need to be chosen to be a factor of 3 of each other:
.. 1 kg, 3 kg, 9 kg, 27 kg.
The base-3 version of the numbers 1 to 40 tell you where to put the weights.
If we use the code - = weight not used, o = weight opposite the cake, w = weight with the cake, then we can make a table, as in the attachment. Left-to-right the corresponding weights are 27 kg, 9 kg, 3 kg, 1 kg.