If the sum is even and N is even it's easy because you can split the sum into pairs of equal numbers.
The only hard bit is if N is odd.
Then you need to split off three numbers that XOR to zero e.g. 1, 2 and 3
That leaves you with N-3 (which is even) numbers to sum to S-6