CF1826E.Walk the Runway

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A fashion tour consists of mm identical runway shows in different cities. There are nn models willing to participate in the tour, numbered from 11 to nn . People in different cities have different views on the fashion industry, so they rate each model differently. In particular, people in city ii rate model jj with rating ri,jr_{i, j} .

You are to choose some number of kk models, and their order, let the chosen models have indices j1,j2,,jkj_1, j_2, \dots, j_k in the chosen order. In each city, these kk models will walk the runway one after another in this order. To make the show exciting, in each city, the ratings of models should be strictly increasing in the order of their performance. More formally, for any city ii and index tt ( 2tk2 \leq t \leq k ), the ratings must satisfy ri,jt1<ri,jtr_{i,j_{t - 1}} < r_{i,j_t} .

After all, the fashion industry is all about money, so choosing model jj to participate in the tour profits you pjp_j money. Compute the maximum total profit you can make by choosing the models and their order while satisfying all the requirements.

输入格式

The first line contains two integers mm and nn ( 1m5001 \leq m \leq 500 , 1n50001 \leq n \leq 5000 ) — the number of shows and the number of models willing to participate respectively.

The second line contains nn integers pjp_j ( 1pj1091 \leq p_j \leq 10^9 ) — the profit you get inviting the jj -th model to the tour.

The next mm lines each contain nn integers. Line number ii contains nn integers ri,jr_{i, j} ( 1ri,jn1 \leq r_{i, j} \leq n ) — the ratings of models in city ii .

输出格式

Output a single integer — the largest total amount of money you can get.

输入输出样例

  • 输入#1

    3 5
    10 10 10 10 10
    1 2 3 4 5
    1 5 2 3 4
    2 3 4 5 1

    输出#1

    30
  • 输入#2

    3 5
    10 10 10 10 50
    1 2 3 4 5
    1 5 2 3 4
    2 3 4 5 1

    输出#2

    50
  • 输入#3

    1 1
    1000000000
    1

    输出#3

    1000000000
  • 输入#4

    5 5
    1000000000 1000000000 1000000000 1000000000 1000000000
    5 4 3 2 1
    5 4 3 2 1
    5 4 3 2 1
    5 4 3 2 1
    5 4 3 2 1

    输出#4

    5000000000
  • 输入#5

    1 3
    1 2 3
    3 3 3

    输出#5

    3

说明/提示

In the first example, there are 33 invited models. The show consists of models in the order [1,3,4][1, 3, 4] .

Then, the corresponding ratings in the cities are as follows:

  • City 11[1,3,4][ 1, 3, 4 ] .
  • City 22[1,2,3][ 1, 2, 3 ] .
  • City 33[2,4,5][ 2, 4, 5 ] .

You can see that the ratings are increasing. So the total profit is 10+10+10=3010 + 10 + 10 = 30 . It can be proven that we can't achieve a bigger profit.

In the second example, we can invite the fifth model to the tour, which would result in a total profit of 5050 . It can be proven that we can't achieve a bigger profit.

In the third example, we invite the single model to the tour, which results in a total profit of 10000000001\,000\,000\,000 .

In the fourth test case, we can invite all the models and make the show in the order [5,4,3,2,1][ 5, 4, 3, 2, 1 ] . The total profit is 51000000000=50000000005 \cdot 1\,000\,000\,000 = 5\,000\,000\,000 .

首页