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)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

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

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

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

pub 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
}

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);
parse_tuple!(A, B, C, D);
parse_tuple!(A, B, C, D, E);

// ===

const INF: usize = 1000000;

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 can_make = vec![vec![false; k+1]; n+1];
    can_make[n][k] = true;

    for i in (0..n).rev() {
        let f = s[i].len();
        for j in 0..k+1 {
            if can_make[i+1][j] {
                if j >= f {
                    can_make[i][j-f] = true;
                }
                can_make[i][j] = true;
            }
        }
    }


    let mut last: Vec<u8> = vec![];
    for i in 1..n+1 {
        // println!("===");
        // println!("{:?}", can_make[i-1]);
        // println!("{:?}", can_make[i]);

        let f = s[i-1].len();
        let mut cmp = s[i-1].clone();
        cmp.append(&mut last.clone());
        let z = z(&cmp);

        let mut best_size = INF;
        // let mut best_size = if can_make[i][f] == 2 { 0 } else { INF };
        for j in f..k+1 {
            if can_make[i][j] && can_make[i-1][j-f] {
                let rst = compare(&z, &last, &s[i-1], best_size, j-f);
                if rst >= 0 {
                    best_size = j-f;
                }
            }
        }

        if best_size < INF {
            let mut next = vec![];
            for i in 0..best_size {
                next.push(last[i]);
            }
            for &c in &s[i-1] {
                next.push(c);
            }

            let nl = next.len();
            for j in 0..k+1 {
                if !can_make[i][j] {
                    continue;
                }
                if j > nl {
                    can_make[i][j] = false;
                } else if j < nl {
                    let lastn = last.len();
                    let mut z = vec![b'~'];
                    if can_make[i-1][j] {
                        z = last[0..min(lastn, j)].iter().cloned().collect();
                    }
                    if j >= f && can_make[i-1][j-f] {
                        let mut zz: Vec<u8> = last[0..min(lastn, j-f)].iter().cloned().collect();
                        let mut f = s[i-1].clone();
                        zz.append(&mut f);
                        if zz < z {
                            z = zz;
                        }
                    }
                    if next < z {
                        can_make[i][j] = false;
                    }
                }
            }
            last = next;
        } else {
            for j in 0..k+1 {
                can_make[i][j] &= can_make[i-1][j];
            }
        }
        // println!("{} {:?}", i, last);
    }

    for i in 0..k {
        print!("{}", last[i] as char);
    }
    println!("");
}

// z := next + last
fn compare(z: &Vec<usize>, last: &Vec<u8>, next: &Vec<u8>, a: usize, b: usize) -> i32 {
    if b > last.len() {
        return -1;
    }

    if a == INF {
        // last vs last[0,b] + next
        let idx = next.len() + b;
        if idx >= z.len() {
            return 0;
        }
        if b + z[idx] >= last.len() {
            return 0;
        }
        if z[idx] >= next.len() {
            return -1;
        }
        return if next[z[idx]] < last[b+z[idx]] { 1 } else { -1 };
    }

    if a == b {
        return 0;
    }
    let n = z.len();
    let nl = next.len();
    let diff = b-a;
    if z[nl+a] >= diff {
        if a + next.len() <= b {
            return 0;
        }
        let l = a + next.len() - b;
        if z[next.len()-l] >= l {
            return 0;
        }
        let zn = z[next.len()-l];
        return if next[zn] < next[next.len()-l+zn] { 1 } else { -1 } ;
    } else if z[nl+a] == nl {
        return 0;
    } else {
        let zn = z[nl+a];
        return if last[a+zn] < next[zn] { 1 } else { -1 };
    }
}

// (z[idx] := longest common prefix of ([idx,n), [0,n))
fn z(a: &Vec<u8>) -> Vec<usize> {
    let n = a.len();
    let mut z = vec![0; n];
    if n == 0 {
        return z;
    }
    z[0] = n;
    let mut l = 0;
    let mut r = 0;
    for i in 1..n {
        if i > r {
            l = i;
            r = i;
            while r < n && a[r-l] == a[r] {
                r += 1;
            }
            z[i] = r - l;
            r -= 1;
        } else {
            let k = i-l;
            if z[k] < r-i+1 {
                z[i] = z[k];
            } else {
                let l = i;
                while r < n && a[r-l] == a[r] {
                    r += 1;
                }
                z[i] = r-l;
                r -= 1;
            }
        }
    }
    z
}