Problem:
Score : 1500 points

Problem Statement

Iroha has a sequence of N strings s_1, s_2, ..., s_N.

She will choose some (possibly all) strings from the sequence, then concatenate those strings retaining the relative order, to produce a long string.

Among all strings of length K that she can produce in this way, find the lexicographically smallest one.

Constraints

1 ≦ N ≦ 2000

1 ≦ K ≦ 10^4

For each i, 1 ≦ |s_i| ≦ K.

|s_1| + |s_2| + ... + |s_N| ≦ 10^6

For each i, s_i consists of lowercase letters.

There exists at least one string of length K that Iroha can produce.

Input

The input is given from Standard Input in the following format:

N K
s_1
s_2
:
s_N

Output

Print the lexicographically smallest string of length K that Iroha can produce.

Sample Input 1

3 7
at
coder
codar

Sample Output 1

atcodar

at and codar should be chosen.

Sample Input 2

3 7
coder
codar
at

Sample Output 2

codarat

codar and at should be chosen.

Sample Input 3

4 13
kyuri
namida
zzzzzzz
aaaaaa

Sample Output 3

namidazzzzzzz

namida and zzzzzzz should be chosen.

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

trait InputValue {
    fn parse(s: &str) -> Self;
}

fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

// ===

fn main() {
    let (n, k): (usize, usize) = read();
    let s: Vec<String> = readn(n);
    let s: Vec<Vec<u8>> = s.into_iter().map(|s| s.into_bytes()).collect();

    let mut available: Vec<Vec<i32>> = vec![vec![0; k+1]; n+1];
    available[0][0] = 1;
    for i in 0..n {
        let l = s[i].len();
        for j in 0..k+1 {
            if available[i][j] == 0 {
                continue;
            }
            available[i+1][j] = 1;
            if j + l <= k {
                available[i+1][j+l] = 1;
            }
        }
    }

    available[n][k] = 2;
    for i in (0..n).rev() {
        let l = s[i].len();
        for j in 0..k+1 {
            if available[i][j] == 1 {
                if available[i+1][j] == 2 || (j + l <= k && available[i+1][j+l] == 2) {
                    available[i][j] = 2;
                }
            }
        }
    }


    let mut prev: Vec<u8> = vec![];
    for i in 0..n {
        let l = s[i].len();
        let mut largest = 0;
        let mut flag = 0;
        for j in 0..k+1 {
            if available[i+1][j] != 2 {
                continue
            }
            let left = j >= l && available[i][j-l] == 2;
            let right = available[i][j] == 2;

            if left && right {
                // println!("{} {} {:?} {:?}", i, j, prev, s[i]);
                if is_larger(&prev, &s[i], j) {
                    largest = j-l;
                    flag = 1;
                } else {
                    largest = j;
                    flag = 0;
                }
            } else if left {
                largest = j-l;
                flag = 1;
            } else if right {
                largest = j;
                flag = 0;
            } else {
                panic!("cant't be");
            }
        }

        if flag >= 1 {
            let mut next: Vec<u8> = vec![];
            for i in 0..largest {
                next.push(prev[i]);
            }
            for &c in &s[i] {
                next.push(c);
            }
            prev = next;
        }
    }

    for c in prev {
        print!("{}", c as char);
    }
    println!();
}

// compare
//   A = s[0..j) and
//   B = s[0..j-t.len()) + t
// returns true if B < A.
fn is_larger(s: &Vec<u8>, t: &Vec<u8>, j: usize) -> bool {
    let mut ti = 0;
    for i in j-t.len()..j {
        if s[i] != t[ti] {
            return t[ti] < s[i];
        }
        ti += 1;
    }
    false
}