Can I first and foremost suggest that stuffing as many ads as you can into the space is going to look blatant, and annoying, to most people.
That said.
What do you do if NONE of the ads will fit?
How do you know what size the ads are?
How do you determine which ad to show first?
Once you have enough information in the javascript, it’s certainly doable. For that matter, it’s probably doable with CSS if your ads, header, and footer are a fixed size…