As you have to minimize the score and maximize the penalty, you have to try to keep the score same as the position you are replacing and decrease from the penalty. Now, there might occur a case where the penalty is and it's not possible to decrease the penalty, then increase the score by which is the minimum possible and make the penalty which is the maximum penalty in this case. There is another case, where the scores and penalties are consecutive. For example, consider the two cases, and . Here in both cases, the answer is . There is another instance where the score might need to be greater than , in that case, answer is .