Superman Celebrates Diwali 
Difficulty: Hard

Superman has been invited to India to celebrate Diwali. Unfortunately, on his arrival he learns that he has been invited mainly to help rescue people from a fire accident that has happened in a posh residential locale of New Delhi, where rescue is proving to be especially difficult. As he reaches the place of the fire, before him there are $N$ buildings, each of the same height $H$, which are on fire. Since it is Diwali, some floors of the buildings are empty as the occupants have gone elsewhere for celebrations. In his hurry to start the rescue Superman reaches the top of the building, but realizes that his jumping power is depleted and restricted due to change in his geographical setting.
He soon understands the restrictions of his jumping power, and they are as follows: 

+ He can use the jumping power any number of times until he reaches the bottom floor, which means he can use the jumping power only until before he reaches the bottom (Ground floor), which means, once he reaches the bottom floor, he cannot move to the top floor again and try to save people. (In one single drop from the top to bottom)

+ While switching buildings, he loses height $I$ while jumping.

The second restriction is explained below with an example.

Assume $I = 2$. Now Superman is in the 2<sup>nd</sup> building 5<sup>th</sup> floor ($B = 2$, $F = 5$). If he wants to switch to the fifth building ($B = 5$), he will lose height ($I = 2$), which means he will be at floor _3_ at building _5_ ($B = 5$, $F = 3$). He can jump freely from the current floor to the floor below on the same building . That is, suppose if he is at $(B = 5, F = 4)$, he can go to $(B = 5, F = 3)$ without any restrictions. He cannot skip a floor while jumping in the same building. He can go to the floor below the current floor of the same building or use his jumping power, switch building, and lose height $I$.

Given the information about the occupied floors in each of the $N$ buildings, help Superman to determine the maximum number of people he can save in one single drop from the top to the bottom floor with the given restrictions.

Input Format:
Input starts with three values:  the number of buildings $N$, the height of the buildings $H$, and the height Superman will lose when he switches buildings $I$.

These are followed by $N$ lines. Each $i^{th}$ line starts with a non negative integer $u$ indicating how many people are in the $i$<sup>th</sup> building. Each of the following $u$ integers indicates that a person is at height $u_i$ in the $i^{th}$ buiding. Each of the following $u$ integers are given and repetitions are allowed which means there can be more than one person in a floor.

$i$ indicates building number and $j$ indicates floor number. Building number will not be given; since $N$ lines follow the first line, you can assume that the $i^{th}$ line indicates the $i^{th}$ building's specifications.

**Constraints**  
$1 \le H,N \le 1900$  
$1 \le I \le 450$  
$0 \le u \le 1900$ (for each $i$, which means the maximum number of people in a particular building will not exceed $1900$)    
$1 \le u_{ij} \le H$

Output Format:
Output the maximum number of people Superman can save.

Sample Input:
	4 15 2 
	5 1 1 1 4 10
	8 9 5 7 7 3 9 8 8
	5 9 5 6 4 3
	0   

Sample Output:
	12

Python 3 starter code:


Reference solution (C++):
###C++  
```cpp  
# include <iostream>
# include <fstream>
# include <sstream>
# include <algorithm>   
# include <numeric>
# include <cstdio>
# include <cmath>
# include <cstdlib>    
# include <cstring> 
# include <vector>
# include <list>
# include <set> 
# include <map>
# include <stack>
# include <queue>
# include <cctype>
# include <climits>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

#define GI ({int t;scanf("%d",&t);t;})
#define REP(i,a,b) for(int i=a;i<b;i++)
#define ALL(v) (v).begin(),(v).end()
#define TR(i,x) for(typeof(x.begin()) i=x.begin();i!=x.end();i++)
#define pb push_back
#define mp make_pair
#define INF (int)1e9
#define EPS (double)(1e-9)
#define PI (double)(3.141592653589793)

int i, j, test_cases, num_buildings, height_b, height_l, a, n, people[2002][2002], dp[2002][2002],maxheight[2002];

int main() 
{
    scanf("%d %d %d", &num_buildings, &height_b, &height_l);
    for (i = 1; i <= num_buildings; i++) 
    {
    scanf("%d", &a);
        for (j = 0; j < a; j++) 
        {
             scanf("%d", &n);
             people[n][i]++; 
        }
    }

    for( int i = 1; i <= height_b; i++ ) 
    {
        maxheight[i]=0;
        for( int j = 1; j <= num_buildings; j++ ) 
        {
            dp[i][j] = dp[i-1][j];
            if( i - height_l >= 1 ) 
                dp[i][j] = max( dp[i][j] , maxheight[ i - height_l ] );
            dp[i][j] += people[i][j];
            maxheight[i] = max( maxheight[i] , dp[i][j] );
        }
    }

    int ans = 0;
    for(int i=1; i<=num_buildings; i++) 
    {
        ans = max(ans,dp[height_b][i]);
    }
    printf("%d\n",ans);
    return 0;      
}     
```