It does not look as if I am going to get any takers. So, to whom it may concern, here is the answer.
Each weight can be in one of 3 places: on one pan, on the other pan, or off of the scale. The total number of arrangements is thus 3*3*3*3 = 81. We can eliminate the configuration that has all weights off of the scale. Each other configuration occurs twice. For any configuration, an equivalent configuration can be had by swapping the weights in the two pans. This gives (81 – 1)/2 = 40 configurations. Note that this will only be true if the 4 weights are all different. If not, there will be fewer configurations, but then there will not be enough to cover the 40 weights from 1 to 40.
Since there are only 40 arrangments for 40 total weights, there can be no two configurations with the same weight. This will prove useful.
We know that all four weights have to be in one pan to get 40 grams. How could we get 39 grams? The only way would be if the smallest weight is 1 gram, so one of weights must be 1 gram.
What is the smallest that the next smallest weight could be? It can’t be 2 grams, because having 2 grams in one pan and the 1 gram in the other would give us a second way of getting 1 gram, but every configuration must have a unique weight. The smallest weight would be 3 grams. Let’s work with that.
A 1 gram weight and a 3 gram weight produce all weights from 1 to 4 grams. What is then next smallest weight that can be used? In order to avoid duplicate weights the next smallest weight would have to be 9 grams. We can now produce all weights from 1 to 13 grams. Also notice we have what looks like a pattern: 1, 3, 9.
The next smallest weight would have to be 27 grams and this will give us all weights from 1 to 40 grams and so is the solution. It also follows the pattern: 1, 3, 9, 27.
One way of seeing how this all works is to look at things as follows. Imagine all 4 weights on one pan. Suppose we want to get a weight of 24 grams. We need to subtract 16 grams from the 40. In base 3, 16 is represented as 121, that is 16 = 3^2 + 2*3 +1 = 9 + 2*3 + 1. To subtract 9 grams, simply remove the 9 gram weight from the scale. To subtract 2*3 grams, remove the 3 gram weight, thus removing 3 grams, and place it on the other pan, subtracting another 2 grams. Finally, remove the 1 gram weight from the scale. So we have the 37 gram wieght on one pan and the 3 gram weight on the other for a net of 24 grams.