Properly selected beam angles contribute to the quality of radiotherapy treatment plans. However, the beam angle optimization (BAO) problem is difficult to solve to optimality due to its non-convex discrete nature with many local minima. In this study, we propose TBS-BAO, a novel approach for solving the BAO problem, and test it for non-coplanar robotic CyberKnife radiotherapy for prostate cancer. First, an ideal Pareto-optimal reference dose distribution is automatically generated using a priori multi-criterial fluence map optimization (FMO) to generate a plan that includes all candidate beams (total-beam-space, TBS). Then, this ideal dose distribution is reproduced as closely as possible in a subsequent segmentation/beam angle optimization step (SEG/BAO), while limiting the number of allowed beams to a user-selectable preset value. SEG/BAO aims at a close reproduction of the ideal dose distribution. For each of 33 prostate SBRT patients, 18 treatment plans with different pre-set numbers of allowed beams were automatically generated with the proposed TBS-BAO. For each patient, the TBS-BAO plans were then compared to a plan that was automatically generated with an alternative BAO method (Erasmus-iCycle) and to a high-quality manually generated plan. TBS-BAO was able to automatically generate plans with clinically feasible numbers of beams (∼25), with a quality highly similar to corresponding 91-beam ideal reference plans. Compared to the alternative Erasmus-iCycle BAO approach, similar plan quality was obtained for 25-beam segmented plans, while computation times were reduced from 10.7 hours to 4.8/1.5 hours, depending on the applied pencil-beam resolution in TBS-BAO. 25-beam TBS-BAO plans had similar quality as manually generated plans with on average 48 beams, while delivery times reduced from 22.3 to 18.4/18.1 min. TBS reference plans could effectively steer the discrete non-convex BAO.