Added optimizations for when sum is greater than max sum possible, and when x is lower than least sum possible