Problem:
Score : 900 points

Problem Statement

Takahashi has decided to give a string to his mother.

The value of a string T is the length of the longest common subsequence of T and T', where T' is the string obtained by reversing T.
That is, the value is the longest length of the following two strings that are equal: a subsequence of T (possibly non-contiguous), and a subsequence of T' (possibly non-contiguous).

Takahashi has a string S. He wants to give her mother a string of the highest possible value, so he would like to change at most K characters in S to any other characters in order to obtain a string of the highest possible value.
Find the highest possible value achievable.

Constraints

1 \leq |S| \leq 300

0 \leq K \leq |S|

S consists of lowercase English letters.

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the highest possible value achievable.

Sample Input 1

abcabcabc
1

Sample Output 1

7

Changing the first character to c results in cbcabcabc.
Let this tring be T, then one longest common subsequence of T and T' is cbabcbc, whose length is 7.

Sample Input 2

atcodergrandcontest
3

Sample Output 2

15

Solution in Rust (1.15.1) (judge verdict: Accepted)

fn getline() -> String {
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}

#[allow(unused_macros)]
macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            s.trim().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
}

#[allow(unused_macros)]
macro_rules! readlvec {
    ($t: ty) => {
        {
            let s = getline();
            let iter = s.trim().split(' ');
            iter.map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
        }
    }
}

#[allow(unused_macros)]
macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn printiter<'a, T>(v: &'a T)
where
    &'a T: std::iter::IntoIterator,
    <&'a T as std::iter::IntoIterator>::Item: std::fmt::Display,
{
    for (i, e) in v.into_iter().enumerate() {
        if i != 0 {
            print!(" ");
        }
        print!("{}", e);
    }
    println!("");
}


#[allow(unused_imports)]
use std::cmp::{min, max};
#[allow(unused_imports)]
use std::collections::{BinaryHeap, BTreeMap, HashMap, HashSet};
#[allow(unused_imports)]
// use std::collections::btree_map::Entry::*;
#[allow(unused_imports)]
use std::collections::hash_map::Entry::*;

fn main() {
    use std::io::Write;
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    macro_rules! print { ($($arg:tt)*) => (write!(out, $($arg)*).unwrap()); }
    macro_rules! println { () => (writeln!(out).unwrap()); ($($arg:tt)*) => (writeln!(out, $($arg)*).unwrap()); }

    let s = readl!(String).into_bytes();
    let k = readl!(usize);
    // let mut dp = vec![vec![vec![None; k+1]; s.len()]; s.len()];
    let mut dp = HashMap::new();
    struct Env<'a> {dp: &'a mut HashMap<(isize, isize, usize), i32>, s: &'a Vec<u8>, }
    fn rec(env: &mut Env, l: isize, r: isize, k: usize) -> i32 {
        if let Some(v) = env.dp.get(&(l, r, k)) {
            return *v;
        }
        if l >= r {
            if l == r {
                1
            } else {
                0
            }
        } else {
            let mut res = 0;
            res = max(rec(env, l+1, r, k), res);
            res = max(rec(env, l, r-1, k), res);
            if env.s[l as usize] == env.s[r as usize] {
                res = max(rec(env, l+1, r-1, k)+2, res);
            } else if k != 0 {
                res = max(rec(env, l+1, r-1, k-1)+2, res);
            }
            env.dp.insert((l, r, k), res);
            // env.dp[l as usize][r as usize][k] = Some(res);
            res
        }
    }
    println!("{}", rec(&mut Env{dp: &mut dp, s: &s}, 0, s.len() as isize-1, k));
}