Optimizing Box Weights Hackerrank Java