First integer of input refers to the total weight the ship can carry. Second integer refers to the number of cargo present and the rest of the integers represents the individual weight of the cargo
For output, all integers except the last are the individual weight of the cargo in order of their previous input and the final integer represents the total weight
Input 99 7 50 40 2 3 8 13 24
Output 50 40 8 98
Input 62 7 50 40 2 3 8 13 24
Output 50 3 8 61
Input 23 6 8 12 13 5 6 4
Output 8 5 6 4 23