cfB - Art Union

B - Art Union
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  CodeForces 416B

Description

A well-known art union called "Kalevich is Alive!" manufactures objects d'art (pictures). The union consists of n painters who decided to organize their work as follows.

Each painter uses only the color that was assigned to him. The colors are distinct for all painters. Let's assume that the first painter uses color 1, the second one uses color 2, and so on. Each picture will contain all these n colors. Adding the j-th color to the i-th picture takes the j-th painter tij units of time.

Order is important everywhere, so the painters' work is ordered by the following rules:

  • Each picture is first painted by the first painter, then by the second one, and so on. That is, after the j-th painter finishes working on the picture, it must go to the (j + 1)-th painter (if j < n);
  • each painter works on the pictures in some order: first, he paints the first picture, then he paints the second picture and so on;
  • each painter can simultaneously work on at most one picture. However, the painters don't need any time to have a rest;
  • as soon as the j-th painter finishes his part of working on the picture, the picture immediately becomes available to the next painter.

Given that the painters start working at time 0, find for each picture the time when it is ready for sale.

Input

The first line of the input contains integers m, n (1 ≤ m ≤ 50000, 1 ≤ n ≤ 5), where m is the number of pictures and n is the number of painters. Then follow the descriptions of the pictures, one per line. Each line contains n integers ti1, ti2, ..., tin (1 ≤ tij ≤ 1000), wheretij is the time the j-th painter needs to work on the i-th picture.

Output

Print the sequence of m integers r1, r2, ..., rm, where ri is the moment when the n-th painter stopped working on the i-th picture.

Sample Input

Input
5 1
1
2
3
4
5
Output
1 3 6 10 15 
Input
4 2
2 5
3 1
5 3
10 1
Output
7 8 13 21 


#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int dp[50005][6],d[50005][6];
int main()
{
    int n,m;
     while(cin>>n>>m)
     {
         memset(dp,0,sizeof(dp));
         for(int i=0;i<n;i++)
         {
             for(int j=0;j<m;j++)
             {
                 scanf("%d",&d[i][j]);
                 if(i==0&&j==0)
                    dp[i][j]=d[i][j];
                else if(i==0&&j>=1)
                  dp[i][j]=dp[i][j-1]+d[i][j];
                else if(j==0&&i>=1)
                    dp[i][j]=dp[i-1][j]+d[i][j];
                else
                {
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1])+d[i][j];
                }
             }
         }
         cout<<dp[0][m-1];
         for(int i=1;i<n;i++)
         cout<<" "<<dp[i][m-1];
         cout<<endl;
     }
    return 0;
}


你可能感兴趣的:(cfB - Art Union)