stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h>



#define MAX_N 2005

#define bit(mask, i) (mask & (1<<i))



using namespace std;



typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;

typedef vector<vi> vvi;

typedef long long ll;

typedef double lf;



class SegmentTree {

private:

    vector<ll> st, A;

    int n;

    int leftc(int p) { return (p << 1) + 1; }

    int rightc(int p) { return (p << 1) + 2; }

    ll join(ll a, ll b) { return min(a, b); }



    void build(int p, int L, int R) {

        if (L == R) { st[p] = A[L]; }

        else {

            int c = (L + R)/2, p1 = leftc(p), p2 = rightc(p);

            build(p1, L, c);

            build(p2, c + 1, R);

            st[p] = join(st[p1], st[p2]);

        }

    }



    ll query(int p, int L, int R, int i, int j) {

        if (i == L && j == R) { return st[p]; }

        int c = (L + R)/2, p1 = leftc(p), p2 = rightc(p);

        if ( j <= c ) { return query(p1, L, c, i, j); }

        if ( i > c ) { return query(p2, c + 1, R, i, j); }

        ll q1 = query(p1, L, c, i, c), q2 = query(p2, c + 1, R, c + 1, j);

        return join(q1, q2);

    }



public:

    SegmentTree(const vector<ll> &_A) {

        A = _A; n = (int)A.size();

        st.assign(4 * n, 0);

        build(0, 0, n - 1);

    }

    ll query(int i, int j) { return query(0, 0, n - 1, i, j); }

};



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);



    //freopen("a.in", "r", stdin);

    //freopen("a.out", "w", stdout);



    ll n, x;



    cin>>n>>x;

    vector<ll> colors(n);

    ll sol = 0;

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

        cin>>colors[i];

        sol += colors[i];

    }



    SegmentTree st(colors);



    ll psol, mincol;



    for(int p = 1; p < n; ++p) {

        psol = x * p;

        for(int i = 0; i < n; ++i) {

            mincol = st.query(max(0, i - p), i);

            if(p > i) { mincol = min(mincol, st.query(n - p + i, n - 1)); }

            psol += mincol;

        }

        sol = min(sol, psol);

    }



    cout<<sol<<"\n";



    return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<string.h>

#include<algorithm>

#include<math.h>

using namespace std;

long long int a[2010]={0};

long long int b[2010][2010]={0};

int main(){   long long int n,x,i,j,sum,minx,temp;

    scanf("%lld %lld",&n,&x);

    for(i=1;i<=n;i++)

        scanf("%lld",&a[i]);

    for(i=1;i<=n;i++){

        b[i][0]=a[i];

        for(j=1;j<n;j++){

            temp=i-j;

            if(temp<=0)

                temp=n+temp;

            b[i][j]=min(b[i][j-1],a[temp]);

        }

    }

    minx=0x3f3f3f3f3f3f3f3f;

    for(i=0;i<n;i++){

        sum=0;

        for(j=1;j<=n;j++)

            sum+=b[j][i];

        sum+=i*x;

        minx=min(sum,minx);

    }

    printf("%lld\n",minx);

}






// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long LL;



const int maxn = 2e3 + 5;

LL a[maxn];

LL b[maxn][maxn];//b[i][k] : 当整个过程使用了【k】次魔法，获得【color i】时的最少花费的时间。(不包括【k * x】).



int main(){

    int n;

    LL x,sum = 1LL << 60LL;

    cin >> n >> x;

    for(int i = 1;i <= n;i++) cin >> a[i];

    for(int i = 1;i <= n;i++){

        b[i][0] = a[i];

        for(int k = 1;k <= n - 1;k++){

            int pos = i - k;

            if(pos <= 0) pos = n - abs(pos);

            b[i][k] = min(b[i][k - 1],a[pos]);//【b[i][k]】的转移方程.

        }

    }

    for(int k = 0;k <= n - 1;k++){

        LL ans = 0;

        for(int i = 1;i <= n;i++){

            ans += b[i][k];

        }

        sum = min(sum,ans + x * k);//取最小值.

    }

    cout << sum << endl;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<string.h>

#include<algorithm>

#include<math.h>

using namespace std;

long long int a[2010]={0};

long long int b[2010][2010]={0};

int main(){   long long int n,x,i,j,sum,minx,temp;

    scanf("%lld %lld",&n,&x);

    for(i=1;i<=n;i++)

        scanf("%lld",&a[i]);

    for(i=1;i<=n;i++){

        b[i][0]=a[i];

        for(j=1;j<n;j++){

            temp=i-j;

            if(temp<=0)

                temp=n+temp;

            b[i][j]=min(b[i][j-1],a[temp]);

        }

    }

    minx=0x3f3f3f3f3f3f3f3f;

    for(i=0;i<n;i++){

        sum=0;

        for(j=1;j<=n;j++)

            sum+=b[j][i];

        sum+=i*x;

        minx=min(sum,minx);

    }

    printf("%lld\n",minx);

}






// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

const long long inf=2e18;

#define N 2050

#define res register int

#define ll long long

using namespace std;

ll a[N],x,n,m,f[N][N],sum,ans;

int main()

{

	scanf("%lld%lld",&n,&x);

	for(res i=1;i<=n;i++)

	scanf("%lld",&a[i]);

	sum=inf;

	for(res i=1;i<=n;i++)

	f[i][0]=a[i];

	

	for(res i=1;i<=n;i++)

	{

		for(res j=1;j<=n-1;j++)

		{

			ll tim=i-j;

			if(tim<=0) tim+=n;

			f[i][j]=min(f[i][j-1],a[tim]);

		}

	}

	

	for(res i=0;i<=n-1;i++)

	{

		ans=0;

		for(res j=1;j<=n;j++)

		ans+=f[j][i];

		sum=min(sum,ans+x*i);

	}

	printf("%lld\n",sum);

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<string.h>

#include<algorithm>

#include<math.h>

using namespace std;

long long int a[2010]={0};

long long int b[2010][2010]={0};

int main(){   long long int n,x,i,j,sum,minx,temp;

    scanf("%lld %lld",&n,&x);

    for(i=1;i<=n;i++)

        scanf("%lld",&a[i]);

    for(i=1;i<=n;i++){

        b[i][0]=a[i];

        for(j=1;j<n;j++){

            temp=i-j;

            if(temp<=0)

                temp=n+temp;

            b[i][j]=min(b[i][j-1],a[temp]);

        }

    }

    minx=0x3f3f3f3f3f3f3f3f;

    for(i=0;i<n;i++){

        sum=0;

        for(j=1;j<=n;j++)

            sum+=b[j][i];

        sum+=i*x;

        minx=min(sum,minx);

    }

    printf("%lld\n",minx);

}






// Fastest known solution for this problem:
// Original C++ solution (slower):
#pragma GCC optimize(2)

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

const int maxn = 2e3 + 5;

LL a[maxn],b[maxn][maxn];



int sleep() {

	int j=0;

	for(int i=1; i<2000001; i++) j++;

	return j;

}



int main() {

	int n;

	LL x,sum = 1LL << 60LL;

	cin >> n >> x;

	for(int i = 1; i <= n; i++) cin >> a[i];

	for(int i = 1; i <= n; i++) {

		b[i][0] = a[i];

		for(int k = 1; k <= n - 1; k++) {

			int pos = i - k;

			if(pos <= 0) pos = n - abs(pos);

			b[i][k] = min(b[i][k - 1],a[pos]);

		}

	}

	for(int k = 0; k <= n - 1; k++) {

		LL ans = 0;

		for(int i = 1; i <= n; i++) {

			ans += b[i][k];

		}

		sum = min(sum,ans + x * k);

	}

	printf("%lld",sum);

	sleep();

	return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<string.h>

#include<algorithm>

#include<math.h>

using namespace std;

long long int a[2010]={0};

long long int b[2010][2010]={0};

int main(){   long long int n,x,i,j,sum,minx,temp;

    scanf("%lld %lld",&n,&x);

    for(i=1;i<=n;i++)

        scanf("%lld",&a[i]);

    for(i=1;i<=n;i++){

        b[i][0]=a[i];

        for(j=1;j<n;j++){

            temp=i-j;

            if(temp<=0)

                temp=n+temp;

            b[i][j]=min(b[i][j-1],a[temp]);

        }

    }

    minx=0x3f3f3f3f3f3f3f3f;

    for(i=0;i<n;i++){

        sum=0;

        for(j=1;j<=n;j++)

            sum+=b[j][i];

        sum+=i*x;

        minx=min(sum,minx);

    }

    printf("%lld\n",minx);

}






// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



#ifdef Lin1043

    #define DEBUG(msg, ...) printf("(%s #%d) " msg, __FUNCTION__, __LINE__, __VA_ARGS__)

    #define massert(...) assert(...)

#else

    #define DEBUG(...)

    #define massert(...)

#endif



#define rep(i , l , r) for(int i = (l) , ___ = (r) ; i <= ___ ; ++i )

#define per(i , r , l) for(int i = (r) , ___ = (l) ; i >= ___ ; --i )



struct iopener

{

    iopener()

    {

#ifdef Lin1043

        freopen("./in" , "r" , stdin);

#endif

    }

} iop;



typedef long long LL;



template<typename T>inline bool chkmin(T &x , const T &y) { return x > y ? (x = y , 1) : 0; }

template<typename T>inline bool chkmax(T &x , const T &y) { return x < y ? (x = y , 1) : 0; }



template<typename T>inline T read(T &f)

{

    f = 0; int x = 1 ; char c = getchar();

    while(!isdigit(c)) x = (c == '-' ? -1 : 1) , c = getchar();

    while(isdigit(c)) (f *= 10) += c & 15 , c = getchar();

    return f = x * f;

}



const int N = 2000 + 5;



int n , m , A[N] , mn[N][N];



LL Ans = 1ll << 60;



int main()

{

    read(n) , read(m);

    rep(i , 1 , n) read(A[i]);

    rep(i , 1 , n)

    {

        int x = A[i];

        rep(j , i , n)

            chkmin(x , A[j]) , mn[i][j] = x;

    }

    rep(i , 0 , n - 1)

    {

        LL res = 1ll * i * m;

        rep(j , 1 , n)

        {

            res += j > i ? mn[j - i][j] :

                   min(mn[1][j] , mn[j - i + n][n]);

        }

        chkmin(Ans , res);

    }

    cout << Ans << endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<string.h>

#include<algorithm>

#include<math.h>

using namespace std;

long long int a[2010]={0};

long long int b[2010][2010]={0};

int main(){   long long int n,x,i,j,sum,minx,temp;

    scanf("%lld %lld",&n,&x);

    for(i=1;i<=n;i++)

        scanf("%lld",&a[i]);

    for(i=1;i<=n;i++){

        b[i][0]=a[i];

        for(j=1;j<n;j++){

            temp=i-j;

            if(temp<=0)

                temp=n+temp;

            b[i][j]=min(b[i][j-1],a[temp]);

        }

    }

    minx=0x3f3f3f3f3f3f3f3f;

    for(i=0;i<n;i++){

        sum=0;

        for(j=1;j<=n;j++)

            sum+=b[j][i];

        sum+=i*x;

        minx=min(sum,minx);

    }

    printf("%lld\n",minx);

}






// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

using namespace std;

#define ll long long

#define inf 0x3f3f3f3f3f3f3f3f

ll a[2050];ll n;ll x;

ll cyz[2050];

int main(){

	std::ios::sync_with_stdio(false);

	cin>>n;cin>>x;

	for(int i=0;i<n;i++){

		cin>>a[i];cyz[i]=a[i];

	}

	ll minn=inf;ll res=0;

	for(int k=0;k<n;k++){

		ll temp=0;

		for(int i=0;i<n;i++){

			int pos=i-k;

			if(pos<0)pos=n+pos;

			cyz[i]=min(cyz[i],a[pos]);

			temp+=cyz[i];

		}

		temp+=x*k;

		minn=min(temp,minn);

	}

	cout<<minn<<endl;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<string.h>

#include<algorithm>

#include<math.h>

using namespace std;

long long int a[2010]={0};

long long int b[2010][2010]={0};

int main(){   long long int n,x,i,j,sum,minx,temp;

    scanf("%lld %lld",&n,&x);

    for(i=1;i<=n;i++)

        scanf("%lld",&a[i]);

    for(i=1;i<=n;i++){

        b[i][0]=a[i];

        for(j=1;j<n;j++){

            temp=i-j;

            if(temp<=0)

                temp=n+temp;

            b[i][j]=min(b[i][j-1],a[temp]);

        }

    }

    minx=0x3f3f3f3f3f3f3f3f;

    for(i=0;i<n;i++){

        sum=0;

        for(j=1;j<=n;j++)

            sum+=b[j][i];

        sum+=i*x;

        minx=min(sum,minx);

    }

    printf("%lld\n",minx);

}






// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long LL;



const int maxn = 2e3 + 5;

LL a[maxn];

LL b[maxn][maxn];



int main(){

    int n;

    LL x,sum = 1LL << 60LL;

    cin >> n >> x;

    for(int i = 1;i <= n;i++) cin >> a[i];

    for(int i = 1;i <= n;i++){

        b[i][0] = a[i];

        for(int k = 1;k <= n - 1;k++){

            int pos = i - k;

            if(pos <= 0) pos = n - abs(pos);

            b[i][k] = min(b[i][k - 1],a[pos]);

        }

    }

    for(int k = 0;k <= n - 1;k++){

        LL ans = 0;

        for(int i = 1;i <= n;i++){

            ans += b[i][k];

        }

        sum = min(sum,ans + x * k);//取最小值.

    }

    cout << sum << endl;

}


// Optimized solution to the same problem (faster):
#include <cstdio>



typedef long long LL;



const int MAXN = 2000;

int n, x, a[MAXN + 5], mn[MAXN + 5];



template<typename Tp>

inline void chkmin ( Tp& a, const Tp b ) { if ( b < a ) a = b; }



int main () {

	scanf ( "%d %d", &n, &x );

	for ( int i = 1; i <= n; ++ i ) scanf ( "%d", &a[i] ), mn[i] = a[i];

	LL ans = 1ll << 60;

	for ( int k = 0; k < n; ++ k ) {

		LL now = 1ll * k * x;

		for ( int i = 1; i <= n; ++ i ) {

			chkmin ( mn[i], a[( i + k - 1 ) % n + 1] );

			now += mn[i];

		}

		chkmin ( ans, now );

	}

	printf ( "%lld\n", ans );

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

 

using namespace std;

const int N=2000+10;

typedef long long ll;

const ll llf=(1ll<<63)-1;

ll dp[N][N],a[N];

int main()

{

    int n;

    ll x,ans=llf;

    scanf("%d%lld",&n,&x);

    for(int i=1;i<=n;i++)

    {

        scanf("%lld",&a[i]);

        dp[0][i]=a[i];

    }

    for(int i=1;i<=n-1;i++)

    {

        for(int j=1;j<=n;j++)

        {

            int ind=j-i;

            if(ind<=0)  ind=n+ind;

            dp[i][j]=min(dp[i-1][j],a[ind]);

        }

    }

    for(int i=0;i<=n-1;i++)

    {

        ll ant=0;

        for(int j=1;j<=n;j++)

            ant+=dp[i][j];

        ans=min(ans,ant+i*x);

       // cout<<ans<<endl;

    }

    printf("%lld\n",ans);

    return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>



typedef long long LL;



const int MAXN = 2000;

int n, x, a[MAXN + 5], mn[MAXN + 5];



template<typename Tp>

inline void chkmin ( Tp& a, const Tp b ) { if ( b < a ) a = b; }



int main () {

	scanf ( "%d %d", &n, &x );

	for ( int i = 1; i <= n; ++ i ) scanf ( "%d", &a[i] ), mn[i] = a[i];

	LL ans = 1ll << 60;

	for ( int k = 0; k < n; ++ k ) {

		LL now = 1ll * k * x;

		for ( int i = 1; i <= n; ++ i ) {

			chkmin ( mn[i], a[( i + k - 1 ) % n + 1] );

			now += mn[i];

		}

		chkmin ( ans, now );

	}

	printf ( "%lld\n", ans );

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



typedef long long ll;



int main() {

	ll N,x;

	cin>>N>>x;

	vector<ll> a(N), dp(N,LLONG_MAX);

	for (int i=0; i<N; i++)

		cin>>a[i];

	ll ret=LLONG_MAX;

	for(int i=0; i<N; i++) {

		ll cur=0;

		for(int j=0; j<N; j++)

			dp[j]=min(dp[j],a[(j+N-i)%N]), cur+=dp[j];

		ret=min(ret,cur+i*x);

	}

	cout<<ret<<"\n";

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>



typedef long long LL;



const int MAXN = 2000;

int n, x, a[MAXN + 5], mn[MAXN + 5];



template<typename Tp>

inline void chkmin ( Tp& a, const Tp b ) { if ( b < a ) a = b; }



int main () {

	scanf ( "%d %d", &n, &x );

	for ( int i = 1; i <= n; ++ i ) scanf ( "%d", &a[i] ), mn[i] = a[i];

	LL ans = 1ll << 60;

	for ( int k = 0; k < n; ++ k ) {

		LL now = 1ll * k * x;

		for ( int i = 1; i <= n; ++ i ) {

			chkmin ( mn[i], a[( i + k - 1 ) % n + 1] );

			now += mn[i];

		}

		chkmin ( ans, now );

	}

	printf ( "%lld\n", ans );

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <cmath>

#include <iomanip>

#include <algorithm>

#include <map>

#include <unordered_map>

#include <string>

#include <queue>

#include <memory>

#include <deque>

#include <set>

#include <unordered_set>

#include <cmath>

#include <list>

#include <cassert>

#include <cstring>



using namespace std;



using big = long long;



const int N = 20000;



big a[N], b[N];



int main() {

	std::ios_base::sync_with_stdio(false);

  int n, m;

  cin >> n >> m;

  for (int i = 0; i < n; ++i) {

    cin >> a[i];

  }

  big ans = 1ll << 60;

  for (int moves = 0; moves < n; ++moves) {

    big s = 1ll * m * moves;

    for (int i = 0; i < n; ++i) {

      s += a[i];

    }

    for (int i = 0; i < n; ++i) {

      b[i] = min(a[i], a[(i + (n - 1)) % n]);

    }

    for (int i = 0; i < n; ++i) {

      a[i] = b[i];

    }

    ans = min(ans, s);

  }

  cout << ans << endl;

}


// Optimized solution to the same problem (faster):
#include <cstdio>



typedef long long LL;



const int MAXN = 2000;

int n, x, a[MAXN + 5], mn[MAXN + 5];



template<typename Tp>

inline void chkmin ( Tp& a, const Tp b ) { if ( b < a ) a = b; }



int main () {

	scanf ( "%d %d", &n, &x );

	for ( int i = 1; i <= n; ++ i ) scanf ( "%d", &a[i] ), mn[i] = a[i];

	LL ans = 1ll << 60;

	for ( int k = 0; k < n; ++ k ) {

		LL now = 1ll * k * x;

		for ( int i = 1; i <= n; ++ i ) {

			chkmin ( mn[i], a[( i + k - 1 ) % n + 1] );

			now += mn[i];

		}

		chkmin ( ans, now );

	}

	printf ( "%lld\n", ans );

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<vector>



#define rep(i, n) for(int i=0;i<n;i++)

#define repb(i, a, b) for(int i=a;i<b;i++)

#define MAX_N 10000

 

using namespace std;

typedef long long int ll;

 

const int INF = 1<<30;

const ll LINF = ll(1e18);

const ll MOD = ll(1e9+7);

 

int main(){

  int n, x;cin>>n>>x;

  vector<ll> a(n), mcost(n);

  rep(i, n){

    cin>>a[i];

    mcost[i]=a[i];

  }

  ll ans = LINF;

  rep(k, n){ // cast a spell k times

    ll cost = 0LL;

    rep(i, n){

      mcost[i] = min(mcost[i], a[(i+n-k)%n]);

      cost += mcost[i];

    }

    ans = min(ans, cost+ll(k)*x);

  }

  cout<<ans<<endl;

  // system("read -n 1 -s -p \"Press any key to continue...\"");

}

// Optimized solution to the same problem (faster):
#include <cstdio>



typedef long long LL;



const int MAXN = 2000;

int n, x, a[MAXN + 5], mn[MAXN + 5];



template<typename Tp>

inline void chkmin ( Tp& a, const Tp b ) { if ( b < a ) a = b; }



int main () {

	scanf ( "%d %d", &n, &x );

	for ( int i = 1; i <= n; ++ i ) scanf ( "%d", &a[i] ), mn[i] = a[i];

	LL ans = 1ll << 60;

	for ( int k = 0; k < n; ++ k ) {

		LL now = 1ll * k * x;

		for ( int i = 1; i <= n; ++ i ) {

			chkmin ( mn[i], a[( i + k - 1 ) % n + 1] );

			now += mn[i];

		}

		chkmin ( ans, now );

	}

	printf ( "%lld\n", ans );

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/**

 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏

 *    created : 2020-01-01 16:45:09

**/



#include <iostream>

#include <deque>

#include <map>

#include <queue>

#include <set>

#include <stack>

#include <string>

#include <vector>

#include <algorithm>

#include <cmath>

#include <complex>

#include <numeric>

#include <bitset>

#include <cassert>

#include <cctype>

#include <iomanip>

#include <random>

#include <utility>

#include <chrono>



using namespace std;



using int64 = long long;



template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) { os << '{'; size_t n = vec.size(); for (size_t i = 0; i < n; ++i) { os << vec[i]; if (i != n - 1) os << ','; } os << '}'; return os; }

template<class T, class U> std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) { return os << '{' << p.first << " " << p.second << '}'; }

template<class T> std::istream& operator>>(std::istream& is, std::vector<T>& vec) { size_t n = vec.size(); for (size_t i = 0; i < n; ++i) is >> vec[i]; return is; }



#ifdef LOCAL

#define debug(_) cerr << #_ << ": " << (_) << '\n'

#else

#define debug(_) 1728

#endif



constexpr int64 INF = 1e18;



int main() {

  ios_base::sync_with_stdio(false);

  cin.tie(nullptr);



  int n; int64 x; cin >> n >> x;

  vector<int64> a(n); cin >> a;



  vector<vector<int64>> dp(n, vector<int64>(n, INF));

  // dp[i][j] : 位置 i にいるスライムを j 回動かしたときの a[i] の min



  for (int i = 0; i < n; ++i) {

    for (int move = 0; move < n; ++move) {

      int pos = (i + move) % n;

      dp[i][move] = min(dp[i][(move + n - 1) % n], a[pos]);

    }

  }



  int64 ans = INF;



  for (int move = 0; move < n; ++move) {

    int64 sum = 0;

    for (int j = 0; j < n; ++j) {

      sum += dp[j][move];

    }

    sum += move * static_cast<int64>(x);

    ans = min(ans, sum);

  }



  cout << ans << endl;



  return 0;

}

// Optimized solution to the same problem (faster):
#pragma GCC optimize("unroll-loops")



#include <cstdio>

#include <algorithm>



using namespace std;



using int64 = long long;



template<class T> inline void fastin(T &num) {

  T c;

  num = 0;

  c = getchar_unlocked();

  for (; (c > 47 && c < 58); c = getchar_unlocked()) num = (num << 3) + (num << 1) + c - 48;

}



constexpr int64 INF = 1e18;



int main() {

  int n; int64 x; fastin(n); fastin(x);

  int64 a[n];

  for (int i = 0; i < n; ++i) fastin(a[i]);



  int64 dp[n][n]; fill(dp[0], dp[n], INF);



  for (int i = 0; i < n; ++i) {

    for (int move = 0; move < n; ++move) {

      int pos = (i + move) % n;

      dp[i][move] = min(dp[i][(move + n - 1) % n], a[pos]);

    }

  }



  int64 ans = INF;



  for (int move = 0; move < n; ++move) {

    int64 sum = 0;

    for (int j = 0; j < n; ++j) {

      sum += dp[j][move];

    }

    sum += move * static_cast<int64>(x);

    ans = min(ans, sum);

  }



  printf("%lld\n", ans);



  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define ll long long

ll n,k,a[100000];

ll f[100000],ans=1e18;

int main(){

    cin>>n>>k;

	memset(f,0x3f,sizeof(f));

    for(int i=0;i<n;i++){

		cin>>a[i];

	}

    for(int i=0;i<=n;i++){

        for(int j=0;j<n;j++)

            f[j]=min(f[j],a[(j-i+n)%n]);

        ll ret=0;

        for (int j=0;j<n;++j) ret+=f[j];

        ans=min(ans,ret+1ll*k*i);

    }

    cout<<ans<<endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <algorithm>

#include <cstring>

#include <cctype>

using namespace std;

typedef long long int64;

inline int read(int f = 1, int x = 0, char ch = ' ')

{

    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;

    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();

    return f*x;

}

const int N = 4e3+5;

int n, x, a[N], q[N], l, r;

int64 ans;

int main()

{

    n = read(), x = read(); 

    for(int i = 1; i <= n; ++i) a[i] = read(), a[i+n] = a[i];

    for(int k = 0; k < n; ++k)

    {

        int64 s = 1ll*k*x; l = 1, r = 0;

        for(int i = 1; i <= k; ++i)

        {

            while(l <= r&&a[q[r]] >= a[i]) --r;

            q[++r] = i;

        } 

        for(int i = k+1; i <= n+k; ++i) 

        {

            while(l <= r&&i-k > q[l]) ++l;

            while(l <= r&&a[q[r]] >= a[i]) --r; 

            q[++r] = i; s += a[q[l]];

        }

        ans = k?min(s, ans):s;

    }

    printf("%lld\n", ans);

    return 0;

}

// Fastest known solution for this problem: