Table of Contents

## A Battle Against a Dragon solution codeforces

A squad of nn warriors is defending a castle from a dragon attack. There are mm barricades between the castle and the dragon.

The warriors are numbered from 11 to nn. The ii-th warrior knows kiki attacks: the jj-th of them deals ai,jai,j damage to the dragon and can only be applied if there are exactly bi,jbi,j barricades between the castle and the dragon.

The warriors make turns one after another, starting from warrior 11. After warrior nn makes his turn, the total damage to the dragon is calculated. The ii-th warrior performs exactly one of three possible moves in his turn:

- destroys one barricade (if there are any left);
- deploys one of his kiki attacks;
- skips a turn.

The total damage is the sum of damages dealt by the warriors who chose to deploy their attacks in their turn. What is the maximum total damage the warriors can deal to the dragon?

### A Battle Against a Dragon solution codeforces

The first line contains two integers nn and mm (1≤n,m≤3⋅1051≤n,m≤3⋅105) — the number of warriors and the initial number of barricades.

Then the descriptions of attacks for each warrior follow. The ii-th description consists of three lines.

The first line contains a single integer kiki (1≤ki≤m+11≤ki≤m+1) — the number of attacks the ii-th warrior knows.

The second line contains kiki integers ai,1,ai,2,…,ai,kiai,1,ai,2,…,ai,ki (1≤ai,j≤1091≤ai,j≤109) — the damage of each attack.

The third line contains kiki integers bi,1,bi,2,…,bi,kibi,1,bi,2,…,bi,ki (0≤bi,j≤m0≤bi,j≤m) — the required number of barricades for each attack. bi,jbi,j for the ii-th warrior are pairwise distinct. The attacks are listed in the increasing order of the barricades requirement, so bi,1<bi,2<⋯<bi,kibi,1<bi,2<⋯<bi,ki.

The sum of kiki over all warriors doesn’t exceed 3⋅1053⋅105.

### A Battle Against a Dragon solution codeforces

Print a single integer — the maximum total damage the warriors can deal to the dragon.

### A Battle Against a Dragon solution codeforces

2 4 1 2 4 2 10 5 3 4

10

### A Battle Against a Dragon solution codeforces

3 3 1 1 0 1 20 2 1 100 3

100

### A Battle Against a Dragon solution codeforces

2 1 2 10 10 0 1 2 30 20 0 1

30

### A Battle Against a Dragon solution codeforces

In the first example, the optimal choice is the following:

- warrior 11 destroys a barricade, now there are 33 barricades left;
- warrior 22 deploys his first attack (he can do it because b1,1=3b1,1=3, which is the current number of barricades).

The total damage is 1010.

If the first warrior used his attack or skipped his turn, then the second warrior would only be able to deploy his second attack. Thus, the total damage would be 2+5=72+5=7 or 55.

In the second example, the first warrior skips his move, the second warrior skips his move and the third warrior deploys his only attack.

In the third example, two equivalent options are:

- both warriors deploy their second attacks;
- the first warrior destroys one barricade and the second warrior deploys his first attack.

Both options yield 3030 total damage.