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[n][k] = 1;
    for i in (0..n).rev() {
        let l = s[i].len();
        for j in 0..k+1 {
            if available[i+1][j] == 1 || (j + l <= k && available[i+1][j+l] == 1) {
                available[i][j] = 1;
            }
        }
    }
    assert!(available[0][0] == 1);

    let mut prev: Vec<u8> = vec![];

    let mut cut: Vec<bool> = vec![false; k+1];
    cut[0] = true;


    for i in 0..n {
        let l = s[i].len();
        let mut largest = k + 1;
        let mut suffixes: Vec<usize> = vec![];

        let mut last_suffix_len = prev.len();
        for j in 0..k+1 {
            if available[i+1][j] != 1 {
                continue
            }

            let left = j >= l && cut[j-l] && available[i][j-l] == 1;
            if left {
                let suffix = is_larger(&prev, &s[i], largest, j-l, k);
                if suffix >= 0 {
                    if suffix == 1 {
                        suffixes = vec![];
                    } else {
                        suffixes.push(last_suffix_len);
                    }
                    last_suffix_len = j;
                    largest = j-l;
                }
            }
        }

        if largest <= k {
            let mut next: Vec<u8> = vec![];
            let mut next_cut: Vec<bool> = vec![false; k+1];
            for i in 0..largest {
                next.push(prev[i]);
                next_cut[i] = cut[i];
            }
            next_cut[0] = true;
            next_cut[largest] = true;
            for &c in &s[i] {
                next.push(c);
            }
            next_cut[next.len()] = true;

            for x in suffixes {
                if x < next.len() {
                    next_cut[x] = true;
                }
            }

            prev = next;
            cut = next_cut;
        }
    }

    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>, t1: usize, t2: usize, k: usize) -> i32 {
    let mut wo2: Vec<u8> = vec![];
    for i in 0..t2 {
        wo2.push(s[i]);
    }
    for &c in t {
        wo2.push(c);
    }

    let mut wo: Vec<u8> = vec![];

    if t1 == k + 1 {
        for &c in s {
            wo.push(c);
        }
    } else {
        for i in 0..t1 {
            wo.push(s[i]);
        }
        for &c in t {
            wo.push(c);
        }
    }

    for i in 0..min(wo.len(), wo2.len()) {
        if wo[i] != wo2[i] {
            return if wo2[i] < wo[i] { 1 } else { -1 };
        }
    }
    return 0;
}