CF75D.Big Maximum Sum

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Ahmed and Mostafa used to compete together in many programming contests for several years. Their coach Fegla asked them to solve one challenging problem, of course Ahmed was able to solve it but Mostafa couldn't.

This problem is similar to a standard problem but it has a different format and constraints.

In the standard problem you are given an array of integers, and you have to find one or more consecutive elements in this array where their sum is the maximum possible sum.

But in this problem you are given nn small arrays, and you will create one big array from the concatenation of one or more instances of the small arrays (each small array could occur more than once). The big array will be given as an array of indexes (1-based) of the small arrays, and the concatenation should be done in the same order as in this array. Then you should apply the standard problem mentioned above on the resulting big array.

For example let's suppose that the small arrays are {1, 6, -2}, {3, 3} and {-5, 1}. And the indexes in the big array are {2, 3, 1, 3}. So the actual values in the big array after formatting it as concatenation of the small arrays will be {3, 3, -5, 1, 1, 6, -2, -5, 1}. In this example the maximum sum is 9.

Can you help Mostafa solve this problem?

输入格式

The first line contains two integers nn and mm , nn is the number of the small arrays ( 1<=n<=501<=n<=50 ), and mm is the number of indexes in the big array ( 1<=m<=2500001<=m<=250000 ). Then follow nn lines, the ii -th line starts with one integer ll which is the size of the ii -th array ( 1<=l<=50001<=l<=5000 ), followed by ll integers each one will be greater than or equal -1000 and less than or equal 1000. The last line contains mm integers which are the indexes in the big array, and you should concatenate the small arrays in the same order, and each index will be greater than or equal to 1 and less than or equal to nn .

The small arrays are numbered from 1 to nn in the same order as given in the input. Some of the given small arrays may not be used in big array.

Note, that the array is very big. So if you try to build it straightforwardly, you will probably get time or/and memory limit exceeded.

输出格式

Print one line containing the maximum sum in the big array after formatting it as described above. You must choose at least one element for the sum, i. e. it cannot be empty.

Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d).

输入输出样例

  • 输入#1

    3 4
    3 1 6 -2
    2 3 3
    2 -5 1
    2 3 1 3
    

    输出#1

    9
    
  • 输入#2

    6 1
    4 0 8 -3 -10
    8 3 -2 -5 10 8 -9 -5 -4
    1 0
    1 -3
    3 -8 5 6
    2 9 6
    1
    

    输出#2

    8
    
首页