Intensity modulated proton therapy (IMPT) offers the possibility of generating excellent target coverage while sparing the neighbouring organs at risk. However, treatment plans optimized for IMPT may be very sensitive to range and setup uncertainties. We developed a method to deal with these uncertainties in the dose optimization. This method aims at two objectives: one for maintaining the dose coverage within the target, and the other for preventing undesired exposure to organs at risk. The former objective was achieved by the algorithm described in our previous paper to suppress the in-field dose gradient within the target. In this study, the latter objective was achieved by a novel algorithm in which we suppressed pencil beams with high risk to deliver undesired doses to organs at risk under conditions where range and setup uncertainties occur. We defined the risk index that quantifies the likelihood of each pencil beam delivering high doses to organs at risk, and introduced it into the objective function of dose optimizations. In order to test the algorithm's performance, this method was applied to an RTOG benchmark phantom geometry and to a cervical chordoma case. These simulations demonstrated that our method provides IMPT plans that are more robust against range and setup errors compared to conventional IMPT plans. Compared to the conventional IMPT plan, the optimization time for the robust plan increased by a factor of only 3, from 4 to 11 min.