Problem:
Problem B: Butterfly

Claire is a man-eater. She's a real man-eater. She's going around with dozens of guys. She's dating all the time. And one day she found some conflicts in her date schedule. D'oh!

So she needs to pick some dates and give the others up. The dates are set by hours like 13:00 to 15:00. She may have more than one date with a guy. For example, she can have dates with Adam from 10:00 to 12:00 and from 14:00 to 16:00 and with Bob from 12:00 to 13:00 and from 18:00 to 20:00. She can have these dates as long as there is no overlap of time. Time of traveling, time of make-up, trouble from love triangles, and the likes are not of her concern. Thus she can keep all the dates with Adam and Bob in the previous example. All dates are set between 6:00 and 22:00 on the same day.

She wants to get the maximum amount of satisfaction in total. Each guy gives her some satisfaction if he has all scheduled dates. Let's say, for example, Adam's satisfaction is 100 and Bob's satisfaction is 200. Then, since she can make it with both guys, she can get 300 in total. Your task is to write a program to satisfy her demand. Then she could spend a few hours with
you... if you really want.

Input

The input consists of a sequence of datasets. Each dataset has the following format:

N

Guy1

...

GuyN

The first line of the input contains an integer N (1 ≤ N ≤ 100), the number of guys. Then there come the descriptions of guys. Each description is given in this format:

M L

S1 E1

...

SM EM

The first line contains two integers Mi (1 ≤ Mi ≤ 16) and Li (1 ≤ Li ≤ 100,000,000), the number of dates set for the guy and the satisfaction she would get from him respectively. Then M lines follow. The i-th line contains two integers Si and Ei (6 ≤ Si < Ei ≤ 22), the starting and ending time of the i-th date.

The end of input is indicated by N = 0.

Output

For each dataset, output in a line the maximum amount of satisfaction she can get.

Sample Input

2
2 100
10 12
14 16
2 200
12 13
18 20
4
1 100
6 22
1 1000
6 22
1 10000
6 22
1 100000
6 22
16
1 100000000
6 7
1 100000000
7 8
1 100000000
8 9
1 100000000
9 10
1 100000000
10 11
1 100000000
11 12
1 100000000
12 13
1 100000000
13 14
1 100000000
14 15
1 100000000
15 16
1 100000000
16 17
1 100000000
17 18
1 100000000
18 19
1 100000000
19 20
1 100000000
20 21
1 100000000
21 22
0

Output for the Sample Input

300
100000
1600000000

Solution in Rust (judge verdict: Accepted)

extern crate core;

use std::str::FromStr;
use std::fmt;
use std::cmp::{Ordering, max};
use std::fmt::{Display, Error, Formatter};
use std::ops::{Range, Add};

struct Istream {
    line: Vec<String>,
    position: usize
}
type Parse<T: FromStr> = Result<T, T::Err>;
impl Istream {
    fn new() -> Istream {
        Istream {line:vec![], position: 0}
    }
    fn read_word(&mut self) -> &String {
        if self.position == self.line.len() {
            let mut line= String::new();
            std::io::stdin().read_line(&mut line).ok();
            self.line = line.split_whitespace().map(|x|x.to_string()).collect();
            self.position = 0
        }
        let result = &self.line[self.position];
        self.position += 1;
        result
    }
    fn read(&mut self) -> String {
        self.read_word().to_string()
    }
    fn get<T: FromStr>(&mut self) -> T {self.read_word().parse().ok().unwrap()}
    fn read_usize(&mut self) -> usize {self.get()}
    fn read_int(&mut self) -> i32 {self.get()}
    fn read_long(&mut self) -> i64 {self.get()}
    fn read_double(&mut self) -> f64 {self.get()}
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}

macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
macro_rules! repeat {
    ($count:expr; $($statement:tt)+) => {
        for _ in 0 .. $count {
            $($statement)*
        }
    };
}

macro_rules! yields {
    ($count:expr; $block:block) => {{
        let mut vec = Vec::with_capacity($count);
        for _ in 0 .. $count {
            vec.push($block);
        }
        vec
    }}
}
macro_rules! array {
    ($factor: expr; ) => {$factor};
    ($factor: expr; $dimension: expr) => {{
        let size = $dimension;
        let mut vec = Vec::with_capacity(size);
        for _ in 0 .. size {
            vec.push($factor)
        }
        vec
    }};
    ($factor:expr; $dimension:expr, $($r:expr),*) => {{
        let size = $dimension;
        let mut vec = Vec::with_capacity(size);
        for _ in 0 .. size{
            let factor = array!($factor; $($r),*);
            vec.push(factor)
        }
        vec
    }};
}
fn tabulate<F, R>(count: usize, mut generator: F) -> Vec<R> where F : FnMut(usize) -> R {
    let mut result = Vec::<R>::with_capacity(count);
    for f in 0..count {
        result.push(generator(f));
    }
    result
}
#[derive(Copy, Clone, Default)]
struct TimeTable {
    state: usize,
}
impl TimeTable {
    fn new() -> TimeTable {
        TimeTable {state: 0}
    }
    fn add_schedule(&self, from: usize, to: usize) -> TimeTable {
        TimeTable { state: self.state | (((1 << (to - from)) - 1) << (from - 6))}
    }
    fn merged(&self, other: &TimeTable) -> TimeTable {
        TimeTable {state: self.state | other.state}
    }
    fn is_overlap(&self, other: &TimeTable) -> bool {
        self.state & other.state != 0
    }
}

#[derive(Copy, Clone)]
struct Guy {
    time: TimeTable,
    satisfaction: i64,
}
impl Guy {
    fn new() -> Guy {
        Guy{time: TimeTable::new(), satisfaction: 0}
    }
    fn write_schedule(&mut self, from: usize, to: usize) -> () {
        self.time = self.time.add_schedule(from, to);
    }
}
fn solve(guys: &Vec<Guy>, memo: &mut Vec<Vec<i64>>,  used: usize, time: TimeTable) -> i64 {
    if used == guys.len() {
        0
    }else if memo[used][time.state] != -1 {
        memo[used][time.state]
    }else {
        memo[used][time.state] = if guys[used].time.is_overlap(&time) {
            solve(guys, memo, used + 1, time)
        }else {
            max(solve(guys, memo, used + 1, time), solve(guys, memo, used + 1, time.merged(&guys[used].time)) + guys[used].satisfaction)
        };
        memo[used][time.state]
    }
}
fn main() {
    loop {
        let_all!(n: usize);
        if n == 0 {
            break
        }
        let guys: Vec<Guy> = yields!(n; {
            let_all!(m:usize, l:i64);
            let mut guy = Guy::new();
            guy.satisfaction = l;
            for _ in 0 .. m {
                let_all!(s: usize, e: usize);
                guy.write_schedule(s, e);
            }
            guy
        });
        let mut memo: Vec<Vec<i64>> = array!(-1; n, 1 << 16);
        println!("{}", solve(&guys, &mut memo, 0, TimeTable::new()))
    }
}