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.



Sample input:
2 10
1 100


Sample output:
12

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = float("inf")
 
 
def main():
    N, x = map(int, input().split())
    a_list = list(map(int, input().split()))
 
    ans = INF
    m = [INF] * N
    for rotate in range(N):
        total_time = rotate * x
        for i in range(N):
            t = a_list[i - rotate]
            if t < m[i]:
                m[i] = t
            total_time += m[i]
        ans = min(ans, total_time)
    print(ans)
 
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def ris(): return list(input())
def pli(): return "".join(list(map(str, ans)))


N, x = rli()
a = rli()
ans = 2000000000001
magic_time = 0
s_min = a[:]
for i in range(0, N):
    for j in range(N):
        s_min[j] = min(a[j-i], s_min[j])
    ans = min(sum(s_min) + x*i, ans)
print(ans)

Submission status: Accepted

Solution code:
n,x=map(int,raw_input().split())
a=map(int,raw_input().split())
s=[[10**12]*n for _ in xrange(n)]
for i in xrange(n):
    for j in xrange(n):
        if j==0:s[i][j]=a[i]
        else:s[i][j]=min(s[i][j-1],a[(i-j)%n])
ans=float("inf")
for j in xrange(n):
    tmp=x*j
    for i in xrange(n):
        tmp+=s[i][j]
    ans=min(ans,tmp)
print ans

Submission status: Accepted

Solution code:
n,x = map(int, raw_input().split())
l = map(int, raw_input().split())
ret = sum(l)
ll = [i for i in l]
for k in range(1,n):
    for i in range(n):
        p = (i+n-k) % n
        ll[i] = min(ll[i], l[p])
    ret = min(ret, sum(ll) + k * x)
print ret

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))

pos = a.index(min(a))
na = [a[pos]] + a[pos+1:] + a[:pos]

ans = sum(a)
old_b = na[:]
for k in range(1, N):
    b = []
    for i in range(N):
        if i - k >= 0:
            b.append(min(old_b[i], na[i - k]))
        else:
            b.append(old_b[i])
    # print(k, b)
    old_b = b
    ans = min(ans, k * x + sum(b))
print(ans)

Submission status: Accepted

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N, x = read()
A = read()
ans = sum(A)
tempcost = ans
BC = A[:]
xt = 0
for i in range(1, N):
	xt += x
	BC = [BC[j - 1] if BC[j - 1] < BC[j] else BC[j] for j in range(N)] 
	tempcost = xt + sum(BC)
	if(ans > tempcost):
		ans = tempcost
	else:
		break
print(ans)

Submission status: Accepted

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N, x = read()
A = read()
ans = sum(A)
tempcost = ans
BC = A[:]
for i in range(1, N):
	BC = [BC[j - 1] if BC[j - 1] < BC[j] else BC[j] for j in range(N)] 
	tempcost = x * i + sum(BC)
	if(ans > tempcost):
		ans = tempcost
	else:
		break
print(ans)

Submission status: Accepted

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N, x = read()
A = read()
ans = sum(A)
tempcost = ans
BC = A[:]
for i in range(1, N):
	BC = [BC[j - 1] if BC[j - 1] < BC[j] else BC[j] for j in range(N)] 
	tempcost = x * i + sum(BC)
	if(ans > tempcost):
		ans = tempcost

print(ans)

Submission status: Accepted

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N, x = read()
A = read()
ans = sum(A)
tempcost = ans
BC = A[:]
for i in range(1, N):
	BC = [BC[j - 1] if BC[j - 1] < BC[j] else BC[j] for j in range(N)] 
	tempcost = x * i + sum(BC)
	if(ans > tempcost):
		ans = tempcost
print(ans)

Submission status: Accepted

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N, x = read()
A = read()
ans = sum(A)
tempcost = ans
xt = 0
BC = A
for i in range(1, N):
	xt += x
	A = [A[-1]] + A[0:-1] 
	BC = [v if v < BC[i] else BC[i] for (i, v) in enumerate(A)]
	tempcost = xt + sum(BC)
	if(ans > tempcost):
		ans = tempcost
	else:
		break
print(ans)

Submission status: Accepted

Solution code:
def read():
	return [int(i) for i in input().split(" ")]
N, x = read()
A = read()
ans = sum(A)
tempcost = ans
xt = 0
BC = A
for i in range(1, N):
	xt += x
	A = [A[-1]] + A[0:-1] 
	BC = [v if v < BC[i] else BC[i] for (i, v) in enumerate(A)]
	tempcost = xt + sum(BC)
	if(ans > tempcost):
		ans = tempcost
print(ans)

Submission status: Accepted

Solution code:
n,x=map(int,raw_input().split(" "))
a=map(int,raw_input().split(" "))
t=[i for i in a]
s=10**15
for m in range(n):
    t=[min(t[j],a[(j-m)%n]) for j in range(n)]
    s=min(x*m+sum(t),s)
print s

Submission status: Accepted

Solution code:
def min_k_shift(k, a, N, x, range_min):
    cost = k*x
    for j in range(N):
        cost += range_min[(j-k)%N][j] #min(a[(j-sj)%N] for sj in range(k))
    return cost

N, x = [int(s) for s in raw_input().split()]

a = [int(s) for s in raw_input().split()]

range_min = [[0]*N for st in range(N)]
for st in range(N):
    range_min[st][st] = a[st]

for k in range(1,N):
    for st in range(N):
        range_min[st][(st+k)%N] = min(range_min[st][(st+k-1)%N],a[(st+k)%N])

print(min(min_k_shift(k,a,N,x,range_min) for k in range(N)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from __future__ import division, print_function, absolute_import, unicode_literals

N, x = map(int, input().split())
a = list(map(int, input().split()))

arg_a = a.index(min(a))
a = a[arg_a:] + a[:arg_a]
ans = sum(a)
for t in range(1, N):
    for i in range(N-1, 0, -1):
        a[i] = min(a[i], a[i-1])
    tmp = (t*x) + sum(a)
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
bs = list(map(int, input().split()))
dp = bs[:]
ans = sum(bs)
for k in range(1, N):
    dp = [min(dp[i], bs[i-k]) for i in range(N)]
    ans = min(ans, sum(dp)+x*k)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
N,x = list(map(int,input().split()))
a = list(map(int,input().split()))
ans = sum(a)
mini = a
nx = 0
for i in range(1,N):
	nx += x
	mini = [k if k < mini[j-1] else mini[j-1] for j,k in enumerate(a)]
	s = sum(mini) + nx
	ans = min(s,ans)
print(ans)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
al = [int(i) for i in input().split()]
bi = [i for i in al]
ans = sum(al)
for k in range(1, n):
    bi = [min(bi[i-1], al[i]) for i in range(n)]
    ans = min(ans, k * x + sum(bi))
print(ans)

Submission status: Accepted

Solution code:
def solve():
    n, x = map(int, input().split())
    al = [int(i) for i in input().split()]
    bi = [i for i in al]
    ans = sum(al)
    for k in range(1, n):
        bi = [min(bi[i-1], al[i]) for i in range(n)]
        ans = min(ans, k * x + sum(bi))
    print(ans)

if __name__=="__main__":
    solve()

Submission status: Accepted

Solution code:
def mygets():
    return [int(v) for v in input().split(' ')]

n, x = mygets()
a = mygets()

b1 = a

ans = sum(a)
xt = 0
for i in range(1, n):
    xt += x
    b1 = [u if u < b1[k - 1] else b1[k - 1] for k, u in enumerate(a)]
    bsum = sum(b1) + xt
    if bsum < ans:
        ans = bsum
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
def mygets():
    return [int(v) for v in input().strip().split(' ')]

n, x = mygets()
a = mygets()

b1 = a

ans = sum(a)

for i in range(1, n):
    b2 = [u if u < b1[k - 1] else b1[k - 1] for k, u in enumerate(a)]
    ans = min(ans, sum(b2) + i * x)
    b1 = b2

print(ans)

Submission status: Accepted

Solution code:
def mygets():
    return list(map(int, input().strip().split(' ')))

n, x = mygets()
a = mygets()

b1 = a

ans = sum(a)

def f(k, u):
    j = k - 1
    if j < 0:
        j = n - 1
    w = b1[j]
    if u < w:
        return u
    return w

for i in range(1, n):
    b2 = [f(k, u) for k, u in enumerate(a)]
    ans = min(ans, sum(b2) + i * x)
    b1 = b2

print(ans)

Submission status: Accepted

Solution code:
def solve(n, x, a):
	costs = []
	cost_min = 0
	for k in range(n):
		if k == 0:
			costs = a.copy()
		else:
			for i in range(n):
				costs[i] = min(costs[i], a[(i-k)%n])
		cost_k = sum(costs) + k*x
		if k == 0:
			cost_min = cost_k
		else:
			cost_min = min(cost_min, cost_k)
			if cost_min != cost_k:
				break
		if cost_min < (k+1) *x:
			break
 
	return cost_min
n, x = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
print(solve(n,x,a))

Submission status: Accepted

Solution code:

def solve_():
    import itertools
    N, x = map(int, input().split())
    a = list(map(int, input().split()))
    s_cost = a.copy()
    cost = sum(s_cost)

    for k, i in itertools.product(range(1, N), range(N)):
        s_cost[i] = a[(i - k) % N] if s_cost[i] > a[(i - k) % N] else s_cost[i]
        if i == N - 1 and cost > k*x + sum(s_cost):
            cost = k*x + sum(s_cost)

    print(cost)

solve_()

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))
B = a[:] # 魔法の回数がk以下のときのスライムjをてに入れる最小の時間（ただし、魔法を唱える時間は最後に
         # 加えるのでここでは除いている
ans = 10 ** 13
for i in range(N):
    B = [a[j - i] if B[j] > a[j - i] else B[j] for j in range(N)]
    tmp = sum(B) + i * x
    if ans > tmp:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))
B = a[:] # 魔法の回数がk以下のときのスライムjをてに入れる最小の時間（ただし、魔法を唱える時間は最後に
         # 加えるのでここでは除いている
ans = 10 ** 13
for i in range(N):
    for j in range(N):
        if B[j] > a[j - i]:
            B[j] = a[j - i]
    tmp = sum(B) + i * x
    if ans > tmp:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
N, x = map(int,input().split())
A = list(map(int,input().split()))
B = A
A = A + A
#print(B)
#print(A)
ans = 10**18
for i in range(N):
    for j in range(N):
        if B[j] > A[j+N-i]:
            B[j] = A[j+N-i]

    #print(sum(B))
    t = sum(B) + x*i
    #print(t)
    if ans > t:
        ans = t
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from copy import deepcopy

N, x = map(int,raw_input().split())
A = map(int,raw_input().split())

def myfunc(N, x, A):
  A = np.array(A)
  B = deepcopy(A)
  m = sum(B)
  for idx in xrange(1,N):
    A = np.append(A, A[0])
    A = np.delete(A, 0)
    np.fmin(B, A, out=B)
    m = min(np.sum(B)+idx*x, m)
  print int(m)

myfunc(N, x, A)

Submission status: Accepted

Solution code:
import numpy as np
from copy import deepcopy


N, x = map(int,raw_input().split())
A = map(int,raw_input().split())

def myfunc(N, x, A):
  B = deepcopy(np.array(A))
  m = sum(B)
  def apppop(A):
    A.append(A[0])
    A.pop(0)
  for idx in xrange(1,N):
    apppop(A)
    np.fmin(B, A, out=B)
    m = min(np.sum(B)+idx*x, m)
  print int(m)

myfunc(N, x, A)

Submission status: Accepted

Solution code:
import time
import numpy as np
import copy

tflag = False
if tflag:
  N = 2000
  m = 1000000000
  x = np.random.randint(m)
  A = list(np.random.randint(m,size=(N)))
  st = time.time()
else:
  N, x = map(int,raw_input().split())
  A = map(int,raw_input().split())

#B = copy.deepcopy(A)
B = A
A = A + A
m = sum(B)
for idx in xrange(1,N):
  for jdx in xrange(N):
    if B[jdx] > A[jdx+N-idx]:
      B[jdx] = A[jdx+N-idx]
#  A = np.append(A, A[0])
#  A = np.delete(A, 0)
#  B = np.fmin(B,A)
  m = min(sum(B)+idx*x, m)

print int(m)

Submission status: Accepted

Solution code:
n,x=map(int,raw_input().split())
a=map(int,raw_input().split())
ans=float('inf')
b=[[float('inf')]*n for _ in xrange(n)]
for i in xrange(n):
    b[i][0]=a[i]
for i in xrange(n):
    for j in xrange(1,n):
        b[i][j]=min(b[i][j-1],a[(i-j)%n])
for j in xrange(n):
    tmp=0
    for i in xrange(n):
        tmp+=b[i][j]
    ans=min(ans,tmp+j*x)
print(ans)

Submission status: Accepted

Solution code:
n, x = map(int, raw_input().split())
a = map(int, raw_input().split())
ans = 10 ** 18
for i in xrange(n):
    t = sum(a) + x * i
    if ans > t:
        ans = t
    t = a[-1]
    for j in xrange(n - 1, 0, -1):
        if a[j] > a[j-1]:
            a[j] = a[j-1]
    if a[0] > t:
        a[0] = t
print ans

Submission status: Accepted

Solution code:
def solve(n, x, a):
	costs = []
	cost_min = 0
	for k in range(n):
		if k == 0:
			costs = a.copy()
		else:
			for i in range(n):
				costs[i] = min(costs[i], a[(i-k)%n])
		cost_k = sum(costs) + k*x
		if k == 0:
			cost_min = cost_k
		else:
			cost_min = min(cost_min, cost_k)
			if cost_min != cost_k:
				break
		if cost_min < (k+1) *x:
			break

	return cost_min
n, x = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
print(solve(n,x,a))

Submission status: Accepted

Solution code:
n, x = map(int, raw_input().split())
a = map(int, raw_input().split())
b = a[:]
a = a + a
ans = 10 ** 18
for i in xrange(n):
    for j in xrange(n):
        if b[j] > a[j+n-i]:
            b[j] = a[j+n-i]
    t = sum(b) + x * i
    if ans > t:
        ans = t
print ans

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**9

def f():
    n,x = list(map(int, input().split()))
    a = list(map(int, input().split()))
    mi = a.index(min(a))
    a = a[mi:] + a[:mi]
    r = sum(a)
    for t in range(n-1):
        for i in range(n-1,0,-1):
            a[i] = min(a[i],a[i-1])
        tr = ((t+1)*x) + sum(a)
        if r > tr:
            r = tr
        else:
            break
    return r

print(f())

Submission status: Runtime Error

Solution code:
# 0-indexed
class SegTree:
    def __init__(self, vals, seg_f, ide_ele):
        n = len(vals)
        self.seg_f = seg_f
        self.ide_ele = ide_ele
        self.leaf_n = 1 << (n-1).bit_length()
        self.tree = [ide_ele]*(2*self.leaf_n-1)
        for i in range(n):  # init leaf
            self.tree[i+self.leaf_n-1] = vals[i]
        for i in range(self.leaf_n-2, -1, -1):  # bottom-up
            self.tree[i] = self.seg_f(self.tree[2*i+1], self.tree[2*i+2])

    def update(self, i, x):
        i += (self.leaf_n-1)
        self.tree[i] = x
        while i > 0:
            i = (i-1)//2
            self.tree[i] = self.seg_f(self.tree[2*i+1], self.tree[2*i+2])

    # [l,r)
    def query(self, l, r):
        v_l = self.ide_ele
        v_r = self.ide_ele
        l += (self.leaf_n-1)
        r += (self.leaf_n-1)
        while l < r:
            if l&1 == 0:
                v_l = self.seg_f(v_l, self.tree[l])
            if r&1 == 0:
                v_r = self.seg_f(v_r, self.tree[r-1])
                r -= 1
            l >>= 1
            r >>= 1
        return self.seg_f(v_l ,v_r)


'''
def add(a,b): return a+b
st = SegTree(vl,add,0)
st = SegTree(vl,(lambda a,b: a+b),0)
st = SegTree(vl,min,10**10)

print(st.query(2,4))
st.update(4,-4)
print(st.query(2,5))
st.update(0,-10)
print(st.query(0,5))
'''
n,x = map(int, input().split())
al = list(map(int, input().split()))

st = SegTree(al, min, 10**18)
ans = 10**18
for i in range(0,n): # move cnt
    curr_i_ans = i*x
    for j in range(n): # ind
        l = j-i
        if l >= 0:
            amin = st.query(l,j+1)
        else:
            amin1 = st.query(0,j+1)
            amin2 = st.query(l+n,n+1)
            amin = min(amin1,amin2)
        curr_i_ans += amin
    ans = min(ans, curr_i_ans) 
print(ans)

Submission status: Runtime Error

Solution code:
n,x=map(int,input().split())
A=list(map(int,input().split()))
ans=0
dp=[[A[i]]*n for i in range(n)]
for l in range(n):
  a=A[l:]+A[:l]
  a=a[::-1]
  for i in range(n):
    dp[l][i+1]=min(dp[l][i],a[i]+x*(i+1))
print(sum(map(lambda x:x[-1], dp)))

Submission status: Runtime Error

Solution code:
n,x=map(int,input().split())
A=list(map(int,input().split()))
ans=0
m=0
r=0
for l in range(n):
  a=A[l:]+A[:l]
  a=a[::-1]
  tmp=A[l]
  r=A[r]
  for i in range(n):
    if tmp>a[i]+x*(i+1):
      r=a[i]
      tmp=a[i]+x*(i+1)
      m=max(i+1,m)
  ans+=r
print(ans+m*x)

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,i = na()
a = na()
dp = [i for i in a]
ans = sum(dp)

for i in range(1,n):
    for j in range(n):
        dp[(j+i)%n] = min(dp[(j+i)%n], a[j])
    ans = min(ans, sum(dp)+x*i)

print(ans)

Submission status: Runtime Error

Solution code:
from itertools import accumulate as acc
N, x, *A = map(int, open('0').read().split())
dp = []
for i in range(N):
  dp.append(list(acc((A[(i-j)%N] for j in range(N)),min)))

ans = 10**10
for i,a in enumerate(zip(*dp)):
  ans = min(ans,i*x+sum(a))
print(ans)

Submission status: Runtime Error

Solution code:
def segfunc(x, y):
    return min(x, y)

class segment_():
    def __init__(self, init_val, n, segfunc):
        self.ide_ele = 10**9+1
        # num: n以上の最小の2のべき乗
        self.num = 2**(n-1).bit_length()
        self.seg = [self.ide_ele]*2*self.num
        self.segfunc = segfunc
        # set_val
        for i in range(n):
            self.seg[i+self.num-1] = init_val[i]
        # built
        for i in range(self.num-2, -1, -1):
            self.seg[i] = self.segfunc(self.seg[2*i+1], self.seg[2*i+2])

    def update(self, k, x):
        k += self.num - 1
        self.seg[k] = x
        while k:
            k = (k-1)//2
            self.seg[k] = self.segfunc(self.seg[2*k+1], self.seg[2*k+2])

    def query(self, p, q):
        if q <= p:
            return self.ide_ele
        p += self.num - 1
        q += self.num - 2
        res = self.ide_ele
        while q-p>1:
            if p&1 == 0:
                res = self.segfunc(res, self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res, self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res, self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
        return res

def main():
    n, x = list(map(int,sys.stdin.readline().split()))
    A = list(map(int,sys.stdin.readline().split()))

    seg_ = segment_(A, n, lambda a, b: min(a, b))
    ans = 10**18
    for k in range(n):
        t = k*x
        for i in range(n):
            if k <= i:
                t += seg_.query(i-k, i+1)
            else:
                t += min(seg_.query(0, i+1), seg_.query(n-(k-i), n))
        ans = min(t, ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,x = map(int, input().split())
a = list(map(int, input().split()))

ans = 0
mx = 0
inf = float('INF')
for i in range(n):
    mn = a[i]
    con = a[i]
    for j in range(i-n,i+1):
        idx = (i-j)%n
        if mn > a[idx] + (i-j) * x:
            mn = a[idx] + (i-j) * x
            con = a[idx]
            if mx < tmp:
                mx = tmp
    ans += con
print(ans + mx * x)

Submission status: Runtime Error

Solution code:
n,x=map(int,input().split())
a=list(map(int,input().split()))
a*=2
INF=10**15
cost=[INF]*n
b=[(c,d) for i in enumerate(a)]
b.sort(key=lambda x:x[1])
start=b[0][1]
cost[start]=a[start]
for i in range(start+1,start+n):
    cost[i%n]=min(cost[(i-1)%n]+x,cost[i%n])
ans=0
for i in cost:
    if i==INF:continue
    ans+=i
print(ans)

Submission status: Runtime Error

Solution code:
from copy import deepcopy as dc
#n,x=map(int,input().split())
x=int(input()) #
a=list(map(int,input().split()))

n=len(a) #
a=[[i,i,0] for i in a]
aa=dc(a)
b=[]

m=0
while(b!=a):
    b=dc(a)
    for _ in range(2):
        for i in range(n):
            j=i-1
            if j==-1:
                j=n-1
            if a[i][1]!=a[j][1]:
                if aa[i][0]>=a[j][1]+x or (aa[i][0]>=a[j][1] and a[j][2]<m):
                    a[i][0]=a[j][1]+x
                    a[i][1]=a[j][1]
                    a[i][2]=a[j][2]+1
    for i in a:
        m=max(i[2],m)
        
s=0
s+=m*x
for i in a:
    s+=i[1]
print(s)
#print(a)

Submission status: Runtime Error

Solution code:
from copy import deepcopy as dc
#n,x=map(int,input().split())
x=int(input()) #
a=list(map(int,input().split()))
n=len(a) #
a=[[i,i,0] for i in a]
b=[]

m=0
while(b!=a):
    b=dc(a)
    for _ in range(2):
        for i in range(n):
            j=i-1
            if j==-1:
                j=n-1
            if a[i][1]!=a[j][1]:
                if a[i][0]>=a[j][1]+x or (a[i][0]>=a[j][1] and a[j][2]<m):
                    a[i][0]=a[j][1]+x
                    a[i][1]=a[j][1]
                    a[i][2]=a[j][2]+1
    for i in a:
        m=max(i[2],m)
        
s=0
s+=m*x
for i in a:
    s+=i[1]
print(s)

Submission status: Runtime Error

Solution code:
N,x = int(input().split())
a = sorted(list(map(int,input().split())))
print(min(N*a[0]+(N-1)*x,sum(a)))

Submission status: Runtime Error

Solution code:
n,x=map(int,input().split())
a=list(map(int,input().split()))
t=[a[i] for i in range(n)]
sp_cnt=[0 for i in range(n)]
for i in range(1,n):
    if a[n-i]+i*x<t[0]:
        t[0]=a[n-i]
        sp_cnt[0]=i

for i in range(1,n):
    for j in range(n):
        if i-j>=0:
            if a[i-j]+j*x<t[i]:
                t[i]=a[i-j]
                sp_cnt[i]=j
            else:
                if a[n+(i-j)]+j*x<t[i]:
                t[i]=a[n+(i-j)]
                sp_cnt[i]=j

max_sp_cnt=max(sp_cnt)

ans=sum(t)+max_sp_cnt*x
print(ans)

Submission status: Runtime Error

Solution code:
n,x=map(int,input().split())
a=list(map(int,input().split()))
t=[a[i] for i in range(n)]
sp_cnt=[0 for i in range(n)]
for i in range(1,n):
    if a[n-i]+i*x<t[0]:
        t[0]=a[n-i]
        sp_cnt[0]=i

for i in range(1,n):
    for j in range(n):
        if i-j>=0:
            if a[i-j]+j*x<t[i]:
                t[i]=a[i-j]
                sp_cnt[i]=j
            else:
                if a[n-(i-j)]+j*x<t[i]:
                t[i]=a[n-(i-j)]
                sp_cnt[i]=j

max_sp_cnt=max(sp_cnt)

ans=sum(t)+max_sp_cnt*x
print(ans,t)

Submission status: Runtime Error

Solution code:
n,x=map(int,input().split())
a=list(map(int,input().split()))
dp=[a[i] for i in range(n)]
sp_cnt[0 for i in range(n)]
for i in range(1,n):
    if a[n-i]+i*x<dp[0]:
        dp[0]=a[n-1]+i*x
        sp_cnt[0]=i

for i in range(1,n):
    for j in range(i):
        if a[i-j]+j*x<dp[0]:
            dp[i]=a[i-j]+j*x
            sp_cnt[i]=max(sp_cnt[i],j)
            
all_sp_cnt=sum(sp_cnt)
max_sp_cnt=max(sp_cnt)

ans=sum(dp)-(all_sp_cnt-max_sp_cnt)*x
print(ans)

Submission status: Runtime Error

Solution code:
import sys


fin = sys.stdin.readline
N, x = [int(elem) for elem in fin().split()]
a_list = [int(elem) for elem in fin().split()]

min_cost_for = a_list.copy()
this_is_from = list(range(N))

max_num_magics = 0
for i, a in enumerate(a_list): 
    for j, other_a in enumerate(a_list):
        if i == j: continue
        num_magics = i - j if j < i else (N - j + i)
        cost = other_a + num_magics * x
        if cost < min_cost_for[i]:
            min_cost_for[i] = cost
            max_num_magics = max(num_magics, max_num_magics)
            this_is_from[i] = j


total_cost = sum(a_list[origin] for origin in this_is_from) + x * max_num_magics
print(total_cost)

Submission status: Runtime Error

Solution code:
N,X = map(int, input().split())
A = list(map(int, input().split()))

# ans = 10**13
ans = float("inf")

for k in range(N):
    tmp = k * X  

    for i in range(2*N):
        j = (i-k+N*4) % N
        A[i] = min(A[i], A[j])
    tmp += sum(A)

    ans = min(ans, tmp)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N, x = map(int, input().split())
a = np.array(map(int, input().split()))
b = np.copy(a)
ans = float('inf') 
for i in range(N):
    c = np.roll(a,i)
    b = np.minimum(b,c)
    ans = min(ans, sum(b)+i*x)
print(ans)

Submission status: Runtime Error

Solution code:

import numpy as np
N, x = map(int, input().split())
a = np.array(map(int, input().split()))
b = np.copy(a)
ans = float('inf') 
for i in range(N):
    c = np.roll(a,i)
    b = np.minimum(b,c)
    ans = min(ans, sum(b)+i*x)
print(ans)

Submission status: Runtime Error

Solution code:
def inpl(): return [int(i) for i in input().split()]
import numpy as np
N, x = inpl()
a = np.array(inpl())
b = np.copy(a)
ans = float('inf') 
for i in range(N):
    c = np.roll(a,i)
    b = np.minimum(b,c)
    ans = min(ans, sum(b)+i*x)
print(ans)

Submission status: Runtime Error

Solution code:
n,x=map(int,input().split())
li=list(map(int,input().split()))
mi=min(li)
ind=-1
for i in range(n):
    if mi==li[i]:
        ind=i
ans=[0]*n
ans[0]=li[ind]
k=0
for i in range(1,n):
    if ind+i>=n:
        k=ind+i-n
    else:
        k=ind+i
    ans[i]=min(li[k],ans[i-1]+x)
print(sun(ans))

Submission status: Runtime Error

Solution code:
loop, magic_cost = map(int, input().split(" "))
cost = [n for n in map(int, input().split(" "))]
answer = float("inf")
min_costs = [float(inf) for n in range(loop)]
for i in range(loop):
    for j in range(loop):
        min_costs[j] = min(min_costs[j], cost[(i - j) % loop])
    answer = min(answer, sum(min_costs) + (magic_cost * (i) ) )
print(answer)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <functional>
#include <string>
#include <stack>
#include <set>
#include <sstream>
#include <iomanip>
#include <limits>
using namespace std;
using ll = long long;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, pair<ll, ll>> PP;

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll INF = 1 << 30;
const ll INF2 = 9e18;
const double INF3 = 9e14;
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
const int tx[8] = { -1,0,1,-1,1,-1,0,1 }, ty[8] = { -1,-1,-1,0,0,1,1,1 };
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define fr first
#define sc second



int main() {
	ll n, x, a[2010], ans = 0;
	vector<ll>v[2010];
	cin >> n >> x;
	for (int i = 0;i < n;i++)cin >> a[i];
	for (int i = 0;i < n;i++) {
		for (int j = 0;j < n;j++) {
			if (i == j)v[j].pb(a[i]);
			else v[j].pb(a[i] + x);
		}
	}
	for (int i = 0;i < n;i++) {
		sort(ALL(v[i]));
	}
	for (int i = 0;i < n;i++) {
		ans += v[i][0];
	}
	cout << ans << endl;
	return 0;
}

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int mx = 4005;
const ll inf = 4e18;
ll dp[mx], rdp[mx];
ll a[mx];
ll x;
int n;

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    cin >> n >> x;
    for(int i = 0; i < n; i++) cin >> a[i];
    copy(a,a+n,a+n);
    for(int i = 0; i < 2 * n; i++){
        dp[i] = min((i ? dp[i-1] : inf) + x, a[i]);
    }
    for(int i = 2 * n; i--; ){
        rdp[i] = min((i < 2*n-1 ? dp[i+1] : inf) + x, a[i]);
    }
    ll ans = 0;
    for(int i = 0; i < n; i++){
        ans += min({dp[i],dp[n+i],rdp[i],rdp[n+i]});
    }
    cout << ans << '\n';
}

Submission status: Runtime Error

Solution code:
n,x=map(int,raw_input().split(" "))
a=map(int,raw_input().split(" "))
t=[i for i in a]
ans=10**10
for m in range(n):
    t=[min(t[j],a[(j-m)%n]) for j in range(n)]
    s=min(x*m+sum(t),s)
print s

Submission status: Runtime Error

Solution code:
N, x = map(int, raw_input().split())
A = map(int, raw_input().split())
A = A+A

b = [[A[i-j]+x*j for i in range(N,2*N)] for j in range(N)]
c = [min(bb) for bb in b]
d = [bb.index(cc) for cc in c]

print sum(c) - x * (sum(d)-max(d))

Submission status: Runtime Error

Solution code:
n, x = map(int, raw_input().split())
a = map(int, raw_input().split())
ans = 0
b = []
for k in range(n):
    if k == 0:
        b = a[:]
    else:
        for i in range(n):
            b[i] = min(a[(i-k)%n],b[i])
    cost = k*x+sum(b)
    if k == 0:
        ans = cost
    else:
        ans = min(ans,cost)
        if ans != cost:
            break
    if ans < (k+1)*x:
        break
print(ans)

Submission status: Runtime Error

Solution code:
n, x = map(int, raw_input().split())
a = map(int, raw_input().split())
ans = 0
b = []
for k in range(n):
    if k == 0:
        b = a.copy()
    else:
        for i in range(n):
            b[i] = min(a[(i-k)%n],b[i])
    cost = k*x+sum(b)
    if k == 0:
        ans = cost
    else:
        ans = min(ans,cost)
        if ans != cost:
            break
    if ans < (k+1)*x:
        break
print ans

Submission status: Runtime Error

Solution code:
n, x = map(int, raw_input().split())
a = map(int, raw_input().split())
b = a[:]
for k in range(n):
    for i in range(n):
        b[i] = min(a[i-k],b[i])
    ans = min(ans,k*x+sum(b))
print ans

Submission status: Runtime Error

Solution code:
def solve_():
    import itertools
    N, x = map(int, input().split())
    a = list(map(int, input().split()))
    s_cost = a.copy()
    cost = sum(s_cost)

    for k, i in itertools.product(range(1, N), range(N)):
        s_cost[i] = a[(i - k) % N] if s_cost > a[(i - k) % N] else s_cost[i]
        if i == N - 1 and cost > k*x + sum(s_cost):
            cost = k*x + sum(s_cost)
    
    print(cost)

solve()

Submission status: Runtime Error

Solution code:
import math

N, x = map(int, raw_input().split())
A = map(int, raw_input().split())
ans = 1e18
b = [[ans for i in range(N)]]
for k in range(N):
    sum = k*x
    for i in range(N):
        b[i] = min(b[i], A[(i+N-k)%N])
        sum += b[i]
    ans = min(ans, sum)
print ans

Submission status: Runtime Error

Solution code:
N, x = list(map(int, input().split()))
a = list(map(int, input().split()))
s_cost = a
cost = sum(s_cost)
for k in xrange(0,N):
    s_cost = [min(s_cost[x],a[y]) for (x,y) in zip( list(xrange(N)), list(xrange(N-k,N))+list(xrange(N-k)) )]
    cost = min(cost, k*x + sum(s_cost))

print(cost)