Optimas: Optimizing Compound AI Systems with Globally Aligned Local Rewards