I have a long list of "invoices" like so......

10.00

10.00

20.00

25.00

30.00

20.00

5.00

etc....

Now, i have received a payment for 40.00 - I need excel to work out how i can match 40.00 from that list of invoices. there are several ways i can get to 40.00 like: using both 20.00's - how can i ask excel to work out what i can allocate the 40.00 to but also to prioritise the "invoices" further up in the column. if i get the answer i'd like to suggest using the two 10's and the 20.00 just below it. not use the two 20.00's

Thanks in advance for any responses.