P18108 [COCI 2026/2027 #2] Posao

Description

A company has five employees, labelled 1 to 5. During the day, $n$ jobs arrive at the company, labelled 1 to $n$. Job $i$ arrives at moment $t_i$, its processing takes $u_i$ seconds, and its review takes $v_i$ seconds. Time is measured in seconds, and all events happen at integer moments. ![](https://cdn.luogu.com.cn/upload/image_hosting/0majun2q.webp) - Employee 1 receives the jobs and keeps them in a queue, ordered by the moment of arrival, and jobs that arrived at the same moment by their labels. As soon as employee 2 or employee 3 is free, employee 1 gives him the first job from the queue: to employee 2 if he is free, and otherwise to employee 3. - Employees 2 and 3 process the jobs. An employee who gets job $i$ at moment $T$ processes it until moment $T+u_i$. He then hands a report on that job to employee 4 and is free again. - Employee 4 keeps the received reports in his queue. He forwards all the reports from the queue at once, as a single group, to employee 5 (after which his queue is empty) in each of the following cases: - (a) the queue contains exactly $s$ reports, - (b) the queue is not empty, and he has not received any report for $c$ seconds, - (c) the queue is not empty, and employees 2 and 3 are both free. - Employee 5 reviews the groups one by one, in the order in which he received them. Reviewing a group takes him as many seconds as the sum of the values $v_i$ of all jobs in that group. At the moment he finishes reviewing a group, all jobs in that group are approved. Employee 5 sleeps whenever he has no work, and it takes him $b$ seconds to wake up. More precisely, let $K$ be the moment at which employee 5 finishes reviewing all the groups he has received so far. If he receives a group at moment $G>K$, or if it is the first group he has received, its review starts at moment $G+b$. Otherwise, the review of that group starts at moment $K$. Handing over jobs, reports and groups takes no time. Events that happen at the same moment $T$ take place in the following order: 1. If employee 2 finishes processing a job at moment $T$, his report enters the queue of employee 4. If the queue then contains exactly $s$ reports, employee 4 immediately forwards the group. Then the same is done for employee 3. 2. All jobs with $t_i=T$ enter the queue of employee 1. 3. While the queue of employee 1 is not empty and at least one of employees 2 and 3 is free, employee 1 gives away the first job from the queue. An employee who finished processing a job at moment $T$ is already free at that moment. 4. If the queue of employee 4 is not empty, and employees 2 and 3 are both free, employee 4 forwards the group. Then, if the queue is still not empty and $T-P\ge c$ holds, where $P$ is the moment at which employee 4 received his last report, employee 4 forwards the group. Determine the moment at which employee 5 finishes reviewing the last group and, for each job, the moment at which that job is approved.

Input Format

The first line contains positive integers $n$, $s$, $c$ and $b$ ($1\le n,s\le 200\,000$, $1\le c,b\le 10^9$), the number of jobs and the numbers described in the task statement. The $i$-th of the following $n$ lines contains positive integers $t_i$, $u_i$ and $v_i$ ($1\le t_i,u_i,v_i\le 10^9$), the moment at which job $i$ arrives, the duration of its processing and the duration of its review. It holds that $t_1\le t_2\le\cdots\le t_n$.

Output Format

In the first line, output the moment at which employee 5 finishes reviewing the last group. In the second line, output $n$ numbers, where the $i$-th number is the moment at which job $i$ is approved.

Explanation/Hint

### Explanation of example 1 - $T=1$: employee 2 gets job 1 and processes it until moment 4. - $T=2$: employee 2 is busy, so employee 3 gets job 2 and processes it until moment 6. - $T=4$: employee 4 receives the report on job 1. Employee 3 is still processing a job, so the group is not forwarded. - $T=6$: employee 4 receives the report on job 2. Employees 2 and 3 are free, so by rule (c) the group with jobs 1 and 2 is forwarded. Employee 5 is asleep, so he is waking up until moment 8, and the review takes $2+5=7$ seconds, until moment 15. - $T=9$: employee 2 gets job 3 and processes it until moment 11. - $T=11$: employee 4 receives the report on job 3 and by rule (c) forwards the group with job 3. Employee 5 is still reviewing the previous group, so the review of the new group starts at moment 15 and ends at moment 16. ### Explanation of example 2 - $T=1$: jobs 1, 2, 3 and 4 arrive. Employee 2 gets job 1, and employee 3 gets job 2; both process them until moment 3. - $T=3$: employee 4 receives the reports on jobs 1 and 2. The queue then contains $s=2$ reports, so by rule (a) the group is forwarded. Employee 5 is waking up until moment 7 and reviews the group until moment 27. Employees 2 and 3 get jobs 3 and 4 and process them until moment 6. - $T=6$: by rule (a), the group with jobs 3 and 4 is forwarded. Employee 5 is still waking up, so this group waits. Employee 2 gets job 5 and processes it until moment 7. - $T=7$: by rule (c), the group with job 5 is forwarded. - Employee 5 reviews the second group from moment 27 until moment 29, and the third one until moment 33. ### Explanation of example 3 - Employee 2 gets job 1 at moment 1 and processes it until moment 21. Employee 3 processes job 2 from moment 2 until moment 4, and job 3 from moment 6 until moment 8. - $T=8$: the report on job 3 arrives exactly $c=4$ seconds after the report on job 2. The report first enters the queue, so $P=8$ and the group is not forwarded. - $T=12$: it holds that $T-P=4\ge c$, so by rule (b) the group with jobs 2 and 3 is forwarded. Employee 5 is waking up until moment 19 and reviews the group until moment 22. - Employee 3 processes job 4 from moment 14 until moment 19. At moment 21, employee 4 receives the report on job 1, and jobs 5 and 6, which arrive at that moment, are immediately given to employees 2 and 3, so the group is not forwarded. - $T=22$: after the report of employee 2 on job 5, the queue contains $s=3$ reports, so by rule (a) the group with jobs 4, 1 and 5 is forwarded. Employee 5 finishes reviewing the previous group at exactly that moment, so there is no waking up and the review lasts until moment 30. Then the report of employee 3 on job 6 arrives, and by rule (c) the group with job 6 is forwarded; its review lasts from moment 30 until moment 32. - Job 7 is processed from moment 40 until moment 42, when by rule (c) the group with that job is forwarded. Employee 5 has been asleep since moment 32, so he is waking up until moment 49 and reviews the group until moment 54. ### Scoring If, for some test case, the first line of the output is correct and the second one is not, the solution scores half of the points on that test case. The number of points earned on a subtask is the subtask’s point value multiplied by the minimum fraction of points earned over all test cases in that subtask. | Subtask | Points | Constraints | | --- | --- | --- | | 1 | 16 | $s=1$ | | 2 | 24 | $u_i\le c$ for each $i=1,2,\ldots,n$ | | 3 | 28 | $n,c,b\le 1000$ and $t_i,u_i,v_i\le 1000$ for each $i=1,2,\ldots,n$ | | 4 | 42 | No additional constraints. |