Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayDeque;
- import java.util.Scanner;
- public class RapidCourier {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] input = scanner.nextLine().split(" ");
- String[] courier = scanner.nextLine().split(" ");
- ArrayDeque<Integer> packages = new ArrayDeque<>();
- ArrayDeque<Integer> couriers = new ArrayDeque<>();
- for (String w : input) {
- packages.add(Integer.parseInt(w));
- }
- for (String c : courier) {
- couriers.add(Integer.parseInt(c));
- }
- int total = 0;
- while (!packages.isEmpty() && !couriers.isEmpty()) {
- int weight = packages.peek();
- int capacity = couriers.peek();
- if (capacity >= weight) {
- couriers.poll(); // Remove the courier from the queue
- total += weight;
- packages.poll(); // Remove the package from the queue
- } else {
- int remaining = weight - capacity;
- total += capacity;
- packages.poll(); // Remove the package from the queue
- couriers.poll(); // Remove the courier from the queue
- packages.addFirst(remaining); // Add remaining weight back to the queue
- }
- }
- System.out.printf("Total weight: %d kg\n", total);
- if (packages.isEmpty()) {
- System.out.println("Congratulations, all packages were delivered successfully by the couriers today.");
- } else if (couriers.isEmpty()) {
- System.out.print("Unfortunately, there are no more available couriers to deliver the following packages: ");
- StringBuilder remainingPackages = new StringBuilder();
- while (!packages.isEmpty()) {
- remainingPackages.append(packages.poll());
- if (!packages.isEmpty()) {
- remainingPackages.append(", ");
- }
- }
- System.out.println(remainingPackages);
- } else {
- System.out.println("Couriers are still on duty: " + couriers.toString() + " but there are no more packages to deliver.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement