Under construction

Still being built. Things may change

Training

Interview

Time limit1sMemory limit1024 MB

Summary
Pick at least one training from each situation so the total time stays within M hours and is as large as possible. Print -1 if no valid plan exists.
Level

Medium5 of 10

Topics
Dynamic programming, Array
Solved
No attempts yet

Problem

Operations section chief Seungseo must plan the unit's training for the first quarter of 24. To do so, Seungseo selects which trainings to include in the plan.

Trainings are grouped into NN training situations, and the ii-th situation consists of did_i trainings. The jj-th training in the ii-th situation takes tijt_{ij} hours.

Seungseo wants to be fully prepared for every situation. So the plan must include at least one training from each situation. The total time of the trainings in the plan cannot exceed MM hours, and each training can be carried out only once.

Under these conditions, find the maximum total training time that the plan can have.

Input

The first line contains the number of training situations NN and the maximum training time MM, separated by a space.

The second line contains NN integers d1,⋯ ,dNd_1, \cdots, d_N, the number of trainings in each situation, separated by spaces.

The next NN lines, starting from line i+2i+2, contain the times of the trainings in the ii-th situation. Line i+2i+2 holds did_i integers ti1,⋯ ,tidit_{i1}, \cdots, t_{id_i}, separated by spaces.

Output

Print the maximum total training time that satisfies the conditions.

If it is impossible to pick at least one training from every situation, print -1.

Constraints

  • 1≤N≤10001 \le N \le 1000
  • 1≤M≤100001 \le M \le 10000
  • 1≤di≤10001 \le d_i \le 1000; ∑i=1Ndi≤1000\sum_{i=1}^N d_i \le 1000
  • 1≤tij≤10001 \le t_{ij} \le 1000
  • All input values are integers.

Examples2

  1. Example 1

    Input
    2 7
    2 1
    4 3
    2
    
    Expected output
    6
    
  2. Example 2

    Input
    5 24
    5 2 4 1 3
    23 7 11 3 2
    8 10
    5 17 20 9
    13
    1 24 3
    
    Expected output
    -1