Without knowing more details of the size of the mast and the physical properties of the proposed foam, I can not say with any certainty that filling the mast with foam would work or not. Even if it helped the mast float I think it would cause other problems. That volume of foam could be somewhat heavy and make the mast more difficult to raise. Additionally I think it would raise the center of gravity of the boat making it more likely to capsize.
There are a number of commercially produced mast floats out there. There is a float that is designed for Hobie Cat's that I have seen retrofitted onto a Precision 15 mast which I think would be a better option than filling the mast with foam. Google search Hobie Mast Float for some options.
Besides the Hobie "football" on the top of the mast, Precision sells a more subtle float that goes on the top of the mainsail and is intended to serve the same purpose. I don't think it's more expensive.