Where should I invest the most for UE4? GPU, CPU or RAM?

I would recommend you to invest in CPU. Either when you compiling ~7k shaders for new map, or when you are building lightmap for a few hours, your CPU will be used in 100%.