Problem: Time Limit: 8 sec / Memory Limit: 64 MB





Example

Input

3 3 2
1 1
2 1 1
3 1 1 1


Output

8

Solution (in CPP):

Solution:
#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

bool prevSubset(bitset<15>& bs, const bitset<15>& mask)
{
    if(bs.none())
        return false;
    bs = bs.to_ulong() - 1ull;
    bs &= mask;
    return true;
}

int solve(vector<int>& w, int limit)
{
    int n = w.size();
    vector<int> dp(1<<n, INT_MAX/2);
    dp[(1<<n)-1] = 0;
    for(int i=(1<<n)-1; i>0; --i){
        bitset<15> mask(i);
        bitset<15> bs(i);
        do{
            int sum = 0;
            for(int j=0; j<n; ++j){
                if(bs[j])
                    sum += w[j];
            }
            if(sum <= limit)
                dp[(mask^bs).to_ulong()] = min(dp[(mask^bs).to_ulong()], dp[i] + 1);
        }while(prevSubset(bs, mask));
    }

    return dp[0];
}

int main()
{
    int n, m, limit;
    cin >> n >> m >> limit;

    vector<vector<int> > w(n);
    for(int i=0; i<n; ++i){
        int k;
        cin >> k;
        w[i].resize(k);
        for(int j=0; j<k; ++j)
            cin >> w[i][j];
    }

    vector<int> a;
    int prev = 0;
    int ret = 1 - m;
    for(int i=n-1; i>=0; --i){
        ret += prev * 2;
        if(w[i].size() > 0){
            for(unsigned j=0; j<w[i].size(); ++j)
                a.push_back(w[i][j]);
            prev = solve(a, limit);
        }
    }

    cout << max(0, ret) << endl;
    return 0;
}