#P3749. [六省联考 2017] 寿司餐厅

    ID: 1326 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2017各省省选广度优先搜索,BFS深度优先搜索,DFS最大流最小割

[六省联考 2017] 寿司餐厅

Description

Kiana has recently been dining at a very delicious sushi restaurant.

Every evening, the restaurant serves nn types of sushi in order. The ii-th sushi has a code aia_i and a deliciousness di,id_{i, i}. Different types of sushi may share the same code. Each type has an unlimited number of servings, and Kiana can take sushi an unlimited number of times; however, in each take she can take at most one serving of each type, and the taken sushi in a single take must form a contiguous segment in the serving order. That is, Kiana may take one serving each of types 11 and 22 in one take, or types 22 and 33 in one take, but she may not take types 11 and 33 together in a single take.

Because there are many types and they may interact: salmon and squid together might be great, but together with fruit sushi might cause a stomachache. Therefore, Kiana defines a comprehensive deliciousness di,jd_{i, j} for i<ji < j, meaning that if, in a single take, she includes all sushi from the ii-th to the jj-th served by the restaurant, then after eating all sushi taken in that take she will gain an extra deliciousness of di,jd_{i, j}. Since taking sushi takes some time, we assume that sushi taken in different takes do not interact. Note that when eating sushi from a single take, more than one comprehensive deliciousness will be added. For example, if Kiana takes one serving each of types 1,2,31, 2, 3 in a single take, then besides d1,3d_{1, 3}, d1,2d_{1, 2} and d2,3d_{2, 3} will also be added to the total deliciousness.

Magically, Kiana’s evaluation of food has memory: whether it is the deliciousness of a single type of sushi or the comprehensive deliciousness of a combination, each will be added to Kiana’s total deliciousness at most once. For example, if Kiana once takes one serving each of types 11 and 22, and another time takes one serving each of types 22 and 33, then the total deliciousness of these two takes is $d_{1, 1} + d_{2, 2} + d_{3, 3} + d_{1, 2} + d_{2, 3}$, where d2,2d_{2, 2} is counted only once.

Strangely, the restaurant’s pricing policy is unusual. Specifically, if Kiana has eaten in total c (c>0)c \ (c > 0) kinds of sushi with code xx, she must pay mx2+cxm x^2 + c x yuan for those sushi, where mm is a constant given by the restaurant.

Now Kiana wants to know, when eating at this restaurant, what is the maximum value of the total deliciousness (including the deliciousness of all eaten single types and all accumulated comprehensive deliciousness) minus the total amount of money paid. Since she cannot compute it, she asks you to tell her.

Input Format

The first line contains two positive integers n,mn, m, denoting the total number of sushi types served by the restaurant and the constant used in pricing.
The second line contains nn positive integers, where the kk-th number aka_k is the code of the kk-th sushi.
Then follow nn lines. The ii-th of these lines contains ni+1n - i + 1 integers, where the jj-th number is di,i+j1d_{i, i + j - 1}, representing the corresponding deliciousness obtained by eating the sushi, as described above.

Output Format

Output a single line containing a positive integer, which is the maximum value of the total deliciousness Kiana can obtain minus the total amount of money paid.

3 1
2 3 2
5 -10 15
-10 15
15
12
5 0
1 4 1 3 4
50 99 8 -39 30
68 27 -75 -32
70 24 72
-10 81
-95
381
10 1
5 5 4 4 1 2 5 1 5 3
83 91 72 29 22 -5 57 -14 -36 -3
-11 34 45 96 32 73 -1 0 29
-48 68 44 -5 96 66 17 74
88 47 69 -9 2 25 -49
86 -9 -77 62 -10 -30
2 40 95 -74 46
49 -52 2 -51
-55 50 -44
72 22
-68
1223

Hint

Sample Explanation 1

In this sample, the restaurant serves 33 sushi in total, with codes a1=2,a2=3,a3=2a_1 = 2, a_2 = 3, a_3 = 2, and the constant for pricing is m=1m = 1.

Under the requirement that each take must yield new deliciousness, Kiana has 1414 distinct ways to eat. Some of them are listed below:

  1. Kiana eats nothing. Then her total deliciousness and total payment are both 00, and their difference is also 00.
  2. Kiana takes sushi exactly once, and only takes the 11-st sushi, i.e., her takes are {[1,1]}\{[1, 1]\}. Then the total deliciousness is 55, the total payment is 1×22+1×2=61 \times 2^2 + 1 \times 2 = 6, and the difference is 1-1.
  3. Kiana takes sushi twice: the first time the 11-st and 22-nd sushi, and the second time the 22-nd and 33-rd sushi, i.e., her takes are {[1,2],[2,3]}\{[1, 2], [2, 3]\}. Then the total deliciousness is 5+(10)+15+(10)+15=155 + (-10) + 15 + (-10) + 15 = 15, the total payment is $(1 \times 2^2 + 2 \times 2) + (1 \times 3^2 + 1 \times 3) = 20$, and the difference is 5-5.
  4. Kiana takes sushi twice: the first time the 11-st sushi, and the second time the 33-rd sushi, i.e., her takes are {[1,1],[3,3]}\{[1, 1], [3, 3]\}. Then the total deliciousness is 5+15=205 + 15 = 20, the total payment is 1×22+2×2=81 \times 2^2 + 2 \times 2 = 8, and the difference is 1212.

Among the 1414 plans, the unique optimal plan is the last one listed, where the maximum value of total deliciousness minus total payment is 1212.

Constraints

For all testdata, it is guaranteed that 500di,j500-500 \leq d_{i, j} \leq 500.

Additional special constraints on the data are as follows:

Case # nn aia_i mm Additional constraint
1 2\leq 2 30\leq 30 =0= 0 -
2 =1= 1
3 3\leq 3 =0= 0
4 =1= 1
5 5\leq 5 =0= 0
6 =1= 1
7 10\leq 10 =0= 0 All aia_i are the same
8 =1= 1 -
9 15\leq 15 =0= 0 All aia_i are the same
10 =1= 1 -
11 30\leq 30 1000\leq 1000 =0= 0 All aia_i are the same
12 30\leq 30
13 1000\leq 1000 -
14 =1= 1
15 50\leq 50 =0= 0 All aia_i are the same
16 30\leq 30
17 1000\leq 1000 -
18 =1= 1
19 100\leq 100 =0= 0
20 =1= 1

Translated by ChatGPT 5