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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.util.*;
 
public class Main{
	
 
	public void solve(){
		int N = nextInt();
		long x = nextInt();
		int[] a = new int[N];
		SegmentTree rmq = new SegmentTree(N + 1);
		
		for(int i = 0; i < N; i++){
			a[i] = nextInt();
			rmq.update(i, a[i]);
		}
		
		long min = Long.MAX_VALUE;
		for(int i = 0; i < N; i++){
			long ans = x * i;
			for(int j = 0; j < N; j++){
				int mm = rmq.values[rmq.query(Math.max(j - i, 0), j + 1)];
				if(j - i < 0){
					mm = Math.min(mm, rmq.values[rmq.query(N + j - i, N)]);
				}
				ans += mm;
			}
			min = Math.min(min, ans);
		}
		out.println(min);
	}
	
	
	class SegmentTree{
		int n;
		int[] values;
		int[] idx;
		public SegmentTree(int nn){
			n = 1;
			while(n < nn){
				n *= 2;
			}
			values = new int[n * 2 - 1];
			idx = new int[n * 2 - 1];
			Arrays.fill(values, Integer.MAX_VALUE);
			
			for(int i = 0; i < n; i++){
				idx[i] = 0;
			}
			for(int i = 1; i < n; i++){
				idx[i + n - 1] = i;
			}
			int i = 0;
			int m = 1;
			int w = n;
			while(i < n){
				for(int j = 0; j < m; j++){
					idx[i++] = j * w;
				}
				m *= 2;
				w /= 2;
			}
		}
		
		public void update(int k, int a){
			values[k] = a;
			k += n - 1;
			while(k > 0){
				k = (k - 1) / 2;
				int i1 = idx[k * 2 + 1];
				int i2 = idx[k * 2 + 2];
				
				if(values[i1] > values[i2]){
					idx[k] = i2;
				}else{
					idx[k] = i1;
				}
			}
		}
		
 
		public int query(int begin, int end){
			return query(begin, end, 0, 0, n);
		}
		
		public int query(int a, int b, int k, int l, int r){
			if(r <= a || b <= l) return -1;
			
			if(a <= l && r <= b) return idx[k];
			else{
				int i1 = query(a, b, k * 2 + 1, l, (l + r) / 2);
				int i2 = query(a, b, k * 2 + 2, (l + r) / 2, r);
				if(i1 == -1) return i2;
				else if(i2 == -1) return i1;
				else if(values[i1] > values[i2]) return i2;
				else return i1;
			}
		}
	}
 
 
	
	private static PrintWriter out;
	public static void main(String[] args){
		out = new PrintWriter(System.out);
		new Main().solve();
		out.flush();
	}
	
	
	
	public static int nextInt(){
		int num = 0;
		String str = next();
		boolean minus = false;
		int i = 0;
		if(str.charAt(0) == '-'){
			minus = true;
			i++;
		}
		int len = str.length();
		for(;i < len; i++){
			char c = str.charAt(i);
			if(!('0' <= c && c <= '9')) throw new RuntimeException();
			num = num * 10 + (c - '0');
		}
		return minus ? -num : num;
	}
	
	public static long nextLong(){
		long num = 0;
		String str = next();
		boolean minus = false;
		int i = 0;
		if(str.charAt(0) == '-'){
			minus = true;
			i++;
		}
		int len = str.length();
		for(;i < len; i++){
			char c = str.charAt(i);
			if(!('0' <= c && c <= '9')) throw new RuntimeException();
			num = num * 10l + (c - '0');
		}
		return minus ? -num : num;
	}
	public static String next(){
		int c;
		while(!isAlNum(c = read())){}
		StringBuilder build = new StringBuilder();
		build.append((char)c);
		while(isAlNum(c = read())){
			build.append((char)c);
		}
		return build.toString();
	}
	
	
	private static byte[] inputBuffer = new byte[1024];
	private static int bufferLength = 0;
	private static int bufferIndex = 0;
	private static int read(){
		if(bufferLength < 0) throw new RuntimeException();
		if(bufferIndex >= bufferLength){
			try{
				bufferLength = System.in.read(inputBuffer);
				bufferIndex = 0;
			}catch(IOException e){
				throw new RuntimeException(e);
			}
			if(bufferLength <= 0) return (bufferLength = -1);
		}
		return inputBuffer[bufferIndex++];
	}
	
	private static boolean isAlNum(int c){
		return '!' <= c && c <= '~';
	}
}