Problem:
Score : 400 points

Problem Statement

Snuke lives in another world, where slimes are real creatures and kept by some people.
Slimes come in N colors. Those colors are conveniently numbered 1 through N.
Snuke currently has no slime. His objective is to have slimes of all the colors together.

Snuke can perform the following two actions:

Select a color i (1≤i≤N), such that he does not currently have a slime in color i, and catch a slime in color i. This action takes him a_i seconds.

Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color i (1≤i≤N-1) will become color i+1, and the color of a slime in color N will become color 1. This action takes him x seconds.

Find the minimum time that Snuke needs to have slimes in all N colors.

Constraints

2≤N≤2,000

a_i are integers.

1≤a_i≤10^9

x is an integer.

1≤x≤10^9

Input

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

N x
a_1 a_2 ... a_N

Output

Find the minimum time that Snuke needs to have slimes in all N colors.

Sample Input 1

2 10
1 100

Sample Output 1

12

Snuke can act as follows:

Catch a slime in color 1. This takes 1 second.

Cast the spell. The color of the slime changes: 1 → 2. This takes 10 seconds.

Catch a slime in color 1. This takes 1 second.

Sample Input 2

3 10
100 1 100

Sample Output 2

23

Snuke can act as follows:

Catch a slime in color 2. This takes 1 second.

Cast the spell. The color of the slime changes: 2 → 3. This takes 10 seconds.

Catch a slime in color 2. This takes 1 second.

Cast the soell. The color of each slime changes: 3 → 1, 2 → 3. This takes 10 seconds.

Catch a slime in color 2. This takes 1 second.

Sample Input 3

4 10
1 2 3 4

Sample Output 3

10

Snuke can act as follows:

Catch a slime in color 1. This takes 1 second.

Catch a slime in color 2. This takes 2 seconds.

Catch a slime in color 3. This takes 3 seconds.

Catch a slime in color 4. This takes 4 seconds.

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

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};

fn main() {
    input!{
        n: usize,
        x: usize,
        v: [usize; n]
    }
    let mut ans = usize::max_value();
    let mut segt = SegT::from_vec(v);
    for i in 0..n {
        let mut t = x * i;
        for j in 0..n {
            if j < i {
                t += min(segt.min(0, j), segt.min(n - (i - j), n - 1));
            } else {
                t += segt.min(j - i, j);
            }
        }
        ans = min(ans, t);
    }
    println!("{}", ans);
}

struct SegT {
    v: Vec<usize>,
    n: usize
}

impl SegT {
    fn new(n: usize) -> SegT {
        let mut s = 1;
        while s < n {
            s *= 2;
        }
        SegT{v: vec![usize::max_value(); 2 * s - 1], n: s}
    }

    fn from_vec(xs: Vec<usize>) -> SegT {
        let l = xs.len();
        let mut s = 1;
        while s < l {
            s *= 2;
        }
        let mut v = vec![usize::max_value(); 2 * s - 1];
        for i in 0..l {
            v[i + s - 1] = xs[i];
        }
        for i in 2..s + 1 {
            let p = s - i;
            v[p] = std::cmp::min(v[2 * p + 1], v[2 * p + 2]);
        }
        SegT{v: v, n: s}
    }

    fn update(&mut self, i: usize, x: usize) {
        let n = self.n;
        self.update_rec(i, x, 0, 0, n - 1);
    }

    fn update_rec(&mut self, i: usize, x: usize, p: usize, pl: usize, pr: usize) {
        if pl > i || pr < i {
            return;
        } else if p == i + self.n - 1 {
            self.v[p] += x;
        } else {
            self.update_rec(i, x, 2 * p + 1, pl, (pl + pr - 1) / 2);
            self.update_rec(i, x, 2 * p + 2, (pl + pr + 1) / 2, pr);
            self.v[p] = std::cmp::max(self.v[2 * p + 1], self.v[2 * p + 2]);
        }
    }

    fn min(&mut self, l: usize, r: usize) -> usize {
        let n = self.n;
        self.min_rec(l, r, 0, 0, n - 1)
    }

    fn min_rec(&mut self, l: usize, r: usize, p: usize, pl: usize, pr: usize) -> usize {
        if pl > r || pr < l {
            usize::max_value()
        } else if l <= pl && pr <= r {
            self.v[p]
        } else {
            let a = self.min_rec(l, r, 2 * p + 1, pl, (pl + pr - 1) / 2);
            let b = self.min_rec(l, r, 2 * p + 2, (pl + pr + 1) / 2, pr);
            std::cmp::min(a, b)
        }
    }
}