Ultracold atoms in a cavity-mediated double-well system