Q: how the heck can you remember a melody from a dream???!?!
A: it has to be the last thing that happened before you wake up. then you gotta write it down immediately using either audio recording or notation, whichever you feel is easier/quicker for you to get the necessary details across