package RegularExam; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Scanner; import java.util.function.Consumer; public class T01 { private static int deliveredWeight; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayDeque packagesWeight = new ArrayDeque<>(); fillArrayDeque(scanner, packagesWeight::add); ArrayDeque couriersCapacity = new ArrayDeque<>(); fillArrayDeque(scanner, couriersCapacity::offer); while (!packagesWeight.isEmpty() && !couriersCapacity.isEmpty()) { int weight = packagesWeight.pollLast(); int capacity = couriersCapacity.pollFirst(); if (capacity >= weight) { deliveredWeight += weight; int newCapacity = capacity - (weight * 2); if (newCapacity > 0) { couriersCapacity.offerLast(newCapacity); } } else { deliveredWeight += capacity; packagesWeight.offerLast(weight - capacity); } } System.out.printf("Total weight: %d kg\n", deliveredWeight); if (packagesWeight.isEmpty() && couriersCapacity.isEmpty()) { System.out.println("Congratulations, all packages were delivered successfully by the couriers today."); } else { if (!packagesWeight.isEmpty()) { System.out.print("Unfortunately, there are no more available couriers to deliver the following packages: "); System.out.println(packagesWeight.toString().replaceAll("[\\[\\]]", "")); } if (!couriersCapacity.isEmpty()) { System.out.print("Couriers are still on duty: "); System.out.println(couriersCapacity.toString().replaceAll("[\\[\\]]", "") + " but there are no more packages to deliver."); } } } public static void fillArrayDeque(Scanner scanner, Consumer insertion) { Arrays.stream(scanner.nextLine().split("\\s+")) .map(Integer::parseInt) .forEach(insertion); } }