import java.util.ArrayList; import java.util.Deque; import java.util.LinkedList; import java.util.Scanner; public class NetworkPacket { static class Request { int arrivalTime; int processTime; Request(int arrivalTime, int processTime) { this.arrivalTime = arrivalTime; this.processTime = processTime; } } static class Response { boolean dropped; int startTime; Response(boolean dropped, int startTime) { this.dropped = dropped; this.startTime = startTime; } } static class Buffer { private int capacity; private Deque finishTime; // My addition to the starter file private int runningTimeCounter = 0; // End of my addition to the starter file Buffer(int capacity) { this.capacity = capacity; this.finishTime = new LinkedList<>(); } Response processRequest(Request request) { if (runningTimeCounter < request.arrivalTime) if (finishTime.size() == 0) runningTimeCounter = request.arrivalTime; else if (finishTime.getLast() < request.arrivalTime) runningTimeCounter = request.arrivalTime; while (finishTime.size() > 0) { int j = finishTime.getFirst(); if (j <= request.arrivalTime) finishTime.pop(); else break; } if (finishTime.size() >= capacity) { return new Response(true, -1); } Response response = new Response(false, runningTimeCounter); runningTimeCounter = runningTimeCounter + request.processTime; if (finishTime.size() > 0) finishTime.addLast(finishTime.getLast() + request.processTime); else finishTime.addLast(runningTimeCounter); return response; } } private static ArrayList ReadQueries(Scanner scanner) { int requests_count = scanner.nextInt(); ArrayList requests = new ArrayList<>(); for (int i = 0; i < requests_count; ++i) { int arrival_time = scanner.nextInt(); int process_time = scanner.nextInt(); requests.add(new Request(arrival_time, process_time)); } return requests; } static ArrayList processRequests(ArrayList requests, Buffer buf) { ArrayList responses = new ArrayList<>(); for (Request request : requests) { responses.add(buf.processRequest(request)); } return responses; } private static void PrintResponses(ArrayList responses) { for (Response response : responses) { if (response.dropped) { System.out.println(-1); } else { System.out.println(response.startTime); } } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int bufferMaxSize = scanner.nextInt(); Buffer buffer = new Buffer(bufferMaxSize); ArrayList requests = ReadQueries(scanner); ArrayList responses = processRequests(requests, buffer); PrintResponses(responses); } }