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: Wrong Answer)

#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部定数
#define D_SLIME_MAX		2000									// 最大スライム数

// 内部構造体 - 時間情報
typedef struct Time {
	long long mlTime;											// 確保時間
	int miCNo;													// 確保スライム
} Time;

// 内部変数
static FILE *szpFpI;											// 入力
static int siSCnt;												// スライム数
static int siMagic;												// 魔法時間
static Time sz2Time[D_SLIME_MAX + 5][D_SLIME_MAX];				// 確保時間[スライム][魔法回数]

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpA;
#endif

// 実行メイン
int
fMain(
	int piTNo					// <I> テスト番号 1～
)
{
	int i;
	char lc1Buf[1024], lc1Out[1024];

	// 入力 - セット
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", piTNo);
	szpFpI = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", piTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#else
	szpFpI = stdin;
#endif

	// スライム数・魔法時間 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d%d", &siSCnt, &siMagic);

	// 確保時間[スライム][魔法0] - 取得
	for (i = 1; i <= siSCnt; i++) {
		fscanf(szpFpI, "%lld", &sz2Time[i][0].mlTime);
		sz2Time[i][0].miCNo = i;
	}
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);

	// 確保時間[スライム][魔法回数] - セット
	int liSNo, liMCnt;
	for (liSNo = 1; liSNo <= siSCnt; liSNo++) {
		for (liMCnt = 1; liMCnt < siSCnt; liMCnt++) {

			// 確保スライム
			int liCNo = (liSNo - liMCnt - 1 + siSCnt) % siSCnt + 1;

			// 確保時間
			long long llTime = sz2Time[liCNo][0].mlTime + (long long)siMagic * (long long)liMCnt;

			// 小さい時間をセット
			if (sz2Time[liSNo][liMCnt - 1].mlTime < llTime) {
				sz2Time[liSNo][liMCnt].mlTime = sz2Time[liSNo][liMCnt - 1].mlTime;
				sz2Time[liSNo][liMCnt].miCNo = sz2Time[liSNo][liMCnt - 1].miCNo;
			}
			else {
				sz2Time[liSNo][liMCnt].mlTime = llTime;
				sz2Time[liSNo][liMCnt].miCNo = liCNo;
			}
		}
	}

	// 最小時間 - 取得
	long long llMin = 0;
	for (liSNo = 1; liSNo <= siSCnt; liSNo++) {
		llMin += sz2Time[liSNo][0].mlTime;
	}

	// 最小時間 - 取得
	for (liMCnt = 1; liMCnt < siSCnt; liMCnt++) {
		long long llSum = (long long)siMagic * (long long)liMCnt;
		for (liSNo = 1; liSNo <= siSCnt; liSNo++) {
			llSum += sz2Time[sz2Time[liSNo][liMCnt].miCNo][0].mlTime;
		}

		// 最小時間 - 更新
		if (llMin > llSum) {
			llMin = llSum;
		}
	}

	// 結果 - セット
	sprintf(lc1Out, "%lld\n", llMin);

	// 結果 - 表示
#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, lc1Out)) {
		siRes = -1;
	}
#else
	printf("%s", lc1Out);
#endif

	// 残データ有無
#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, "")) {
		siRes = -1;
	}
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpI);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", piTNo);
	}
	else {
		printf("NG %d\n", piTNo);
	}
#endif

	return 0;
}

int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		fMain(i);
	}
#else
	fMain(0);
#endif

	return 0;
}