416B - Art Union (DP)

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), where tij 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 test(s)
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 


题目大意:有n副画,m个画家,每幅画只能一次一个画家去画,并且按照顺序。画家画完当前的画后,可以不去休息的去画另一幅画

,问每幅画的最短完成时间。


思路:可以用DP做,dp[i][j]表示当前i幅画第j个画家画的所需的时间。


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
int a[50000][6], dp[50001][6];
int main()
{
    int n,m,x,k,i,j;
    int cla;
    while(~scanf("%d%d",&n,&m))
    {
        memset(dp,0,sizeof(dp));
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                scanf("%d",&a[i][j]);
        int tmp=0;
        for(i=1;i<=n;i++)//预处理一下
        {
            tmp+=a[i][1];//第一个画家完成每一幅画的时间
            dp[i][1]=tmp;
        }
        tmp=0;
        for(i=1;i<=m;i++)//第一幅画第i位画家完成所需要的时间
        {
            tmp+=a[1][i];
            dp[1][i]=tmp;
        }
        for(i=2;i<=n;i++)
        {
            for(j=2;j<=m;j++)
            {
                dp[i][j]=max(dp[i-1][j],dp[i][j-1])+a[i][j];
            }//当前的状态只有自己或者前者转化过来的。
        }
        for(i=1;i<n;i++)
        printf("%d ",dp[i][m]);
        printf("%d\n",dp[n][m]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值