Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains a_{i} liters of paint of color i.

Vika also has an infinitely long rectangular piece of paper of width 1, consisting of squares of size 1 × 1. Squares are numbered 1, 2, 3 and so on. Vika decided that she will start painting squares one by one from left to right, starting from the square number 1 and some arbitrary color. If the square was painted in color x, then the next square will be painted in color x + 1. In case of x = n, next square is painted in color 1. If there is no more paint of the color Vika wants to use now, then she stops.

Square is always painted in only one color, and it takes exactly 1 liter of paint. Your task is to calculate the maximum number of squares that might be painted, if Vika chooses right color to paint the first square.


-----Input-----

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of jars with colors Vika has.

The second line of the input contains a sequence of integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9), where a_{i} is equal to the number of liters of paint in the i-th jar, i.e. the number of liters of color i that Vika has.


-----Output-----

The only line of the output should contain a single integer — the maximum number of squares that Vika can paint if she follows the rules described above.


-----Examples-----
Input
5
2 4 2 3 3

Output
12

Input
3
5 5 5

Output
15

Input
6
10 10 10 1 10 10

Output
11



-----Note-----

In the first sample the best strategy is to start painting using color 4. Then the squares will be painted in the following colors (from left to right): 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5.

In the second sample Vika can start to paint using any color.

In the third sample Vika should start painting using color number 5.

Difficulty: EASY
Tags: Implementation

Constructive algorithms

Starter code:

Solutions:
n = int(input())
a = list(map(int, input().split()))
x = min(a)
count = x * n
m = []
for i in range(n):
	if a[i] == x:
		m.append(i)
y = len(m)
z = m[0]
for i in range(y - 1):
	m[i] = m[i + 1] - m[i] - 1
m[-1] = n - (m[-1] - z) - 1
count += max(m)
print(count)


n = int(input())
R = lambda : map(int, input().split())
v = list(R())
m = min(v)
v = v + v
vi = [i for i in range(len(v)) if v[i] == m]
di = max([vi[i] - vi[i - 1] for i in range(1, len(vi))])
print(n * m + di - 1)


N = int(input())
x = list(map(int, input().split()))
f = min(x)
ans = N * f
curlen = 0
maxlen = 0
index = 0
while index < 2 * N:
	if x[index % N] == f:
		maxlen = max(maxlen, curlen)
		curlen = 0
	else:
		curlen += 1
	index += 1
maxlen = max(maxlen, curlen)
print(ans + maxlen)


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
sys.setrecursionlimit(10 ** 5)
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
n = int(input())
l = list(map(int, input().split()))
z = min(l)
posn = []
for i in range(n):
	if l[i] == z:
		posn.append(i)
ans = z * n
maxi = -inf
for i in range(len(l)):
	z = bisect_left(posn, i)
	if z == len(posn):
		maxi = max(n - i + posn[0], maxi)
	else:
		maxi = max(posn[z] - i, maxi)
print(ans + maxi)


n = int(input())
l = list(map(int, input().split()))
index_position = []
minimum = min(l)
for i in range(len(l)):
	if l[i] == minimum:
		index_position.append(i)
index_minimum = index_position[0]
index_minimimu = n - 1 - index_position[-1]
maxi = index_minimimu + index_minimum
for i in range(1, len(index_position)):
	if index_position[i] - index_position[i - 1] - 1 > maxi:
		maxi = index_position[i] - index_position[i - 1] - 1
print(n * minimum + maxi)


import sys
import math
MAXNUM = math.inf
MINNUM = -1 * math.inf

def getInt():
	return int(sys.stdin.readline().rstrip())

def getInts():
	return map(int, sys.stdin.readline().rstrip().split(' '))

def getString():
	return sys.stdin.readline().rstrip()

def printOutput(ans):
	sys.stdout.write(str(ans) + '\n')
	pass

def solve(jars, colors):
	mx = 0
	counter = 0
	minPaint = min(colors)
	for i in range(0, jars * 2 + 1):
		cur = i % jars
		if colors[cur] == minPaint:
			if counter > mx:
				mx = counter
			counter = 0
		else:
			counter += 1
	return jars * minPaint + mx

def readinput():
	jars = getInt()
	colors = list(getInts())
	printOutput(solve(jars, colors))
readinput()


n = int(input())
arr = list(map(int, input().split()))
(l, c, ext) = (min(arr), 0, 0)
id = arr.index(l)
for i in range(id, n + id + 1):
	if arr[i % n] == l:
		c = 0
	else:
		c += 1
	ext = max(ext, c)
print(l * n + ext)


n = int(input())
a = list(map(int, input().split()))
m = min(a)
pos = []
for i in range(n):
	if a[i] == m:
		pos.append(i)
cnt = 0
ans = -1
for i in range(len(pos)):
	cnt = m * n
	x = pos[i]
	if i == len(pos) - 1:
		cnt += n - 1 - x
		cnt += pos[0]
	else:
		cnt += pos[i + 1] - pos[i] - 1
	ans = max(ans, cnt)
print(ans)


n = int(input())
a = list(map(int, input().strip().split(' ')))
p = min(a)
lf = []
rf = []
c1 = 0
c2 = 0
kc = -1
for i in range(n):
	if i == 0:
		if a[i] == p:
			lf.append(i)
			kc = 1
			c1 += 1
		else:
			lf.append(-1)
			c1 += 1
	elif a[i] == p and kc == -1:
		lf.append(i)
		kc = 1
		c1 += 1
	else:
		lf.append(lf[c1 - 1])
		c1 += 1
for i in range(n - 1, -1, -1):
	if i == n - 1:
		if a[i] == p:
			rf.append(i)
			c2 += 1
		else:
			rf.append(-1)
			c2 += 1
	elif a[i] == p:
		rf.append(i)
		c2 += 1
	else:
		rf.append(rf[c2 - 1])
		c2 += 1
rf.reverse()
ans = p * n
kk = 0
for i in range(n):
	if lf[i] != -1 and rf[i] != -1 and (a[i] != p):
		kk = max(kk, rf[i] - i)
	elif lf[i] != -1 and a[i] != p:
		kk = max(kk, lf[i] + n - i)
	elif rf[i] != -1 and a[i] != p:
		kk = max(kk, rf[i] - i)
ans += kk
print(ans)


import sys
n = int(input())
paint = list(map(int, input().split()))
(low, high) = (min(paint), max(paint))
if low == high:
	print(n * low)
	sys.exit()
(count, best) = (0, 0)
start = paint.index(low)
pos = start
while True:
	pos += 1
	if pos == n:
		pos = 0
	if pos == start:
		break
	if paint[pos] != low:
		count += 1
		best = max(best, count)
	else:
		count = 0
print(n * low + best)


import sys
from sys import stdin, stdout

def main():
	n = int(stdin.readline())
	listaPinturas = [int(x) for x in stdin.readline().split()]
	valMin = min(listaPinturas)
	indice = 0
	for i in range(n):
		if listaPinturas[i] == valMin:
			indice = i
	maxLineasPintadas = 0
	maxLineasPintadasTemporal = 0
	for i in range(n + 1):
		if listaPinturas[indice] != valMin:
			maxLineasPintadasTemporal += 1
		else:
			if maxLineasPintadasTemporal > maxLineasPintadas:
				maxLineasPintadas = maxLineasPintadasTemporal
			maxLineasPintadasTemporal = 0
		indice = indice - 1 + (n - indice) // n * n
	maxLineasPintadas += n * valMin
	print(str(maxLineasPintadas))
main()


n = int(input())
l = list(map(int, input().split()))
g = min(l)
r = []
t = 0
k = 0
for i in range(n):
	if l[i] == g and t == 0:
		k = 0
		t = 1
	elif l[i] == g and t == 1:
		r.append(k)
		k = 0
	else:
		k = k + 1
t = 1
for i in range(n):
	if l[i] == g:
		r.append(k)
		break
	else:
		k = k + 1
print(max(r) + g * n)


n = int(input())
v = [int(x) for x in input().split()]
low = min(v)
res = low * n
v = [x - low for x in v]
v *= 2
start = -1
sol = 0
for (i, x) in enumerate(v):
	if x != 0:
		if start < 0:
			start = i
	elif start >= 0:
		sol = max(sol, i - start)
		start = -1
res += sol
print(res)


def find_right(x, data):
	answer = -1
	for i in range(len(data)):
		if data[i] == x:
			answer = i
	return answer

def find_left(x, data):
	for i in range(len(data)):
		if data[i] == x:
			return i
n = int(input())
data = list(map(int, input().split()))
min1 = min(data)
index_min_r = find_right(min1, data)
index_min_l = find_left(min1, data)
k = data.count(min1)
c = index_min_r - index_min_l - 1
help = []
for i in range(n):
	if data[i] == min1:
		help.append(i)
answer = []
for i in range(1, len(help)):
	answer.append(help[i] - help[i - 1] - 1)
answer.append(n - (help[-1] - help[0]) - 1)
print(max(answer) + n * min1)


n = int(input())
color = list(map(int, input().split()))
Min = color[0]
index = [0]
for i in range(1, n):
	if color[i] < Min:
		Min = color[i]
		index = [i]
	elif color[i] == Min:
		index.append(i)
if len(index) == 1:
	total = (Min + 1) * n - 1
else:
	Max = n - (index[-1] - index[0])
	for i in range(len(index) - 1):
		diff = index[i + 1] - index[i]
		if diff > Max:
			Max = diff
	total = Min * n + Max - 1
print(total)


from collections import defaultdict, deque, Counter, OrderedDict
from bisect import insort, bisect_right, bisect_left
import threading, sys

def main():
	n = int(input())
	l = [int(i) for i in input().split()]
	m = min(l)
	pos = []
	for (i, v) in enumerate(l):
		if v == m:
			pos.append(i)
	if len(pos) > 1:
		c = []
		for i in range(len(pos) - 1):
			c.append((pos[i + 1] - pos[i] - 1, pos[i]))
		f = n - (pos[-1] - pos[0] + 1)
		c.append((f, pos[-1]))
		c = sorted(c)
		index = c[-1][0]
		ans = n * m + index
		print(ans)
	else:
		ans = n * m + n - 1
		print(ans)
thread = threading.Thread(target=main)
thread.start()


n = int(input())
m = list(map(int, input().split()))
l = m + m
a = min(m)
i = l.index(a)
ans = 0
while i < n:
	j = l.index(a, i + 1, 2 * n)
	if j >= n:
		c = (j - i - 1) * (a + 1) + (n - (j - i - 1)) * a
		if c > ans:
			ans = c
		break
	else:
		c = (j - i - 1) * (1 + a) + (n - (j - i - 1)) * a
		if c > ans:
			ans = c
		i = j
print(ans)


n = int(input())
mas = [int(x) for x in input().split()]
r = min(mas)
mas = [0] + mas + [0] * 200000
for i in range(1, n + 1):
	mas[i] -= r
	mas[i + n] = mas[i]
c = p = 0
for i in range(1, 2 * n + 1):
	if mas[i] > 0:
		c += 1
		if c > p:
			p = c
	else:
		c = 0
print(p + r * n)


n = int(input())
L = list(map(int, input().split()))
x = min(L)
if all((e == x for e in L)):
	print(x * n)
else:
	M = []
	for i in range(0, len(L)):
		if L[i] == x:
			M.append(i)
	if len(M) == 1:
		print(n * x + (n - 1))
	else:
		temp = n - M[-1] + M[0] - 1
		for i in range(0, len(M) - 1):
			p = M[i + 1] - M[i] - 1
			if p > temp:
				temp = p
			a = n * x + temp
		print(a)


n = int(input())
a = list(map(int, input().split()))
m = min(a)
a = ['#' if i == m else '+' for i in a]
a = ''.join(a)
flag = a[0] != '#' and a[-1] != '#'
a = [len(i) for i in a.split('#')]
m *= n
if flag:
	k = max(a[1:-1]) if len(a) > 2 else 0
	m += k if k > a[0] + a[-1] else a[0] + a[-1]
else:
	m += max(a)
print(m)


n = int(input())
col = list(map(int, input().split()))
low = min(col)
start = int()
store = list()
dist = list()
for i in range(len(col)):
	if col[i] == low:
		store.append(i + 1)
		if i + 1 in range(len(col)):
			start = col[i + 1]
		else:
			start = col[0]
for j in range(len(store)):
	if j == len(store) - 1:
		dist.append(n - store[-1] + store[0])
	else:
		dist.append(store[j + 1] - store[j])
high = max(dist)
if max(col) == min(col):
	print(max(col) * n)
elif len(store) == 1:
	print(n * (low + 1) - 1)
else:
	print(n * low + (high - 1))


am = 0
x = int(input())
l = list(map(int, input().split(' ')))
m = min(l)
k = [i for i in range(x) if l[i] == m]
k.append(k[0] + x)
for i in range(len(k) - 1):
	am = max(am, k[i + 1] - k[i])
print(m * x + am - 1)


x = int(input())
s = [int(i) for i in input().split()]
n = min(s)
count = n * x
t = 0
for i in range(x):
	s[i] -= n
s += s
m = 0
for i in s:
	if not i:
		m = max(m, t)
		t = 0
	else:
		t += 1
print(count + m)


n = int(input())
a = list(map(int, input().split()))
mini = min(a)
l = []
for i in range(n):
	if a[i] == mini:
		l.append(i)
l.append(l[0] + n)
res = mini * n
maxi = 0
for i in range(len(l) - 1):
	maxi = max(l[i + 1] - l[i] - 1, maxi)
print(res + maxi)


n = int(input())
v = [int(i) for i in input().split()]
a = min(v)
v2 = [i - a for i in v]
c = 0
v3 = []
for i in range(n):
	if v2[i] > 0:
		c = c + 1
	else:
		c = 0
	v3.append(c)
b = v3[-1]
if v2[0] > 0 and v2[-1] > 0:
	i = 0
	while v2[i] > 0 and i < n:
		i = i + 1
	b = b + i
print(a * n + max(max(v3), b))


n = int(input())
a = [int(i) for i in input().split()]
(pos, list) = (0, [0])
for i in range(1, n):
	if a[i] < a[pos]:
		(pos, list) = (i, [i])
	elif a[i] == a[pos]:
		list.append(i)
if len(list) == 1:
	print(n - 1 - pos + n * a[pos] + pos)
	exit(0)
elif len(list) == n:
	print(a[0] * n)
	exit(0)
list.append(n + list[0])
(pos, distance) = (0, list[1] - list[0])
for i in range(1, len(list) - 1):
	if list[i + 1] - list[i] > distance:
		(pos, distance) = (i, list[i + 1] - list[i])
ans = n * a[list[pos]] + list[pos + 1] - list[pos] - 1
print(ans)


n = int(input())
l = list(map(int, input().split()))
mi = min(l)
a = []
b = []
for i in range(n):
	if l[i] == mi:
		a.append(i)
for i in range(len(a) - 1):
	b.append(a[i + 1] - a[i] - 1)
b.append(a[0] + n - a[-1] - 1)
print(mi * n + max(b))


from sys import stdin, stdout
from math import floor
st = lambda : list(stdin.readline().strip())
li = lambda : list(map(int, stdin.readline().split()))
mp = lambda : map(int, stdin.readline().split())
inp = lambda : int(stdin.readline())
pr = lambda n: stdout.write(str(n) + '\n')
mod = 1000000007

def solve():
	n = inp()
	l = li()
	a = min(l)
	x = [i for i in range(n) if l[i] == a]
	ans = float('-inf')
	for i in x:
		j = (i + 1) % n
		p = n * a
		while l[j] != a:
			j = (j + 1) % n
			p += 1
		ans = max(ans, p)
	pr(ans)
for _ in range(1):
	solve()


def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def MAP2():
	return map(float, input().split())

def LIST():
	return list(map(int, input().split()))

def STRING():
	return input()
import string
import sys
from heapq import heappop, heappush
from bisect import *
from collections import deque, Counter
from math import *
from itertools import permutations, accumulate
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
n = INT()
arr = LIST()
mn = min(arr)
pos = 0
for i in range(n):
	if arr[i] == mn:
		pos = i
		break
c = 0
ans = 0
for i in arr:
	if i > mn:
		c += 1
	else:
		ans = max(ans, c)
		c = 0
ans = max(ans, c + pos)
res = mn * n + ans
print(res)


n = int(input())
a = list(map(int, input().split()))
h = min(a)
a = a + a
pos = a.index(h)
w = 0
for i in range(pos + 1, 2 * n):
	if a[i] == h:
		w = max(w, i - pos - 1)
		pos = i
print(h * n + w)


from os import path
import sys
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def read():
	sys.stdin = open('input1.txt', 'r')
	sys.stdout = open('output1.txt', 'w')

def powermod(a, b, m):
	a %= m
	res = 1
	if a == 0:
		return 0
	while b > 0:
		if b & 1:
			res = res * a % m
		b = b >> 1
		a = a * a % m
	return res

def isprime(n):
	if n <= 1:
		return 0
	for i in range(2, int(sqrt(n)) + 1):
		if n % i == 0:
			return 0
	return 1

def solve():
	n = ii()
	a = li()
	x = min(a)
	ans = n * x
	f = [0] * (2 * n)
	for i in range(n):
		f[i] = 1 if a[i] - x > 0 else 0
		f[i + n] = f[i]
	c = 0
	x = 0
	for i in range(2 * n):
		if f[i] == 0:
			x = max(x, c)
			c = 0
		else:
			c += 1
	x = max(x, c)
	print(x + ans)
if path.exists('input.txt'):
	read()
solve()


def areSame(a):
	if len(a) > 0:
		result = all((elem == a[0] for elem in a))
		if result:
			return True
		else:
			return False

def square(a, n):
	sum = 0
	if areSame(a):
		sum = a[0] * n
	else:
		for i in range(len(a)):
			if a[i] == min(a):
				reqidx = i
		start = reqidx + 1
		while start < len(a):
			if a[start] == 0:
				break
			else:
				sum += 1
				a[start] = a[start] - 1
			if start == len(a) - 1:
				start = 0
			else:
				start += 1
	return sum

def square2(a, n):
	mv = min(a)
	t = []
	for i in range(n):
		if a[i] == mv:
			t.append(i)
	md = n + t[0] - t[-1]
	for i in range(len(t) - 1):
		if t[i + 1] - t[i] > md:
			md = t[i + 1] - t[i]
	return mv * n + md - 1
num = int(input())
arr = list(map(int, input().split()))
print(square2(arr, num))


n = int(input())
a = list(map(int, input().split()))
numColors = len(a)
minLiter = min(a)
nonZeroLen = []
ans = 0
ans += numColors * minLiter
for i in range(len(a)):
	a[i] -= minLiter
counter = 0
for i in range(len(a)):
	if a[i] != 0:
		counter += 1
	else:
		nonZeroLen.append(counter)
		counter = 0
nonZeroLen.append(counter)
nonZeroLen.append(nonZeroLen[0] + nonZeroLen[len(nonZeroLen) - 1])
ans += max(nonZeroLen)
print(ans)


(a, b) = (int(input()), list(map(int, input().split())))
(d, s, c, e) = (0, 0, b.index(min(b)), min(b))
for i in range(a):
	if b[i] == e:
		d = max(d, s)
		s = 0
	else:
		s += 1
print(e * a + max(s + c, d))


n = int(input())
a = list(map(int, input().split()))
m = min(a)
b = [i for i in range(n) if a[i] == m]
c = float('-inf')
for i in b:
	j = (i + 1) % n
	t = n * m
	while a[j] != m:
		j = (j + 1) % n
		t += 1
	c = max(c, t)
print(c)


n = int(input())
l = list(map(int, input().split()))

def paint(l):
	n = len(l)
	m = min(l)
	a = []
	t = 0
	for i in range(n):
		if l[i] == m:
			a.append(i)
			t += 1
	b = []
	for j in range(t - 1):
		b.append(a[j + 1] - a[j])
	b.append(n - a[t - 1] + a[0])
	return m * n + max(b) - 1
print(paint(l))


n = int(input())
lis = list(map(int, input().split()))
minpos = None
minval = float('inf')
maxdiff = float('-inf')
difflis = list()
for (i, j) in enumerate(lis):
	if j <= minval:
		minval = j
for (i, j) in enumerate(lis):
	if j == minval:
		difflis.append(i)
		fp = difflis[0]
if len(difflis) == 1:
	ans = (n - 1) * (minval + 1) + minval
	print(ans)
	exit()
for i in range(1, len(difflis)):
	cur = difflis[i] - difflis[i - 1] - 1
	maxdiff = max(maxdiff, cur)
if len(set(lis)) == 1:
	print(minval * n)
	exit()
diff = n - difflis[len(difflis) - 1] + difflis[0] - 1
maxdiff = max(maxdiff, diff)
ans = maxdiff * (minval + 1) + (n - maxdiff) * minval
print(ans)


n = int(input())
arr = list(map(int, input().split()))
m = min(arr)
count = n * m
arr = arr + arr
prev = 0
dist = 0
for i in range(len(arr)):
	if arr[i] == m:
		if i - prev > dist:
			dist = i - prev
		prev = i
print(count + dist - 1)


n = int(input())
a = [int(x) for x in input().split()]
m = min(a)
mdist = 0
(fst, l) = (None, None)
for i in [i for (i, x) in enumerate(a) if x == m]:
	if fst is None:
		fst = l = i
	else:
		mdist = max(mdist, i - l - 1)
		l = i
mdist = max(mdist, fst + (n - l) - 1)
print(n * m + mdist)


n = int(input())
x = list(map(int, input().split()))
m = min(x)
(a, b) = (0, 0)
for i in range(n):
	if x[i] == m:
		a = i
		break
for i in range(n):
	if x[i] == m:
		b = i + 1
t = n - b + a
c = 0
for i in range(n):
	if x[i] == m:
		c = 0
	else:
		c += 1
		t = max(t, c)
print(m * n + t)


import sys
import math

def scan(TYPE_1, TYPE_2=0):
	if TYPE_1 == int:
		return map(int, sys.stdin.readline().strip().split())
	elif TYPE_1 == float:
		return map(float, sys.stdin.readline().strip().split())
	elif TYPE_1 == list and TYPE_2 == float:
		return list(map(float, sys.stdin.readline().strip().split()))
	elif TYPE_1 == list and TYPE_2 == int:
		return list(map(int, sys.stdin.readline().strip().split()))
	elif TYPE_1 == str:
		return sys.stdin.readline().strip()
	else:
		print('ERROR!!!!')

def main():
	n = int(scan(str))
	a = scan(list, int)
	mn = min(a)
	pos = [0]
	for i in range(n):
		if a[i] == mn:
			pos.append(i)
	pos.append(n)
	ans = 0
	for i in range(1, len(pos)):
		ans = max(ans, pos[i] - pos[i - 1] - 1)
	ans = max(ans, n - pos[-2] - 1 + pos[1])
	print(ans + mn * n)
main()


n = int(input())
a = list(map(int, input().split()))
mini = min(a)
r = 0
rmax = 0
beg = a.index(mini)
for i in range(beg + 1, n):
	if a[i] != mini:
		r += 1
	elif r > rmax:
		rmax = r
		r = 0
	else:
		r = 0
r += beg
if r > rmax:
	rmax = r
print(n * mini + rmax)


n = int(input())
l = list(map(int, input().split()))
mini = min(l)
for i in range(n):
	l[i] -= mini
res = mini * n
tmp = 0
maxi = 0
for i in range(2 * n):
	if l[i % n] > 0:
		tmp += 1
		if tmp > maxi:
			maxi = tmp
	else:
		tmp = 0
print(res + maxi)


import sys
N = int(input())
colors = list(map(int, input().split()))
minimumColor = min(colors)
a = None
b = None
gaps = set()
for (i, e) in enumerate(colors):
	if e != minimumColor:
		continue
	if a == None:
		a = i
		leftMostMin = i
	else:
		b = a
		a = i
		gaps.add(a - b - 1)
gaps.add(N - (a - leftMostMin) - 1)
print(N * minimumColor + max(gaps))


l = int(input())
t = list(map(int, input().split()))
m = min(t)
for i in range(l):
	t[i] -= m
t[0] = int(t[0] > 0)
for i in range(1, l):
	t[i] = (t[i] > 0) * t[i - 1] + (t[i] > 0)
i = 0
while t[i] > 0:
	t[i] += t[l - 1]
	i += 1
print(m * l + max(t))


n = int(input())
ps = list(map(int, input().split()))
mini = min(ps)
first = ps.index(mini)
ds = []
d = first
for p in reversed(ps):
	ds.append(d)
	d += 1
	if p == mini:
		d = 0
r = n * mini + max(ds)
print(r)


n = int(input())
l = list(map(int, input().split()))
min_val = min(l)
b = [i for i in range(n) if l[i] == min_val]
b.append(b[0] + n)
c = 0
f = b[0]
for i in b:
	c = max(c, i - f)
	f = i
print(min_val * n + c - 1)


n = int(input())
A = list(map(int, input().split()))
min = min(A)
if A.count(min) == 1:
	print(n * (min + 1) - 1)
else:
	temp = []
	start = 0
	for i in range(n):
		if A[i] == min:
			temp.append(i - start)
			start = i
	temp.append(n - start + A.index(min))
	max = max(temp)
	print(n * min + max - 1)


n = int(input())
arr = list(map(int, input().rstrip().split()))
index = [0]
minn = arr[0]
for i in range(1, n):
	if minn > arr[i]:
		minn = arr[i]
		index = [i]
	elif minn == arr[i]:
		index.append(i)
mi = 0
for i in range(1, len(index)):
	mi = max(mi, index[i] - index[i - 1] - 1)
mi = max(mi, n - 1 - index[-1] + index[0])

def maxx(n, index, minn):
	ans = minn * n + mi
	return ans
print(maxx(n, index, minn))


n = int(input())
arr = list(map(int, input().strip().split()))
m = min(arr)
i = arr.index(m)
c = 0
l = []
for j in range(n):
	if arr[j] == m:
		c += 1
		l.append(j)
d = 0
if c == 1:
	print(n * m + n - 1)
elif c > 1:
	for j in range(len(l) - 1):
		if l[j + 1] - l[j] >= d:
			d = l[j + 1] - l[j]
			i = l[j]
	if n + l[0] - l[-1] >= d:
		i = l[-1]
		d = n + l[0] - l[-1]
	print(n * m + d - 1)


n = int(input())
a = list(map(int, input().split()))
l = len(a)
m = min(a)
mi = []
b = 0
for i in range(l):
	if m == a[i]:
		mi.append(i)
if len(mi) > 1:
	c = l - 1 - mi[-1] + mi[0]
	for i in range(len(mi) - 1):
		b = mi[i + 1] - mi[i] - 1
		if b > c:
			c = b
	print(l * m + c)
else:
	print((m + 1) * l - 1)


n = int(input())
a = list(map(int, input().strip().split()))[:n]
val1 = min(a)
k = []
l = []
for i in range(n):
	if a[i] == val1:
		k.append(i)
for i in range(len(k) - 1):
	l.append(k[i + 1] - k[i])
if len(k) > 1:
	ans = n * val1 + max(max(l) - 1, n - max(k) - 1 + min(k))
	print(ans)
if len(k) == 1:
	ans = n * val1 + n - 1
	print(ans)


n = int(input())
a = list(map(int, input().split()))
m = a[0]
ind1 = 0
ind2 = 0
for i in range(1, n):
	if a[i] < m:
		m = a[i]
mi = []
for i in range(n):
	if a[i] == m:
		mi.append(i)
if len(mi) == 1:
	print(m * n + n - 1)
elif len(mi) == n:
	print(m * n)
else:
	mii = [0] * len(mi)
	for i in range(1, len(mi)):
		mii[i] = mi[i] - mi[i - 1]
	mii[0] = n - mi[len(mi) - 1] + mi[0]
	mm = mii.index(max(mii))
	if mm == 0:
		ind1 = mi[len(mi) - 1]
		ind2 = mi[0]
		print(m * n + n - ind1 + ind2 - 1)
	else:
		ind1 = mi[mm - 1]
		ind2 = mi[mm]
		x = m * n
		e = ind2 - ind1 - 1
		print(x + e)


n = int(input())
a = [int(x) for x in input().split()]
d = 0
mini = min(a)
maxd = 0
for x in range(2 * len(a)):
	if mini == a[x % n]:
		maxd = max(maxd, d)
		d = 0
	else:
		d += 1
maxd = max(maxd, d)
print(mini * n + maxd)


def squares(lst):
	(m, k) = (min(lst), 0)
	m_ind = len(lst) + lst.index(m)
	for i in range(len(lst) - 1, -1, -1):
		if lst[i] == m:
			m_ind = i
		k = max(k, m_ind - i)
	return m * len(lst) + k
n = int(input())
a = [int(i) for i in input().split()]
print(squares(a))


n = int(input())
a = list(map(int, input().split()))
minimum = min(a)
maximum = 0
ans = 0
ind = 0
challengers = []
for i in range(n):
	if a[i] != minimum and a[i - 1] == minimum:
		challengers.append(i)
while challengers:
	cnt = 0
	num = challengers.pop()
	similar = num
	while a[similar] != minimum:
		cnt += 1
		similar = (similar + 1) % n
	if cnt > maximum:
		maximum = cnt
		ind = num
ans = minimum * n
while a[ind] != minimum:
	ans += 1
	ind = (ind + 1) % n
print(ans)


n = int(input())
numbers = input().split(' ')
mi = 10000000007
for i in range(len(numbers)):
	mi = min(mi, int(numbers[i]))
for i in range(len(numbers)):
	numbers[i] = int(numbers[i]) - mi
ret = 0
length = 0
j = 0
while numbers[j] > 0:
	j += 1
for i in range(len(numbers)):
	if numbers[i] != 0:
		length += 1
	else:
		ret = max(ret, length)
		length = 0
ret = max(ret, length + j)
print(ret + n * mi)


n = int(input())
L = list(map(int, input().split()))
m = min(L)
i = L.index(m)
count = 0
c = 0
for j in range(i, len(L)):
	if L[j] == m:
		count = max(count, c)
		c = 0
	if L[j] != m:
		c += 1
i2 = L[::-1].index(m)
count = max(count, i + i2)
print(m * len(L) + count)


n = int(input())
a = list(map(int, input().split()))
min_a = min(a)
mass = []
for i in range(n):
	if a[i] == min_a:
		mass.append(i)
index = 0
value = min_a * n
for j in range(len(mass) - 1):
	if mass[j + 1] - mass[j] - 1 > index:
		index = mass[j + 1] - mass[j] - 1
		value = min_a * n + index
if len(mass) > 1:
	if n - mass[len(mass) - 1] - 1 + mass[0] > index:
		index = n - mass[len(mass) - 1] - 1 + mass[0]
		value = min_a * n + index
else:
	value = n * min_a + n - 1
print(value)


n = int(input())
L = list(map(int, input().split()))
M = min(L)
result = M * n
C = 0
tmp = 0
for i in L:
	if i != M:
		tmp += 1
	else:
		C = max(C, tmp)
		tmp = 0
if tmp:
	for i in L:
		if i != M:
			tmp += 1
		else:
			C = max(C, tmp)
			break
print(result + C)


col = int(input())
ll = list(map(int, input().split()))
a1 = col * min(ll)
m = min(ll)
s = []
t = 0
for i in ll:
	t += 1
	if i == m:
		s.append(t - 1)
		t = 0
s[0] += t
print(a1 + max(s))


n = int(input())
a = list(map(int, input().split()))
inf = 1061109567
x = inf
for i in a:
	x = min(i, x)
b = a * 2
len = 0
l = -1
for i in range(2 * n):
	if b[i] == x:
		len = max(len, i - l - 1)
		l = i
print(x * n + len)


n = int(input())
a = list(map(int, input().split()))
minimum = min(a)
max_segment = 0
count = 0
a = a + a
for i in range(len(a)):
	if a[i] == minimum:
		max_segment = max(count, max_segment)
		count = 0
	else:
		count += 1
max_segment = max(count, max_segment)
print(minimum * n + max_segment)


def zeroes(data):
	number = 0
	indices = []
	for i in range(len(data)):
		if data[i] == 0:
			number += 1
			indices.append(i)
	return (number, indices)
n = int(input())
data = list(map(int, input().split()))
x = min(data)
ans = x * n
data = [data[i] - x for i in range(n)]
(num, index) = zeroes(data)
m = n + index[0] - index[num - 1]
for i in range(num - 1):
	if index[i + 1] - index[i] > m:
		m = index[i + 1] - index[i]
ans += m - 1
print(ans)


n = int(input())
li = list(map(int, input().split()))
mn = min(li)
re = [i for i in range(0, n) if li[i] == mn]
mx = 0
for i in range(0, len(re)):
	mx = max(mx, (re[i] - re[i - 1]) % n)
if mx == 0:
	mx = n
print(mn * n + mx - 1)


def main():
	(n, aa) = (int(input()), list(map(int, input().split())))
	m = min(aa)
	(res, stop, x) = ([], aa.index(m) + 1, 0)
	for i in range(stop - n, stop):
		if aa[i] == m:
			res.append(x)
			x = 0
		else:
			x += 1
	print(m * n + max(res))
main()


n = int(input())
l = [int(x) for x in input().split()]
mini = min(l)
diff = None
lastmin = None
firstmin = None
for i in range(n):
	if l[i] == mini:
		if firstmin == None:
			firstmin = i
			lastmin = i
		else:
			x = i - lastmin - 1
			lastmin = i
			if diff == None:
				diff = x
			elif diff < x:
				diff = x
if diff == None:
	print(mini * n + n - 1)
else:
	if firstmin - lastmin + n - 1 > diff:
		diff = firstmin - lastmin + n - 1
	print(diff + mini * n)


from sys import *

def square(a):
	k = min(a)
	l = [i for (i, x) in enumerate(a) if x == k]
	if len(l) == 1:
		return n * k + (n - 1)
	else:
		b = [0] * len(l)
		for i in range(len(l) - 1):
			b[i] = l[i + 1] - l[i]
		b[len(l) - 1] = len(a) - l[len(l) - 1] + l[0]
		return n * k + (max(b) - 1)
n = int(input())
a = list(map(int, input().strip().split()))[:n]
print(square(a))


__author__ = 'Utena'
n = int(input())
s = list(map(int, input().split()))
m = min(s)
a = []
b = []
for i in range(n):
	if s[i] == m:
		a.append(i)
for t in range(len(a) - 1):
	b.append(a[t + 1] - a[t] - 1)
b.append(a[0] - a[-1] - 1 + n)
print(n * m + max(b))


n = int(input())
a = input()
A = list(map(int, list(a.split())))
i = 0
c = 0
p = 0
s = min(A)
l = []
if A.count(s) == n:
	print(s * n)
elif A.count(s) == 1:
	print(s * n + (n - 1))
else:
	while i < n:
		if A[i] == s:
			e = i
			prev = i
			j = i + 1
			while j < n:
				if A[j] == s:
					p = max(p, j - prev - 1)
					l.append(p)
					prev = j
				j = j + 1
			p = max(p, n - (prev - e) - 1)
			break
		else:
			i = i + 1
	print(s * n + p)


def squares(a):
	m = min(a)
	mi = [i for i in range(len(a)) if a[i] == m]
	if len(mi) == 1:
		mg = len(a) - 1
	else:
		mg = max(((mi[k] - mi[k - 1]) % len(a) for k in range(len(mi)))) - 1
	return m * len(a) + mg
n = int(input())
a = tuple(map(int, input().split()))
print(squares(a))


import itertools
import math
n = int(input())
a = [int(x) for x in input().split()]
amin = min(a)
b = list(filter(lambda x: x[1] == amin, enumerate(a)))
l = len(b)
k = max(((b[(i + 1) % l][0] - b[i][0] - 1) % n for i in range(l)))
print(amin * n + k)


def main():
	n = int(input())
	a = [int(i) for i in input().split()]
	mi = min(a)
	d = 0
	p = n + a.index(mi)
	for i in range(n - 1, -1, -1):
		if a[i] == mi:
			p = i
		d = max(d, p - i)
	print(mi * n + d)
main()


n = int(input())
a = list(map(int, input().split()))
m = min(a)
v = []
for i in range(n):
	if a[i] == m:
		idx = i
		v.append(i)
cnt = m * n
mi = 0
for i in range(1, len(v)):
	mi = max(mi, v[i] - v[i - 1] - 1)
mi = max(mi, n - (v[len(v) - 1] - v[0] + 1))
print(mi + cnt)


def max_diff(a2, lis2, lis3):
	a3 = []
	c1 = 0
	if j2[0] == len(lis2) - 1:
		index = 0
	else:
		index = j2[0] + 1
	for i2 in range(len(lis2)):
		if lis2[index] != a2:
			c1 += 1
		elif lis2[index] == a2:
			a3.append(c1)
			c1 = 0
		if index == len(lis2) - 1:
			index = 0
		else:
			index = index + 1
	return max(a3)

def peak(a1, lis):
	j3 = []
	for i1 in range(len(lis)):
		if lis[i1] == a1:
			j3.append(i1)
	return j3
n = int(input())
j1 = list(map(int, input().split(' ')))
mn = min(j1)
count = mn * n
j2 = peak(mn, j1)
count += max_diff(mn, j1, j2)
print(count)


trash = int(input())
line1 = [int(x) for x in input().split()]
low = min(line1)
lows = []
for i in range(0, len(line1)):
	if line1[i] == low:
		lows.append(i)
dists = []
for i in range(1, len(lows)):
	dists.append(lows[i] - lows[i - 1] - 1)
dists.append(lows[0] + (len(line1) - 1) - lows[-1])
print(max(dists) + low * len(line1))


n = int(input())
b = list(map(int, input().split()))
(max_len, curr_len, e) = (0, 0, min(b))
for i in range(n):
	if b[i] == e:
		max_len = max(max_len, curr_len)
		curr_len = 0
	else:
		curr_len += 1
print(e * n + max(curr_len + b.index(e), max_len))


arr = []
n = int(input())
lst = list(map(int, input().split()))
m = min(lst)
for i in range(len(lst)):
	if lst[i] == m:
		arr.append(i)
x = arr[0]
if len(arr) > 1:
	x = arr[1] - arr[0]
	for i in range(len(arr) - 1):
		x = max(x, arr[i + 1] - arr[i])
x = max(x, n - (arr[-1] - arr[0]))
ans = n * m + (x - 1)
if len(arr) == n:
	ans = sum(lst)
print(ans)


def arr_inp(n):
	if n == 1:
		return [int(x) for x in stdin.readline().split()]
	elif n == 2:
		return [float(x) for x in stdin.readline().split()]
	else:
		return [str(x) for x in stdin.readline().split()]
from sys import stdin
from collections import deque
(n, a) = (int(input()), deque(arr_inp(1)))
mi = min(a)
(ans, c, tem) = (mi * n, 0, 0)
for i in range(n):
	if a[0] == mi:
		break
	a.rotate(-1)
for i in range(n):
	if a[i] != mi:
		c += 1
	else:
		tem = max(tem, c)
		c = 0
print(ans + max(tem, c))


n = int(input())
a = list(map(int, str(input()).split()))
minNumber = min(a)
sumA = minNumber * n
for i in range(n):
	a[i] -= minNumber
hasGoRound = False
isCounting = False
index = 0
count = 0
maxCount = 0
while True:
	if a[index] != 0 and (not isCounting):
		isCounting = True
	if a[index] != 0 and isCounting:
		count += 1
	else:
		if maxCount < count:
			maxCount = count
		if hasGoRound:
			break
		isCounting = False
		count = 0
	index += 1
	if index == n:
		index = 0
		hasGoRound = True
print(sumA + maxCount)


n = int(input())
colors = list(map(int, input().split()))
m = min(colors)
longest_sub = 0
cur_sub = 0
pos = 0
break_on_min = False
while pos < n:
	if colors[pos] > m:
		cur_sub += 1
		if cur_sub > longest_sub:
			longest_sub = cur_sub
	else:
		cur_sub = 0
		if break_on_min:
			break
	pos += 1
	if pos == n and cur_sub > 0:
		pos = 0
		break_on_min = True
print(m * n + longest_sub)


def compute(n, jars):
	m = min(jars)
	l = 0
	ml = 0
	for i in range(n):
		if jars[i] == m:
			if l > ml:
				ml = l
			l = 0
		else:
			l += 1
	for i in range(n):
		if jars[i] == m:
			if l > ml:
				ml = l
			l = 0
		else:
			l += 1
	return n * m + ml
n = int(input())
jars = list(map(int, input().split()))
print(compute(n, jars))


t = int(input())
data = [int(i) for i in input().split(' ')]
datamin = min(data)
data = [i - datamin for i in data]
maxn = 0
cont = 0
for i in range(t):
	if data[i] == 0:
		maxn = max(maxn, cont)
		cont = 0
	else:
		cont += 1
if cont != 0:
	for i in range(t):
		if data[i] == 0:
			maxn = max(maxn, cont)
			break
		else:
			cont += 1
maxn = max(maxn, cont)
print(maxn + datamin * t)


(n, a) = (int(input()), list(map(int, input().split())))
m = min(a)
b = [i for (i, x) in enumerate(a) if x == m]
k = max(((b[i] - b[i - 1] - 1) % n for i in range(len(b))))
print(n * m + k)


n = int(input())
litres = list(map(int, input().split()))
mini = 10 ** 9 + 1
for i in range(n):
	if mini >= litres[i]:
		mini = litres[i]
		last = i
for i in range(n):
	if litres[i] == mini:
		first = i
		break
litres += litres[:first + 1]
maxi = 0
prev = first
if first == last:
	print(mini * n + n - 1)
else:
	for i in range(first + 1, n + first + 1):
		if litres[i] == mini:
			if maxi < i - prev:
				maxi = i - prev
			prev = i
	print(mini * n + maxi - 1)


n = int(input())
l = list(map(int, input().split()))
a = min(l)
for i in range(0, len(l)):
	l[i] = l[i] - a
b = l + l
k = []
for i in range(0, len(b)):
	if b[i] == 0:
		k.append(i)
c = k[1] - k[0] - 1
for i in range(1, len(k)):
	q = k[i] - k[i - 1] - 1
	if q > c:
		c = q
print(c + a * n)


from sys import *

def solve(a, n):
	m = min(a)
	s = sum(a)
	s1 = 0
	for i in range(n):
		a[i] -= m
	a = a + a
	temp = 0
	store = 0
	for i in a:
		if i == 0:
			temp = 0
		else:
			temp += 1
		store = max(store, temp)
	return m * n + store
	pass
test = 1
for t in range(0, test):
	n = int(input())
	arr = [int(x) for x in input().split()]
	ans = solve(arr, n)
	print(ans)


n = int(input())
a = list(map(int, input().split()))
mi = min(a)
cur = 0
ans = 0
for i in range(3 * n):
	if a[i % n] == mi:
		cur = 0
	else:
		cur += 1
	ans = max(ans, cur)
print(n * mi + ans)


n = int(input())
a = list(map(int, input().split()))
minA = min(a)
a = a + a
consecutive_nonzero = [0 for _ in a]
consecutive_nonzero[0] = int(a[0] > minA)
for i in range(1, len(a)):
	if a[i] > minA:
		consecutive_nonzero[i] = consecutive_nonzero[i - 1] + 1
res = minA * n + max(consecutive_nonzero)
print(res)


(a, b) = (int(input()), list(map(int, input().split())))
maks = min(b)
c = [i for (i, x) in enumerate(b) if x == maks]
k = max(((c[i] - c[i - 1] - 1) % a for i in range(len(c))))
print(a * maks + k)


n = int(input(''))
a = list(map(int, input('').split()))
mv = min(a)
t = []
for i in range(n):
	if a[i] == mv:
		t.append(i)
md = n + t[0] - t[-1]
for i in range(len(t) - 1):
	if t[i + 1] - t[i] > md:
		md = t[i + 1] - t[i]
print(mv * n + md - 1)


from sys import stdin
a = int(input())
b = list(map(int, stdin.readline().split()))
k = min(b)
s = k * a
b *= 2
i = 0
t = 0
a *= 2
while i < a:
	f = 0
	while i < a and b[i] - k:
		f += 1
		i += 1
	t = max(t, f)
	i += 1
print(t + s)


n = int(input())
a = list(map(int, input().split()))
amin = min(a)
cou = a.count(amin)
dmass = []
for i in range(n):
	if a[i] == amin:
		dmass += [i]
an2 = dmass[0] - dmass[-1] + n
if cou > 1:
	for j in range(1, cou):
		an2 = max(an2, dmass[j] - dmass[j - 1])
else:
	an2 = n
print(amin * n + an2 - 1)


(n, a, mn, j, ans) = (int(input()), input().split(' '), 1000000001, 0, 0)
for i in range(n):
	(a[i], mn) = (int(a[i]), min(mn, int(a[i])))
for i in range(n):
	a[i] -= mn
while j < n:
	cnt = 0
	while j < n and a[j] > 0:
		j += 1
		cnt += 1
	j += 1
	ans = max(ans, cnt)
j = 0
while j < n and a[j] > 0:
	j += 1
(cnt, j) = (j, n - 1)
while j >= 0 and a[j] > 0:
	j -= 1
cnt += n - 1 - j
ans = max(ans, cnt)
ans += n * mn
print(ans)


from sys import stdin as Si
from math import floor as F
from collections import defaultdict as dt
from operator import itemgetter as ig
n = int(Si.readline())
p = list(map(int, Si.readline().split(' ')))
m = min(p)
(atleast, first, between, s) = (m * n, p.index(m), 0, 0)
for i in p:
	if i == m:
		between = max(between, s)
		s = 0
	else:
		s += 1
print(atleast + max(s + first, between))


n = int(input())
a = [int(x) for x in input().split()] * 2
ans = min(a) * n
add = 0
l = r = a.index(min(a))
while r < 2 * n:
	while r < 2 * n and a[r] != a[l]:
		r += 1
	add = max(add, r - l - 1)
	l = r
	r += 1
ans += add
print(ans)


n = int(input())
lis = list(map(int, input().split()))
m = min(lis)
ans = c = t = i = 0
while lis[t] > m:
	t += 1
for i in lis:
	if i > m:
		c += 1
	else:
		ans = max(ans, c)
		c = 0
ans = max(ans, c + t)
print(ans + m * n)


n = int(input())
a = list(map(int, input().split(' ')))
mn = a[0]
ff = 0
fl = 0
mx = 0
t = 0
c = 0
for i in range(len(a)):
	if a[i] < mn:
		mn = a[i]
		ff = i
		fl = i
		t = 1
	elif a[i] == mn:
		t += 1
		fl = i
mx = len(a) - (fl - ff + 1)
for i in range(ff + 1, fl + 1):
	if a[i] == mn:
		if c > mx:
			mx = c
		c = 0
	else:
		c += 1
print(mx + mn * len(a))


n = int(input())
a = [int(x) for x in input().split()]
m = min(a)
ind = []
for i in range(len(a)):
	if a[i] == m:
		ind.append(i)
ans = ind[0] + n - ind[-1] - 1
for i in range(1, len(ind)):
	ans = max(ans, ind[i] - ind[i - 1] - 1)
print(m * n + ans)


n = int(input())
a = list(map(int, input().split()))
minimum = min(a)
count_min = a.count(minimum)
pos_min_first = a.index(minimum)
pos_min = pos_min_first
max_offset = 0
for i in range(count_min - 1):
	pos_min_new = a.index(minimum, pos_min + 1)
	max_offset = max(pos_min_new - pos_min - 1, max_offset)
	pos_min = pos_min_new
max_offset = max(pos_min_first + n - 1 - pos_min, max_offset)
print(minimum * n + max_offset)


import os, io
from sys import stdout
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def binomial_coefficient(n, k):
	if 0 <= k <= n:
		ntok = 1
		ktok = 1
		for t in range(1, min(k, n - k) + 1):
			ntok *= n
			ktok *= t
			n -= 1
		return ntok // ktok
	else:
		return 0
n = int(input())
l = list(map(int, input().split()))
m = min(l)
for i in range(len(l)):
	l[i] -= m
gl = 0
current = 0
for element in l:
	if element == 0:
		gl = max(gl, current)
		current = 0
	else:
		current += 1
if current > 0:
	i = 0
	while i < len(l) and l[i] != 0:
		i += 1
		current += 1
	gl = max(gl, current)
print(m * n + gl)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
n = Int()
a = array()
m = min(a)
ans = []
for i in range(0, n):
	if a[i] == m:
		ans.append(i + 1)
box = max(0, n - ans[-1] + ans[0] - 1)
for i in range(1, len(ans)):
	box = max(ans[i] - 1 - ans[i - 1], box)
box += n * m
print(box)


n = int(input())
paint = [int(c) for c in input().split()]
_min = min(paint)
idx = [i for (i, e) in enumerate(paint) if e == _min]
dist = [n - 1] * n
if len(idx) == 1:
	dist = n - 1
else:
	dist = 0
	cur = 0
	for i in range(2 * n):
		if paint[i % n] > _min:
			cur += 1
		else:
			dist = max(dist, cur)
			cur = 0
ans = _min * n + dist
print(ans)


import sys, os
from io import BytesIO, IOBase
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
n = int(input())
arr = list(map(int, input().split()))
m = min(arr)
ml = [i for i in range(n) if arr[i] == m]
ans = float('-inf')
for i in ml:
	j = (i + 1) % n
	p = n * m
	while arr[j] != m:
		j = (j + 1) % n
		p += 1
	ans = max(ans, p)
print(ans)


n = int(input())
nums = list(map(int, input().split()))
m = min(nums)
x = [num > m for num in nums]
x = x + x
left = 0
right = 0
ans = 0
while right < 2 * n:
	if left == right:
		right += 1
	elif not x[left]:
		left += 1
	elif x[right]:
		right += 1
	else:
		ans = max(ans, right - left)
		left = right
print(n * m + ans)


import re, sys, math, string, operator, functools, fractions, collections
from random import *
sys.setrecursionlimit(10 ** 7)
dX = [-1, 1, 0, 0, -1, 1, -1, 1]
dY = [0, 0, -1, 1, 1, -1, -1, 1]
RI = lambda : list(map(int, input().split()))
RS = lambda : input().rstrip().split()
mod = 1000000000.0 + 7
n = RI()[0]
a = RI()
minVal = min(a)
inds = []
for i in range(n):
	if a[i] == minVal:
		inds.append(i)
inds.append(inds[0] + n)
ans = 0
for i in range(len(inds) - 1):
	ans = max(inds[i + 1] - inds[i] - 1, ans)
ans += minVal * n
print(ans)


def last_index(l, value):
	return len(l) - 1 - l[::-1].index(value)
n = int(input())
l = [int(x) for x in input().split(' ')]
mini = min(l)
l_mini = [i for (i, item) in enumerate(l) if item == mini]
max_dist = l_mini[0] + (len(l) - l_mini[-1])
for i in range(1, len(l_mini)):
	if max_dist < l_mini[i] - l_mini[i - 1]:
		max_dist = l_mini[i] - l_mini[i - 1]
print(n * mini + max_dist - 1)


def ext(lst):
	n = len(lst)
	c = 0
	ans = []
	for i in range(n):
		if lst[i] != 0:
			c += 1
		elif c != 0:
			ans.append(c)
			c = 0
	if c:
		ans.append(c)
	return ans
n = int(input())
l = list(map(int, input().split()))
y = l[0]
m = min(l)
for i in range(n):
	l[i] = l[i] - m
l2 = ext(l)
if l[-1] != 0 and l[0] != 0:
	if 0 in l:
		l2[-1] += l2[0]
if l2 == []:
	print(n * y)
else:
	print(m * n + max(l2))


n = int(input())
a = list(map(int, input().split()))
m = min(a)
b = []
for i in range(n):
	if a[i] == m:
		b.append(i)
d = 0
for i in range(0, len(b) - 1):
	d = max(d, b[i + 1] - b[i] - 1)
d = max(d, n - b[len(b) - 1] + b[0] - 1)
print(d + m * n)


from sys import stdin

def get_required(arr):
	mn = min(arr)
	indexes = []
	for i in range(len(arr)):
		if arr[i] == mn:
			indexes.append(i)
	indexes.append(indexes[0])
	start = -1
	diff = -1
	for i in range(1, len(indexes)):
		if (indexes[i] - indexes[i - 1]) % n > diff:
			diff = (indexes[i] - indexes[i - 1]) % n
			start = (indexes[i - 1] + 1) % n
	return start
n = int(stdin.readline().strip())
arr = list(map(int, stdin.readline().split()))
index = get_required(arr)
temp = []
for i in range(index, n):
	temp.append(arr[i])
for i in range(index):
	temp.append(arr[i])
cycles = min(temp)
ans = 0
ans += cycles * n
for i in range(n):
	temp[i] -= cycles
	if temp[i] != 0:
		ans += 1
	else:
		break
print(ans)


import sys
if False:
	inp = open('B.txt', 'r')
else:
	inp = sys.stdin
n = int(inp.readline())
liters = list(map(int, inp.readline().split()))
before = 0
after = 0
low = 10 ** 9 + 10
maxBetween = 0
between = 0
for i in range(n):
	if liters[i] == low:
		after = -1
		maxBetween = max(maxBetween, between)
		between = -1
	if liters[i] < low:
		before = i
		low = liters[i]
		after = -1
		between = -1
		maxBetween = 0
	after += 1
	between += 1
ans = low * n + max(maxBetween, before + after)
print(ans)


import logging
import copy
import sys
import math

def gcd(p, q):
	if q == 0:
		return p
	return gcd(q, p % q)

def solve(firstLine, inputLines):
	n = firstLine[0]
	minN = min(inputLines)
	minIndex = []
	for (i, j) in enumerate(inputLines):
		if inputLines[i] == minN:
			minIndex.append(i)
	pre = minIndex[0]
	maxRemain = 0
	for (i, j) in enumerate(minIndex[1:]):
		maxRemain = max([j - pre - 1, maxRemain])
		pre = j
	last = n - minIndex[len(minIndex) - 1] - 1 + minIndex[0]
	log('n - minIndex[len(minIndex)-1] - 1', minIndex[len(minIndex) - 1])
	maxRemain = max([last, maxRemain])
	return minN * n + maxRemain

def main():
	firstLine = input().split()
	firstLine = list(map(int, firstLine))
	inputLines = input().split()
	inputLines = list(map(int, inputLines))
	print(solve(firstLine, inputLines))

def log(*message):
	logging.debug(message)
main()


(n, a, c, v) = (int(input()), list(map(int, input().split())), 0, [])
m = min(a)
for x in a:
	if x > m:
		c += 1
	else:
		if c:
			v.append(c)
		c = 0
v.append(c if a[0] == m else c + v[0])
print(n * m + max(v))


a = int(input())
l = list(map(int, input().split()))
b = list()
j = min(l)
for x in range(a):
	if l[x] == j:
		b.append(x)
d = 0
k = len(b)
for x in range(0, k - 1):
	d = max(d, b[x + 1] - b[x] - 1)
d = max(d, a - b[k - 1] + b[0] - 1)
print(d + a * min(l))


from sys import stdin
from ctypes import c_longlong
n = int(input())
arr = list(map(int, stdin.readline().split()))
m = min(arr)
dis = cnt = 0
num = -1
for i in range(n):
	if arr[i] == m:
		if num == -1:
			firstdis = i
		num = i
		if dis < cnt:
			dis = cnt
		cnt = 0
	else:
		cnt += 1
	if i == n - 1:
		lastdis = n - 1 - num
		if dis < lastdis + firstdis:
			dis = c_longlong(lastdis + firstdis).value
res = c_longlong(n * m + dis).value
print(res)


n = int(input())
a = list(map(int, input().split()))
m = min(a)
b = [int(a[i] > m) for i in range(n)]
L = Max = 0
last = -1
for i in range(n):
	if b[i] == 0 and last != -1:
		Max = max(i - last, Max)
		last = -1
	elif last == -1 and b[i]:
		last = i
if last != -1:
	Max = max(n - last, Max)
if b[0] and b[-1]:
	L = 0
	R = n - 1
	while b[L] and L < n - 1:
		L += 1
	while b[R] and L > 0:
		R -= 1
	L -= 1
	R += 1
	Max = max(L + n - R + 1, Max)
ans = m * n + Max
print(ans)


n = int(input())
paint = [int(c) for c in input().split()]
_min = min(paint)
dist = cur = 0
for i in range(2 * n - 1):
	if paint[i % n] > _min:
		cur += 1
	else:
		dist = max(dist, cur)
		cur = 0
ans = _min * n + dist
print(ans)


n = int(input())
l = list(map(int, input().split()))
l += l
mi = min(l)
j = 0
dist = 0
for i in range(n << 1):
	if l[i] == mi:
		dist = max(i - j - 1, dist)
		j = i
print(mi * n + dist)


n = int(input())
a = list(map(int, input().split()))
minn = 10 ** 9
sum = 0
for i in range(n):
	if a[i] < minn:
		minn = a[i]
i = 0
sum2 = 0
a = a + a
while i < n:
	if a[i] == minn:
		sum1 = 0
		k = i + 1
		while a[k] != minn:
			sum1 += 1
			k += 1
		i = k
		sum2 = max(sum2, sum1)
	else:
		i += 1
t = minn * n + sum2
print(t)


n = int(input())
a = list(map(int, input().split()))
mini = min(a)
maxi = max(a)
if mini == maxi:
	print(n * mini)
elif a.count(mini) == 1:
	print(n * mini + (n - 1))
else:
	ans = mini * n
	left = -1
	z = 0
	right = n
	for i in range(len(a)):
		if a[i] == mini:
			z = max(z, i - left)
			left = i
	ans += max([z, right - left, a.index(mini) + (n - 1 - left) + 1])
	print(ans - 1)