Optimal stability for the inverse problem of multiple cavities