public class Gifts{ private static int findUpperBound(int giftPrices[], int shippingPrices[]){ int upperBound = 0; for(int i = 0; i <= giftPrices.length - 1; i ++) { upperBound = upperBound + giftPrices[i] + shippingPrices[i]; } return upperBound; } private static int findMaxCows(int upperBound, int giftPrices[], int shippingPrices[], int B){ if (upperBound > B){ for(int i = 0; i <= giftPrices.length - 2; i ++) { upperBound = upperBound - (giftPrices[i]) + shippingPrices[i]; } return findMaxCows(upperBound, giftPrices, shippingPrices, B); } else{ return giftPrices.length; } } public static void main(String[] args){ int B = 24; int giftPrices[] = {4, 2, 8, 6, 12}; int shippingPrices[] = {2, 0, 1, 3, 5}; int rawResult = findUpperBound(giftPrices, shippingPrices); int result = findMaxCows(rawResult, giftPrices, shippingPrices, B); System.out.println(result-1); } }