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 (Clang 3.8.0) (judge verdict: Accepted)

#include <stdio.h>
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
#define max(X, Y) ((X) > (Y) ? (X) : (Y))
typedef long long ll;

const ll sqrtN = 64;
const ll INF = (1ll << 60ll);

ll n, x;
ll a[4096];
ll bucket[65];

void init(void);
ll range_min(int s, int t);//[s, t)
void update(int k, ll dat);

int main()
{
    // comment out
    
    ll ans = INF;
    ll m;

    init();

    scanf("%lld %lld", &n, &x);
    m = n;
    n += n;
    for (ll i = 1; i <= m; i++) {
        scanf("%lld", &a[i]);
        update(i, a[i]);
        update(i + m, a[i]);
    }

    for (ll i = 0; i < m; i++) {

        ll a_sum = 0;
        for (ll j = 1; j <= m; j++) {
            ll k = j + m;

            a_sum += range_min(k - i, k + 1);
        }

        ans = min(ans, i * x + a_sum);
    }
    
    printf("%lld\n", ans);

    return 0;
}

void init(void)
{
    for (int i = 0; i < 4096; i++) {
        a[i] = INF;
    }

    for (int i = 0; i < 65; i++) {
        bucket[i] = INF;
    }

    return;
}

//[s, t)
ll range_min(int s, int t)
{
    ll minv = INF;
    ll K = (n + sqrtN - 1) / sqrtN;
    ll l, r;
    
    for (int k = 0; k < K; k++) {
        l = k * sqrtN;
        r = (k + 1) * sqrtN;

        // out range
        if (t <= l) {
            break;
        }
        else if (r <= s) {
            continue;
        }

        // in range
        if (s <= l && r <= t) {
            minv = min(minv, bucket[k]);
        }
        else {
            for (ll i = max(l, s); i < min(r, t); i++) {
                minv = min(minv, a[i]);
            }
        }
        
    }

    return minv;
}

void update(int k, ll dat)
{
    ll minv = INF;
    a[k] = dat;

    k /= sqrtN;
    for (ll i = k * sqrtN; i < (k + 1) * sqrtN; i++) {
        minv = min(minv, a[i]);
    }

    bucket[k] = minv;

    return;
}