stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 5.293046564602241x):
#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;

}
// Slower C++ solution:
#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...\"");

}

// Faster C++ solution for the same problem (measured speedup: 5.280751137923964x):
#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;

}
// Slower C++ solution:
/**

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

}

// Faster C++ solution for the same problem (measured speedup: 5.424843563099462x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 1.7971174642303624x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 9.562203479412107x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 5.320856131965329x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 1.8266694754227453x):
#include<bits/stdc++.h>

using namespace std;

#define ll long long

ll a[2005],b[2005][2005],n,x,ans=10e18;

template <class code>inline code read(const code &a)

{

    code x=0;short w=0;char ch=0;

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

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

    return w?-x:x;

}

int main()

{

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

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

    {

        a[i]=read(a[i]);

    }

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

    {

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

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

        {

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

        }

    }

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

    {

        ll temp=i*x;

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

        {

            int k=j-i;

            if(k<=0)

            {

                temp+=min(b[k+n][n],b[1][j]);

            }

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

        }

        ans=min(ans,temp);

    }

    printf("%lld",ans);

    return 0;

}
// Slower C++ solution:
#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);

}

// Faster C++ solution for the same problem (measured speedup: 5.16952259725697x):
#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;

}
// Slower C++ solution:
#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;

}




// Faster C++ solution for the same problem (measured speedup: 5.2137818115675545x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.291411110541311x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.158050796520637x):
#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);

}
// Slower C++ solution:
//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;

}

// Faster C++ solution for the same problem (measured speedup: 5.1612606870551465x):
#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);

}
// Slower C++ solution:
#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;	

}

// Faster C++ solution for the same problem (measured speedup: 5.31686738731948x):
#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);

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.139839905781271x):
#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);

}