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 C (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <inttypes.h>

#define INF UINT64_C(12345678901234567)

#define KI_MAX 4096

uint64_t ki[KI_MAX * 2 - 1];
uint64_t* ki_buffer = ki + (KI_MAX - 1);

void ki_koutiku(void) {
	int i;
	for (i = KI_MAX - 1 - 1; i >= 0; i--) {
		uint64_t hoge = ki[i * 2 + 1], fuga = ki[i * 2 + 2];
		ki[i] = hoge <= fuga ? hoge : fuga;
	}
}

uint64_t ki_get_i(int i, int qs, int qe, int ss, int se) {
	if (qe <= ss || se <= qs) { /* 完全に重ならない */
		return INF;
	} else if (qs <= ss && se <= qe) { /* セグメントがクエリに完全に含まれる */
		return ki[i];
	} else {
		int sm = ss + (se - ss) / 2;
		uint64_t l = ki_get_i(i * 2 + 1, qs, qe, ss, sm);
		uint64_t r = ki_get_i(i * 2 + 2, qs, qe, sm, se);
		return l <= r ? l : r;
	}
}

uint64_t ki_get(int qs, int qe) {
	return qs < qe ? ki_get_i(0, qs, qe, 0, KI_MAX) : INF;
}

int N;
uint64_t x;
uint64_t a[2048];

int main(void) {
	int i;
	uint64_t kotae = INF;
	if (scanf("%d%" SCNu64, &N, &x) != 2) return 1;
	for (i = 0; i < N; i++) {
		if (scanf("%" SCNu64, &a[i]) != 1) return 1;
		ki_buffer[i] = ki_buffer[N + i] = a[i];
	}
	ki_koutiku();
	for (i = 0; i <= N; i++) {
		uint64_t candidate = x * i;
		int j;
		for (j = 0; j < N; j++) {
			candidate += ki_get(N + j - i, N + j + 1);
		}
		if (candidate < kotae) kotae = candidate;
	}
	printf("%" PRIu64 "\n", kotae);
	return 0;
}