This page is still under construction.

Parts of this page are still being built. What you see may change.

Bank

Interview

Time limit2sMemory limit512 MB

Summary
Given n dwarves with arrival times, employee service times, and accountant times, simulate a shared employee queue of m servers and a single accountant to find each dwarf's departure time.
Level

Medium6 of 10

Topics
Simulation, Heap, Greedy, Sorting
Solved
No attempts yet

Problem

In a distant world, a new bank has opened in the glorious city of Erbovle. The bank has mm employees who serve clients and one chief accountant.

Dwarves come to the bank to take care of their business. The ii-th dwarf arrives at the bank tit_i minutes after it opens. First he must spend aia_i minutes with one of the mm employees, and then another bib_i minutes in the chief accountant's office.

Of course, several dwarves cannot be with the same employee or in the chief accountant's office at the same time, so queues form at the employees and at the chief accountant.

The queue for the employees is shared, and a dwarf from the queue goes to the first employee who becomes free. If two dwarves arrive at the bank at the same time, the one with the smaller index joins the employees' queue first. If a dwarf starts being served by an employee at time xx, he finishes at time x+aix+a_i, and at that time another dwarf may start being served by the same employee. A dwarf who arrives at the bank at time tt may start being served by an employee at any time from tt onward.

After finishing his business with an employee, the dwarf joins the queue for the chief accountant. Similarly, if two dwarves join this queue at the same time, the one with the smaller index goes first; when one dwarf's service ends, the next dwarf's service may start immediately; and a dwarf may go to the chief accountant from the moment he finishes being served by an employee.

Today nn dwarves are going to come to the bank. For each one, the time he enters the bank, how long he wants to spend at a window, and how long he wants to spend with the accountant are known. Report the time each dwarf leaves the bank.

Input

The first line contains two integers nn and mm (1≤n≤100 0001 \le n \le 100\,000, 1≤m≤101 \le m \le 10), the number of dwarves and employees, respectively. The next nn lines contain three integers each: tit_i, aia_i, and bib_i (1≤ti,ai,bi≤1091 \le t_i, a_i, b_i \le 10^9), the arrival time of the ii-th dwarf, the number of minutes the ii-th dwarf must spend with a bank employee, and the number of minutes he must spend in the chief accountant's office. The dwarves are given in order of arrival at the bank, that is, for every pair i<ji < j we have ti≤tjt_i \le t_j.

Output

Print nn integers; the ii-th number must be the number of minutes after opening when the ii-th dwarf leaves the bank.

Examples1

  1. Example 1

    Input
    4 2
    1 3 3
    1 2 2
    2 2 1
    2 1 4
    
    Expected output
    8
    5
    9
    13