Flow Shop

Time limit6sMemory limit512 MB

Summary
Given N jobs processed through M stages in the same order with per-job times, and a smallest-label-first queue rule at each stage, find each job's finish time.
Level

Medium7 of 10

Topics
Simulation, Queue, Implementation
Solved
No attempts yet

Problem

A workshop builds custom swathers, the machines used to cut grain. Every swather goes through the same sequence of stages: a cutting bar is mounted, a grain belt is installed, a reel is fitted. The parts are customized for each buyer, so the same stage can take a different amount of time on different swathers.

NN swathers have been ordered and the manufacturing process has MM stages. Every swather passes through the stages in the same order, from stage 1 to stage MM.

Stage jj takes Pi,jP_{i,j} units of time on swather ii. The workers at a stage handle one swather at a time and never interrupt a job once they have started it. At time 0 all NN orders are waiting at stage 1. Whenever the workers at stage jj are idle and at least one swather is waiting there, they take the waiting swather with the smallest label; the swathers are labelled 1 to NN. Stage jj can start on a swather only after stage j−1j-1 has finished that same swather.

Determine the time at which each swather is completed.

Input

The first line contains two integers NN and MM (1≤N,M≤10001 \le N, M \le 1000), the number of swathers and the number of stages. Each of the next NN lines contains MM integers. The jj-th integer on the ii-th line is Pi,jP_{i,j} (1≤Pi,j≤1061 \le P_{i,j} \le 10^6).

Output

Print one line with NN integers T1 T2 … TNT_1\ T_2\ \dots\ T_N, separated by single spaces, where TiT_i is the time at which stage MM is completed for swather ii.

Examples2

  1. Example 1

    Input
    2 3
    1 2 3
    3 2 1
    
    Expected output
    6 7
    
  2. Example 2

    Input
    3 2
    3 1
    4 7
    2 5
    
    Expected output
    4 14 19