Hello
I would suggest using spray foam to fill the gaps
It expands and will be flexable
You can clean up the excess with a hacksaw blade
hi
this is the expandable foam it's made of poly propolene once it's hard you can cut off the excess with a hack saw blade
Yes you can paint over it
Electrician
former journeyman electrician /maintenance mechanic for 30 years