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

using namespace std;

int n,k;

long long dp[2010][2010],a[1010];

int main()

{

    cin>>n>>k;

    for(int i=1;i<=n;++i)scanf("%lld",a+i);

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

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

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

            int y=i-j;if(y<1)y+=n;

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

		}

	}

	long long mn=1e18;

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

	    long long ans=0;

	    for(int j=1;j<=n;++j)ans+=dp[j][i];

	    mn=min(mn,(long long)i*k+ans);

	}

	cout<<mn<<endl;

}


// 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:
// 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<cstdio>

#include<cmath>

#include<cstring>

#include<algorithm>

using namespace std;

const int N=2005;

int n,x,i,j,a[N],mn[N][N];

long long w,ans;

int main()

{

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

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

		scanf("%d",a+i);

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

	{

		mn[i][i]=a[i];

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

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

	}

	ans=1ll<<60;

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

	{

		w=0;

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

			w+=min((j-i-1>=0?mn[j-i][j]:mn[1][j]),(i-j>=0?mn[n-(i-j)][n]:1<<30));

		ans=min(ans,w+1ll*i*x);

	}

	printf("%lld",ans);

	return 0;

}


// 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<cstdio>

#include<cmath>

#include<cstring>

#include<algorithm>

using namespace std;

const int N=2005;

int n,x,i,j,a[N],mn[N][N];

long long w,ans;

int main()

{

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

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

		scanf("%d",a+i);

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

	{

		mn[i][i]=a[i];

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

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

	}

	ans=1ll<<60;

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

	{

		w=0;

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

			w+=min((j-i-1>=0?mn[j-i][j]:mn[1][j]),(i-j>=0?mn[n-(i-j)][n]:1<<30));

		ans=min(ans,w+1ll*i*x);

	}

	printf("%lld",ans);

	return 0;

}


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

using namespace std;

#define ll long long



template <class T>

inline void read(T &x)

{

    static char c;

    x=0;

    for(c=getchar();isspace(c);c=getchar());

    static bool sign;

    sign=0;

    if (c=='-') sign=!sign,c=getchar();

    for(;isdigit(c);x=x*10+c-'0',c=getchar());

    if (sign) x=-x;

    return;

}



#ifndef DEBUG//-DDEBUG

#define SHOW(x)

#else

#define SHOW(x) cout<<#x"="<<(x)<<endl

#endif



const int MAXN=2e3+5;

ll a[MAXN<<1];

ll best[MAXN];

const ll INF=1LL<<50;

int main()

{

	int n;

	ll x;

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

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

	{

		read(a[i]);

		a[i+n]=a[i];

	}

	ll ans=INF;

	memset(best,15,sizeof(best));

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

	{

		ll sum=ev*x;

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

		{

			best[i]=min(best[i],a[i+ev]);

			sum+=best[i];

		}

		ans=min(ans,sum);

	}

	printf("%lld",ans);

    return 0;

}

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

#include<cmath>

#include<cstring>

#include<algorithm>

using namespace std;

const int N=2005;

int n,x,i,j,a[N],mn[N][N];

long long w,ans;

int main()

{

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

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

		scanf("%d",a+i);

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

	{

		mn[i][i]=a[i];

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

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

	}

	ans=1ll<<60;

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

	{

		w=0;

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

			w+=min((j-i-1>=0?mn[j-i][j]:mn[1][j]),(i-j>=0?mn[n-(i-j)][n]:1<<30));

		ans=min(ans,w+1ll*i*x);

	}

	printf("%lld",ans);

	return 0;

}


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

#define ll long long

#define res register ll

#define inf 2e18

#define N 2005

using namespace std;

ll n,a[N],ans=inf,x,b[N];

inline ll read(){

	res X=0,w=0;char ch=0;

	while(!isdigit(ch)){w|ch=='-';ch=getchar();}

	while(isdigit(ch)){X=(X<<1)+(X<<3)+(ch^48);ch=getchar();}

	return w?-X:X;

}

int main(){

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

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

	  a[i]=read();

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

	  b[i]=inf;

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

		res sum=0;

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

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

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

		  sum+=b[j];

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

	}

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

	return 0;

}

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

#include<algorithm>

#include<cstring>

using namespace std;

long long f[2002][2002],n,a[2002],x;//f[i][j]:使用j次魔法，获得color i的最小花费的时间（不包括k*x）

int main()

{

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

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

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

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

	{

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

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

		{

			int pos=i-j;

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

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

		}

	}

	long long ans=1LL<<60LL,h;

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

	{

		h=0;

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

			h+=f[i][j];

		ans=min(ans,h+x*j);

	}

	printf("%lld",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
//2018.9.29 by ljz

#include<bits/stdc++.h>

using namespace std;

#define res register LL

#define LL long long

#define inf 0x3f3f3f3f

#define eps 1e-15

inline LL read(){

    res s=0;

    bool w=0;

    char ch=getchar();

    while(ch<'0'||ch>'9'){if(ch=='-')w=1;ch=getchar();}

    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();

    return w?-s:s;

}

inline void _swap(res &x,res &y){

    x^=y^=x^=y;

}

inline LL _abs(const res &x){

    return x>0?x:-x;

}

inline LL _max(const res &x,const res &y){

    return x>y?x:y;

}

inline LL _min(const res &x,const res &y){

    return x<y?x:y;

}

const LL N=2e3+10;

namespace MAIN{

    LL n,x;

    LL a[N],b[N][N],ans;

    inline void MAIN(){

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

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

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

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

            for(res j=i+1;j<=n;j++)b[i][j]=_min(b[i][j-1],a[j]);

        }

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

            res ret=i*x;

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

                res k=j-i;

                if(k<=0)k+=n,ret+=_min(b[k][n],b[1][j]);

                else ret+=b[k][j];

            }

            ans=_min(ans,ret);

        }

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

    }

}

int main(){

    MAIN::MAIN();

    return 0;

}

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

#include<algorithm>

#include<cstring>

using namespace std;

long long f[2002][2002],n,a[2002],x;//f[i][j]:使用j次魔法，获得color i的最小花费的时间（不包括k*x）

int main()

{

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

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

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

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

	{

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

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

		{

			int pos=i-j;

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

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

		}

	}

	long long ans=1LL<<60LL,h;

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

	{

		h=0;

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

			h+=f[i][j];

		ans=min(ans,h+x*j);

	}

	printf("%lld",ans);

}

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

#include <bits/stdc++.h>



#define pb push_back

#define pp pop_back

#define mp make_pair

#define f first

#define s second

#define all(x) x.begin(), x.end()

typedef long long ll;



using namespace std;



const int N = 4000 + 111, inf = 1e9, mod = 1e9 + 7;

const ll INF = 1e18;

const double pi = acos(-1.0), eps = 1e-10;



int n;

ll a[N], x, mn[N / 2][N], ans = INF;



int main(){

	srand(time(NULL));

	cin.tie(0);

	cout.tie(0);

	ios_base::sync_with_stdio(0);

	cout << setprecision(10) << fixed;



	cin >> n >> x;



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

		cin >> a[i];



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

		a[i] = a[i - n];



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

		ll m = inf;

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

			m = min(m, a[j]), mn[i][j] = m;

	}	



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

		ll answer = i * x;

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

			answer += mn[j][j + i];

		ans = min(ans, answer); 

	}



	cout << ans;



	return 0;

 	//cout << (clock() + 0.0) / CLOCKS_PER_SEC;	

}

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

#include<algorithm>

#include<cstring>

using namespace std;

long long f[2002][2002],n,a[2002],x;//f[i][j]:使用j次魔法，获得color i的最小花费的时间（不包括k*x）

int main()

{

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

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

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

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

	{

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

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

		{

			int pos=i-j;

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

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

		}

	}

	long long ans=1LL<<60LL,h;

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

	{

		h=0;

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

			h+=f[i][j];

		ans=min(ans,h+x*j);

	}

	printf("%lld",ans);

}

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



#define f first 

#define s second 

#define pb push_back               

#define mp make_pair 



using namespace std;                    



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<long long, long long> pll;



const int N = (int)1e5 + 123, mod = (int)1e9 + 7, inf = (int)1e9;



ll n, x, ans = 1e18, a[N], b[N];

int main(){    

	cin >> n >> x;

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

		cin >> a[i];

		b[i] = a[i];

	}



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

		ll cur = 0;

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

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

			cur += b[j];

		}

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

	}

	cout << ans;

	return 0;

}


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

#include<cstring>

#include<algorithm>

using namespace std;

int n,a[3000],x,am[3000];

long long ans=0;

int main()

{

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

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

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

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

	{

		long long tmp=0;

		for (int j=1;j<=n;j++) am[j]=min(am[j],a[(j+i-1)%n+1]),tmp+=am[j];

		tmp+=1ll*x*i;

		ans=min(ans,tmp);

	}

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

}

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

#include<cstdio>

#include<cmath>

#include<cstring>

#include<map>

#include<set>

#include<vector>

#include<stack>

#include<queue>

#include<algorithm>

#define ll long long

using namespace std;

const ll inf=0x3f3f3f3f3f3f3f3f;

ll cost[2010];

ll res[2010]; 

int main()

{

	ll n,x;

	cin>>n>>x;

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

	{

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

		res[i]=cost[i];

	}

	ll ans=inf;

	for(int k=0;k<n;k++)//xiugai k ci

	{

		ll temp=0;

		for(int i=1;i<=n;i++)//mei ge slime

		{

			ll low;

			if(i<=k)

			{

				low=min(res[i],cost[n-(k-i)]);

			}

			else

			{

				low=min(res[i],cost[i-k]);

			}

			res[i]=low;

			temp+=low;

		}

		temp+=k*x;

		ans=min(ans,temp);

	}

	printf("%lld",ans);

} 

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

#include<algorithm>

using namespace std;

typedef long long ll;

int n;

ll x;

ll a[2048];

ll md[2048][2048];

int main()

{

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

	for (int i = 0; i < n; i++) scanf("%lld", &a[i]);

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

	{

		md[i][i] = a[i];

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

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

	}

	ll ans = -1;

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

	{

		ll tot = x * y;

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

		{

			tot += md[i][(i + y) % n];

		}

		if (ans<0 || ans>tot) ans = tot;

	}

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

	return 0;

}


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

#include<string>

#include<cstring>

#include<map>

#include<algorithm>

#include<set>

#include<vector>

#include<cstdio>

#include<cmath>

#include<queue>

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>

#include<algorithm>

using namespace std;

typedef long long ll;

int n;

ll x;

ll a[2048];

ll md[2048][2048];

int main()

{

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

	for (int i = 0; i < n; i++) scanf("%lld", &a[i]);

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

	{

		md[i][i] = a[i];

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

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

	}

	ll ans = -1;

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

	{

		ll tot = x * y;

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

		{

			tot += md[i][(i + y) % n];

		}

		if (ans<0 || ans>tot) ans = tot;

	}

	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=2010;

typedef long long ll;

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

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

ll x,ans=llf;int n;

int main()

{

   

    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);

    }

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

    return 0;

}

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

#include<algorithm>

using namespace std;

typedef long long ll;

int n;

ll x;

ll a[2048];

ll md[2048][2048];

int main()

{

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

	for (int i = 0; i < n; i++) scanf("%lld", &a[i]);

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

	{

		md[i][i] = a[i];

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

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

	}

	ll ans = -1;

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

	{

		ll tot = x * y;

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

		{

			tot += md[i][(i + y) % n];

		}

		if (ans<0 || ans>tot) ans = tot;

	}

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

	return 0;

}


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

#include <algorithm>

using namespace std;

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

typedef long long ll;

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

ll dp[2010][2010];ll a[2010];

int main()

{

    ll n,x;

    cin>>n>>x;

    int i;

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

        cin>>a[i];

    int k;

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

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

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

    {

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

        {

            int temp=i-k;

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

            dp[i][k]=min(dp[i][k-1],(ll)a[temp]);

        }

    }

    ll sum=0;ll ans=llf;

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

    {

        sum=0;

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

            sum+=dp[j][i];

        sum+=i*x;

        ans=min(ans, sum);

    }

    cout<<ans<<endl;

    return 0;

}

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

#include<algorithm>

using namespace std;

typedef long long ll;

int n;

ll x;

ll a[2048];

ll md[2048][2048];

int main()

{

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

	for (int i = 0; i < n; i++) scanf("%lld", &a[i]);

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

	{

		md[i][i] = a[i];

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

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

	}

	ll ans = -1;

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

	{

		ll tot = x * y;

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

		{

			tot += md[i][(i + y) % n];

		}

		if (ans<0 || ans>tot) ans = tot;

	}

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

	return 0;

}


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

#include<cstdio> 

using namespace std;

typedef long long ll;

const int N=2005;



int n,x,mymin,mymini;

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



int main(){

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

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

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

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

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

			int k=i-j;

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

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

		}

	ll ans=1e18;

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

		ll sum=0;

		for (int j=1;j<=n;j++) sum+=dp[j][i];

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

	}

	printf("%lld",ans);

	return 0;

}

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

#include<algorithm>

using namespace std;

typedef long long ll;

int n;

ll x;

ll a[2048];

ll md[2048][2048];

int main()

{

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

	for (int i = 0; i < n; i++) scanf("%lld", &a[i]);

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

	{

		md[i][i] = a[i];

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

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

	}

	ll ans = -1;

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

	{

		ll tot = x * y;

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

		{

			tot += md[i][(i + y) % n];

		}

		if (ans<0 || ans>tot) ans = tot;

	}

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

	return 0;

}


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

using namespace std;

long long n,x,a[2001],f[2001][2001],minn,ans=2e18;

int main(){

	cin>>n>>x;

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

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

	}

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

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

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

			long long p=i-j;

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

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

		}

	}

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

		minn=0;

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

			minn+=f[j][i];

		}

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

	}

	cout<<ans;

}

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

#include<algorithm>

#include<cstring>

using namespace std;

long long f[2002][2002],n,a[2002],x;//f[i][j]:使用j次魔法，获得color i的最小花费的时间（不包括k*x）

int main()

{

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

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

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

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

	{

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

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

		{

			int pos=i-j;

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

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

		}

	}

	long long ans=1LL<<60LL,h;

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

	{

		h=0;

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

			h+=f[i][j];

		ans=min(ans,h+x*j);

	}

	printf("%lld",ans);

}

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

#include <cstdlib>

#include <cstring>

#include <iostream>

#include <algorithm>

using namespace std;

typedef long long LL;

const int maxn = 2005;

const int mod = 1e9 + 7;

LL a[maxn], n, x, b[maxn], ans;

int main() {

    cin>>n>>x;

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

    ans = 1LL * maxn * 1e9;

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

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

        LL tmp = 0;

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

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

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

            tmp += b[j];

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

    }

    cout<<ans<<endl;

    return 0;

}

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

#include<cctype>

#define reg register

inline int read(){

	reg int x=0,f=1;reg char ch=getchar();

	for(;!isdigit(ch);f=ch=='-'?-1:1,ch=getchar());

	for(;isdigit(ch);x=(x<<3)+(x<<1)+(ch^48),ch=getchar());

	return x*f;

}

#define min(a,b) (a<b?a:b)

#define loop(a) (a<1?(n+a):a)

typedef long long ll;

ll a[2005],f[2005][2005],ans=1ll<<55;

int main(){

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

	for(reg int i=1;i<=n;i++)a[i]=f[i][0]=read();

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

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

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

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

		ll t=x*1ll*i;

		for(reg int j=1;j<=n;j++)t+=f[j][i];

		ans=min(ans,t);

	}

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

	return 0;

}

// Fastest known solution for this problem: