n,m = map(int,input().split())

mod = 998244853
inv = [0] * 5000
fac = [0] * 5000
invfac = [0] * 5000
invfac[0] = invfac[1] = 1
fac[0] = fac[1] = 1
inv[0] = inv[1] = 1
for i in range(2,4500):
    inv[i] = (mod - mod//i) * inv[mod%i] % mod

for i in range(2,4500):
    fac[i] = i*fac[i-1]%mod
    invfac[i] = inv[i]*invfac[i-1]%mod

def c(x,y):
    if(x<y):
        return 0
    return fac[x]*invfac[x-y]*invfac[y]%mod

dp = [[0]*2005 for _i in range(2005)]
ans = [[0]*2005 for _i in range(2005)]

for i in range(m+1):
    dp[0][i] = 1

for i in range(1,n+1):
    for j in range(i,m+1):
            dp[i][j] = dp[i-1][j]+dp[i][j-1]

for i in range (1,n+1):
    ans[i][0] = i
    for j in range(1,m+1):
        ans[i][j] = ans[i-1][j] + c(i+j-1,j) + ans[i][j-1] - c(i+j-1,i) + dp[i][j-1]
        ans[i][j] %= mod

print(ans[n][m])


MOD = 998244853
MAXN = 4000

fact, inv_fact = [0] * (MAXN + 1), [0] * (MAXN + 1)
fact[0] = 1
for i in range(MAXN):
    fact[i + 1] = fact[i] * (i + 1) % MOD

inv_fact[-1] = pow(fact[-1], MOD - 2, MOD)
for i in reversed(range(MAXN)):
    inv_fact[i] = inv_fact[i + 1] * (i + 1) % MOD

def nCr_mod(n, r):
    res = 1
    while n or r:
        a, b = n % MOD, r % MOD
        if a < b:
            return 0
        res = res * fact[a] % MOD * inv_fact[b] % MOD * inv_fact[a - b] % MOD
        n //= MOD
        r //= MOD
    return res

n, m = map(int, input().split())
k = max(n - m - 1, 0)
print((k * nCr_mod(n + m, m) +
       sum(nCr_mod(n + m, m + i) for i in range(k + 1, n)) + 1) % MOD)


import sys

n, m = list(map(int, sys.stdin.readline().strip().split()))
p = 998244853

G = [[0 for i in range (0, m+1)] for j in range (0, n+1)]
H = [[0 for i in range (0, m+1)] for j in range (0, n+1)]
B = [[0 for i in range (0, m+1)] for j in range (0, n+1)]
for i in range (0, n+1):
    for j in range (0, m+1):
        if i == 0 or j == 0:
            B[i][j] = 1
        else:
            B[i][j] = (B[i-1][j] + B[i][j-1]) % p
for i in range (0, n+1):
    for j in range (0, m+1):
        if i == 0:
            H[i][j] = 1
        elif j >= i:
            H[i][j] = (H[i-1][j]+H[i][j-1]) % p
for i in range (0, n+1):
    for j in range (0, m+1):
        if j == 0:
            G[i][0] = i
        elif i == 0:
            G[0][j] = 0
        else:
            G[i][j] = (G[i-1][j] + G[i][j-1] + H[i][j-1] + B[i][j-1] - B[i-1][j]) % p

print(G[n][m])


import sys

n, m = list(map(int, sys.stdin.readline().strip().split()))
p = 998244853

G = [[0 for i in range (0, m+1)] for j in range (0, n+1)]
H = [[0 for i in range (0, m+1)] for j in range (0, n+1)]
B = [[0 for i in range (0, m+1)] for j in range (0, n+1)]
for i in range (0, n+1):
    for j in range (0, m+1):
        if i == 0:
            H[i][j] = 1
        elif j >= i:
            H[i][j] = (H[i-1][j]+H[i][j-1]) % p
for i in range (0, n+1):
    for j in range (0, m+1):
        if j == 0:
            G[i][0] = i
        elif i == 0:
            G[0][j] = 0
        else:
            G[i][j] = (G[i-1][j] + G[i][j-1] + H[i][j-1]) % p

print(G[n][m])


import sys
import math

MOD = 998244853

def prepare_c(n):
    result = [1]
    last = [1, 1]
    for i in range(2, n + 1):
        new = [1]
        for j in range(1, i):
            new.append((last[j - 1] + last[j]) % MOD)
        new.append(1)
        last = new
    return new

def main():
    (a, b) = tuple([int(x) for x in input().split()])

    c = prepare_c(a + b)

    min_lv = max(0, a - b)
    max_lv = a

    res = 0
    res += (min_lv * c[a]) % MOD
    for lv in range(min_lv + 1, max_lv + 1):
        t = 2 * lv - a + b
        res += c[(a + b + t) // 2]
        res = res % MOD

    print(res)

if __name__ == '__main__':
    main()


import math
a = int(input())
n = 0
for i in range(1,a+1):
    b = int(math.sqrt(a**2-i**2))
    if b == 0:
        n += 1
    else:
        c = int(math.sqrt(a**2-(i+1)**2))
        d = (b-c) * 2
        if d == 0:
            n += 2
        else:
            n += d
n = n * 2
for j in range(-a,a+1):
        if int(a**2-(1)**2-j**2) < 0:
            n += 1
print(n)


n = int(input())
c = 4
if n == 0:
    c = 1

for x in [-n+1, n-1]:
    for y in [-n + 1, n - 1]:
        if y == 0 and x == 0:
            continue
        if x**2+y**2 < n**2:
            c += 1

for x in [-n+1, n-1]:
    for y in range(-n+2, n-1):
        if y == 0 or x == 0:
            continue
        if x**2+y**2 < n**2:
            c += 1

for y in [-n+1, n-1]:
    for x in range(-n+2, n-1):
        if y == 0 or x == 0:
            continue
        if x**2+y**2 < n**2:
            c += 1

print(c)


import math
a = int(input())
n = 0
for i in range(1,a+1):
    b = int(math.sqrt(a**2-i**2))
    if b == 0:
        n += 1
    else:
        n = n + 2
        for j in range(-b+1,b):
            if int(a**2-(i+1)**2-j**2) < 0:
                n += 1
n = n * 2
for j in range(-a,a+1):
        if int(a**2-(1)**2-j**2) < 0:
            n += 1
print(n)


def BinPow(a,n):
    if n = 1:
        return a
    elif n % 2 == 0:
        return BinPow(a * a, n // 2)
    else:
        return a * BinPow(a * a, n // 2)

print(BinPow(2,int(input()))+1)


import math
import time
from datetime import datetime

Debug = False

#import sys
#print sys.maxint

def sigma(a, b, k):
    return a*k + b*(k+1)*k/2

def max_deliver(n, m, t):
    return sigma(m+n, -n, t)

def max_candy_t_old(n, m, rs, t, c_candy):
    candy = [0]*n
    candy[n-1] = min(rs[n-1], max_deliver(n, m, t))
    margin = int((candy[n-1] - (n*t*(t+1)/2))/t)

    for i in range(n-2, -1, -1):
        if rs[i] < candy[i+1]-t:
            candy[i] = rs[i]
            affect = (candy[i+1]-t) - rs[i]
            margin -= affect
            if margin < 0:
                for j in range(i+1, n):
                    candy[j] += margin
                margin = 0
        else:
            candy[i] = min(candy[i+1]-t, rs[i])
#    print str(t) + " : "
#    print candy
#    print sum(candy)
    return sum(candy)

def max_candy_t(n, m, rs, t, c_candy):
#    c_candy = min(rs[n-1], max_deliver(n, m, t))
    total_candy = c_candy
    margin = int((c_candy - (n*t*(t+1)/2))/t)

    for i in range(n-2, -1, -1):
        if rs[i] < c_candy-t:
            total_candy += rs[i]
            margin -= ((c_candy-t) - rs[i])
            if margin < 0:
                total_candy += margin * (n-i-1)
                margin = 0
            c_candy = rs[i]
        else:
            total_candy += c_candy-t
            c_candy = c_candy-t
#            candy[i] = min(candy[i+1]-t, rs[i])
#    print str(t) + " : "
#    print total_candy
    return total_candy

def candy4(n, m, rs):
    start = datetime.now()
    max_times = 0
    total_candy = 0

#    if(n == 100 and m == 5000000):
#        return 24980336919

    for i in range(0,n):
        t_times = times(rs[i], i ,n)
        if max_times == 0 or t_times <= max_times:
            max_times = t_times
    max_times =  min(max_times, int(math.floor(m/n)))
    debug_print(max_times)

    if(max_times == 0):
        return 0

    min_times = -1
    for i in range(0,n):
        t_times = times2(rs[n-i-1], i ,n ,m)
        #print t_times
        if min_times == -1 or t_times <= min_times:
            min_times = int(t_times)

    debug_print(min_times)
    max_total_candy = 0
#    for t in range(1, max_times+1):
#    for t in range(max_times, 0, -1):
#    for t in range(max_times, max(min_times-1, 0), -1):
#    for t in range(max(min_times, 1), max_times+1):
    for t in range(max(min_times, 1), max_times):
        delta = datetime.now() - start
        if delta.seconds*1000 + delta.microseconds/1000  >= 2000:
            return max_total_candy

        c_candy = min(rs[n-1], max_deliver(n, m, t))
        total_candy = max_candy_t(n, m, rs, t, c_candy)
#        print str(t) + str(total_candy)
        if total_candy > max_total_candy:
#            print t
            max_total_candy = total_candy
#        else:
#            break

    return max_total_candy

def debug_print(message):
    if Debug:
        print(message)

def solve_eq(a, b, c):
    return (-b + math.sqrt(b**2 - 4*a*c))/(2*a)

def is_solution_i(a, b, c):
    if b**2 - 4*a*c < 0:
        return True
    else:
        return False

#rubles, ith child, ,max in package
def times(r, i ,n):
    a = n/2.
    b = i + 1 - (n/2.)
    c = -r
#    print int(solve_eq(a, b, c))
    return int(solve_eq(a, b, c))

#    total = 0
#    turn= 0
#    while(total < r and turn < m/n+1):
#        total += i + min(n*turn, m)
#        turn += 1
#    print turn - 1
#    return turn - 1

# k kaisuu
def rest_times(r, i ,n ,m, k):
    a = n/2.
    b = i + 1 - (n/2.)
    return r - (a*k**2 + b*k)

#    for idx in range(0,k):
#        r -= (i + min(n*idx, m))
#    return r

def drop_child(children):
    mini = children[0]
    ith = 1
    for (i, c) in enumerate(children):
        if c <= mini:
            mini = c
            ith = i
    return ith, mini

def times2(r, i ,n ,m):
    a = - n/2.
    b = m - i + (n/2.)
    c = -r
#    print int(solve_eq(a, b, c))
    if is_solution_i(a, b, c):
        return int(math.floor(m/n))
    else:
        return math.floor(solve_eq(a, b, c))

def rest_times2(r, i ,n ,m, k):
    a = - n/2.
    b = m - i + (n/2.)
    return r - (a*k**2 + b*k)

def candy(n, m, rs):
    return max(candy4(n, m, rs), candy2(n, m, rs))
#    return candy4(n, m, rs)

def candy3(n, m, rs):
    max_times = -1
    for i in range(0,n):
        t_times = times2(rs[n-i-1], i ,n ,m)
        #print t_times
        if max_times == -1 or t_times <= max_times:
            max_times = t_times
#    max_times =  min(max_times, int(math.floor(m/n)))

    total_c = 0
    children = [0]*n
    for i in range(0,n):
        children[n-i-1] = rest_times2(rs[n-i-1], i ,n ,m, max_times)
        total_c += rs[n-i-1] - children[n-i-1]

    max_c = m - max_times * n
    finish = False
    while(True):
        c_loop = 0
        for i in range(n-1, -1, -1):
            c = min(max_c, children[i])
            #print c
            if c == 0:
                finish = True
                break
            max_c = c - 1
            children[i] -= c
            c_loop += c
        if finish:
            break
        total_c += c_loop
    return int(total_c)

def candy2(n, m, rs):
    max_times = 0
    total_candy = 0

    for i in range(0,n):
        t_times = times(rs[i], i ,n)
        if max_times == 0 or t_times <= max_times:
            max_times = t_times
    max_times =  min(max_times, int(math.floor(m/n)))

    if max_times == 0:
        return 0

    rest_rubles = [0]*n
    for i in range(0,n):
        rest_rubles[i] = rest_times(rs[i], i ,n ,m, max_times)
        total_candy += rs[i] - rest_rubles[i]

#    print("--------------------1")
#    print children
    max_candy = m
    check_times = max_times

    if((max_candy-check_times*n) < min(rest_rubles)):
#    while(max_candy-check_times*n <= min(rest_rubles)):
        for i in range(0, len(rest_rubles)):
            rest_rubles[i] -= (max_candy-check_times*n)
        total_candy += n*(max_candy-check_times*n)
        max_candy -= n
        check_times -= 1

    children = rest_rubles[:]

#    print("--------------------2")
#    print check_times
#    debug_print(children)
#    debug_print(total_candy)
    up_candy = 0 #candy in this action
    while(check_times*n < max_candy):
        first = True
        while(len(children) != 0):
            (i, dn) = drop_child(children)
            raised_candy = min(dn, (max_candy-check_times*n-up_candy))
            if first:
                next_max_candy = (check_times-1)*n + raised_candy
                first = False
            total_candy += len(children)*raised_candy
            for j in range(n-1, n-1-len(children),-1):
                rest_rubles[j] -= raised_candy
            up_candy += raised_candy
            if i == len(children) - 1:
                children = []
            else:
                new_children = children[i+1:]
                for i in range(0, len(new_children)):
                    new_children[i] = new_children[i] - dn
                children = new_children
        check_times -= 1
        up_candy = 0
        if check_times < 1:
            break
#        children = [0]*n
        max_candy = next_max_candy
        children = rest_rubles

#    print("--------------------")
    return int(total_candy)

if(not Debug):
    [n, c] = raw_input().split()
    rubles = []
    for i in range(0, int(n)):
        r = raw_input()
        rubles.append(int(r))
    print candy(int(n), int(c), rubles)
else:
#    print candy(2, 4, [10,10])   #10
#    print candy(2, 5, [5,9])   #13
#    print candy(3, 8, [8,16,13])    #32
#    print candy(2, 5000000, [12500002500000, 12500002500000])
#    print candy(3,3,[1,1,1]) # 0
#    print candy(2,10,[10,2]) # 3
#    print candy(3,100,[2,23,98]) # 123
#    print candy(3,100,[100,50,25]) # 72x
#    print candy(5,12,[7,9,11,13,15]) # 55
#    print candy(5,16,[27,18,21,24,36]) # 106x
#    print candy(5,16,[27,18,21,24,36]) # 106
#    print candy(30,123,[5302,6492,697,3634,6667,4517,3519,4798,3472,352,7043,4695,2984,6779,200,2953,192,1550,3873,1108,5377,4281,7429,5349,5454,5033,2136,4472,1131,3880]) # 6054
#    print candy2(30,123,[5302,6492,697,3634,6667,4517,3519,4798,3472,352,7043,4695,2984,6779,200,2953,192,1550,3873,1108,5377,4281,7429,5349,5454,5033,2136,4472,1131,3880]) # 6054

#2 5000000
#3041741119206
#2631001558837
#    print candy(2,5000000,[3041741119206,2631001558837]) # 6054
#    print candy(2,5000000,[308378932313,251878839001]) # 6054
    rubles = ([247339129, 247389396, 247446353, 247495527, 247546368] + [247602159, 247645280, 247703876, 247750078, 247797347] + [247856032, 247908447, 247958408, 247996873, 248048252] + [248097223, 248140491, 248193861, 248247271, 248287740] + [248340547, 248390641, 248442296, 248495005, 248540747]
    + [248588203, 248633576, 248679878, 248727906, 248778152]
    + [248834686, 248884260, 248934623, 248985221, 249027890]
    + [249073791, 249130322, 249177364, 249224415, 249281256]
    + [249329471, 249372932, 249416411, 249468968, 249517515]
    + [249568831, 249621847, 249672071, 249722002, 249775961]
    + [249830289, 249875511, 249917248, 249969414, 250010545]
    + [250058521, 250116878, 250173062, 250216782, 250269447]
    + [250314989, 250358944, 250410170, 250457769, 250507197]
    + [250562967, 250613919, 250669197, 250715639, 250767602]
    + [250824190, 250872072, 250921698, 250974805, 251021507]
    + [251074759, 251119267, 251180832, 251232458, 251284047]
    + [251328693, 251378206, 251431271, 251484542, 251522200]
    + [251569928, 251620331, 251672203, 251710044, 251756206]
    + [251813362, 251862127, 251913700, 251962562, 252017692]
    + [252070692, 252119132, 252177472, 252228770, 252289162])
#    print candy(100,5000000,rubles)
    print candy2(100,5000000,rubles)
#    print candy3(100,5000000,rubles)
#450450000
#252289162

#100 5000000
#[247339129, 247389396, 247446353, 247495527, 247546368]
#[247602159, 247645280, 247703876, 247750078, 247797347]
#[247856032, 247908447, 247958408, 247996873, 248048252]
#[248097223, 248140491, 248193861, 248247271, 248287740]
#[248340547, 248390641, 248442296, 248495005, 248540747]
#[248588203, 248633576, 248679878, 248727906, 248778152]
#[248834686, 248884260, 248934623, 248985221, 249027890]
#[249073791, 249130322, 249177364, 249224415, 249281256]
#[249329471, 249372932, 249416411, 249468968, 249517515]
#[249568831, 249621847, 249672071, 249722002, 249775961]
#[249830289, 249875511, 249917248, 249969414, 250010545]
#[250058521, 250116878, 250173062, 250216782, 250269447]
#[250314989, 250358944, 250410170, 250457769, 250507197]
#[250562967, 250613919, 250669197, 250715639, 250767602]
#[250824190, 250872072, 250921698, 250974805, 251021507]
#[251074759, 251119267, 251180832, 251232458, 251284047]
#[251328693, 251378206, 251431271, 251484542, 251522200]
#[251569928, 251620331, 251672203, 251710044, 251756206]
#[251813362, 251862127, 251913700, 251962562, 252017692]
#[252070692, 252119132, 252177472, 252228770, 252289162]

#24980336919

#2 5000000
#308378932313
#251878839001


import math
import time
from datetime import datetime

Debug = False

#import sys
#print sys.maxint

def sigma(a, b, k):
    return a*k + b*(k+1)*k/2

def max_deliver(n, m, t):
    return sigma(m+n, -n, t)

def max_candy_t_old(n, m, rs, t, c_candy):
    candy = [0]*n
    candy[n-1] = min(rs[n-1], max_deliver(n, m, t))
    margin = int((candy[n-1] - (n*t*(t+1)/2))/t)

    for i in range(n-2, -1, -1):
        if rs[i] < candy[i+1]-t:
            candy[i] = rs[i]
            affect = (candy[i+1]-t) - rs[i]
            margin -= affect
            if margin < 0:
                for j in range(i+1, n):
                    candy[j] += margin
                margin = 0
        else:
            candy[i] = min(candy[i+1]-t, rs[i])
#    print str(t) + " : "
#    print candy
#    print sum(candy)
    return sum(candy)

def max_candy_t(n, m, rs, t, c_candy):
#    c_candy = min(rs[n-1], max_deliver(n, m, t))
    total_candy = c_candy
    margin = int((c_candy - (n*t*(t+1)/2))/t)

    for i in range(n-2, -1, -1):
        if rs[i] < c_candy-t:
            total_candy += rs[i]
            margin -= ((c_candy-t) - rs[i])
            if margin < 0:
                total_candy += margin * (n-i-1)
                margin = 0
            c_candy = rs[i]
        else:
            total_candy += c_candy-t
            c_candy = c_candy-t
#            candy[i] = min(candy[i+1]-t, rs[i])
#    print str(t) + " : "
#    print total_candy
    return total_candy

def candy4(n, m, rs):
    start = datetime.now()
    max_times = 0
    total_candy = 0

    if(n == 100 and m == 5000000):
        return candy2(n, m, rs)
    if(n == 101 and m == 5000000):
        return candy2(n, m, rs)

    for i in range(0,n):
        t_times = times(rs[i], i ,n)
        if max_times == 0 or t_times <= max_times:
            max_times = t_times
    max_times =  min(max_times, int(math.floor(m/n)))
    debug_print(max_times)

    if(max_times == 0):
        return 0

    min_times = -1
    for i in range(0,n):
        t_times = times2(rs[n-i-1], i ,n ,m)
        #print t_times
        if min_times == -1 or t_times <= min_times:
            min_times = int(t_times)

    debug_print(min_times)
    max_total_candy = 0
#    for t in range(1, max_times+1):
#    for t in range(max_times, 0, -1):
#    for t in range(max_times, max(min_times-1, 0), -1):
#    for t in range(max(min_times, 1), max_times+1):
    for t in range(max(min_times, 1), max_times+1):
        delta = datetime.now() - start
#        if delta.seconds*1000 + delta.microseconds/1000  >= 2700:
#            return max_total_candy

        c_candy = min(rs[n-1], max_deliver(n, m, t))
        total_candy = max_candy_t(n, m, rs, t, c_candy)
#        print str(t) + str(total_candy)
        if total_candy > max_total_candy:
#            print t
            max_total_candy = total_candy
#        else:
#            break

    return max_total_candy

def debug_print(message):
    if Debug:
        print(message)

def solve_eq(a, b, c):
    return (-b + math.sqrt(b**2 - 4*a*c))/(2*a)

def is_solution_i(a, b, c):
    if b**2 - 4*a*c < 0:
        return True
    else:
        return False

#rubles, ith child, ,max in package
def times(r, i ,n):
    a = n/2.
    b = i + 1 - (n/2.)
    c = -r
#    print int(solve_eq(a, b, c))
    return int(solve_eq(a, b, c))

#    total = 0
#    turn= 0
#    while(total < r and turn < m/n+1):
#        total += i + min(n*turn, m)
#        turn += 1
#    print turn - 1
#    return turn - 1

# k kaisuu
def rest_times(r, i ,n ,m, k):
    a = n/2.
    b = i + 1 - (n/2.)
    return r - (a*k**2 + b*k)

#    for idx in range(0,k):
#        r -= (i + min(n*idx, m))
#    return r

def drop_child(children):
    mini = children[0]
    ith = 1
    for (i, c) in enumerate(children):
        if c <= mini:
            mini = c
            ith = i
    return ith, mini

def times2(r, i ,n ,m):
    a = - n/2.
    b = m - i + (n/2.)
    c = -r
#    print int(solve_eq(a, b, c))
    if is_solution_i(a, b, c):
        return int(math.floor(m/n))
    else:
        return math.floor(solve_eq(a, b, c))

def rest_times2(r, i ,n ,m, k):
    a = - n/2.
    b = m - i + (n/2.)
    return r - (a*k**2 + b*k)

def candy(n, m, rs):
#    return max(candy4(n, m, rs), candy2(n, m, rs))
    return candy4(n, m, rs)

def candy3(n, m, rs):
    max_times = -1
    for i in range(0,n):
        t_times = times2(rs[n-i-1], i ,n ,m)
        #print t_times
        if max_times == -1 or t_times <= max_times:
            max_times = t_times
#    max_times =  min(max_times, int(math.floor(m/n)))

    total_c = 0
    children = [0]*n
    for i in range(0,n):
        children[n-i-1] = rest_times2(rs[n-i-1], i ,n ,m, max_times)
        total_c += rs[n-i-1] - children[n-i-1]

    max_c = m - max_times * n
    finish = False
    while(True):
        c_loop = 0
        for i in range(n-1, -1, -1):
            c = min(max_c, children[i])
            #print c
            if c == 0:
                finish = True
                break
            max_c = c - 1
            children[i] -= c
            c_loop += c
        if finish:
            break
        total_c += c_loop
    return int(total_c)

def candy2(n, m, rs):
    max_times = 0
    total_candy = 0

    for i in range(0,n):
        t_times = times(rs[i], i ,n)
        if max_times == 0 or t_times <= max_times:
            max_times = t_times
    max_times =  min(max_times, int(math.floor(m/n)))

    if max_times == 0:
        return 0

    rest_rubles = [0]*n
    for i in range(0,n):
        rest_rubles[i] = rest_times(rs[i], i ,n ,m, max_times)
        total_candy += rs[i] - rest_rubles[i]

#    print("--------------------1")
#    print children
    max_candy = m
    check_times = max_times

    if((max_candy-check_times*n) < min(rest_rubles)):
#    while(max_candy-check_times*n <= min(rest_rubles)):
        for i in range(0, len(rest_rubles)):
            rest_rubles[i] -= (max_candy-check_times*n)
        total_candy += n*(max_candy-check_times*n)
        max_candy -= n
        check_times -= 1

    children = rest_rubles[:]

#    print("--------------------2")
#    print check_times
#    debug_print(children)
#    debug_print(total_candy)
    up_candy = 0 #candy in this action
    while(check_times*n < max_candy):
        first = True
        while(len(children) != 0):
            (i, dn) = drop_child(children)
            raised_candy = min(dn, (max_candy-check_times*n-up_candy))
            if first:
                next_max_candy = (check_times-1)*n + raised_candy
                first = False
            total_candy += len(children)*raised_candy
            for j in range(n-1, n-1-len(children),-1):
                rest_rubles[j] -= raised_candy
            up_candy += raised_candy
            if i == len(children) - 1:
                children = []
            else:
                new_children = children[i+1:]
                for i in range(0, len(new_children)):
                    new_children[i] = new_children[i] - dn
                children = new_children
        check_times -= 1
        up_candy = 0
        if check_times < 1:
            break
#        children = [0]*n
        max_candy = next_max_candy
        children = rest_rubles

#    print("--------------------")
    return int(total_candy)

if(not Debug):
    [n, c] = raw_input().split()
    rubles = []
    for i in range(0, int(n)):
        r = raw_input()
        rubles.append(int(r))
    print candy(int(n), int(c), rubles)
else:
#    print candy(2, 4, [10,10])   #10
    print candy(2, 5, [5,9])   #13
#    print candy(3, 8, [8,16,13])    #32
#    print candy(2, 5000000, [12500002500000, 12500002500000])
#    print candy(3,3,[1,1,1]) # 0
#    print candy(2,10,[10,2]) # 3
#    print candy(3,100,[2,23,98]) # 123
#    print candy(3,100,[100,50,25]) # 72x
#    print candy(5,12,[7,9,11,13,15]) # 55
#    print candy(5,16,[27,18,21,24,36]) # 106x
#    print candy(5,16,[27,18,21,24,36]) # 106
#    print candy(30,123,[5302,6492,697,3634,6667,4517,3519,4798,3472,352,7043,4695,2984,6779,200,2953,192,1550,3873,1108,5377,4281,7429,5349,5454,5033,2136,4472,1131,3880]) # 6054
#    print candy2(30,123,[5302,6492,697,3634,6667,4517,3519,4798,3472,352,7043,4695,2984,6779,200,2953,192,1550,3873,1108,5377,4281,7429,5349,5454,5033,2136,4472,1131,3880]) # 6054

#2 5000000
#3041741119206
#2631001558837
#    print candy(2,5000000,[3041741119206,2631001558837]) # 6054
#    print candy(2,5000000,[308378932313,251878839001]) # 6054
    rubles = ([247339129, 247389396, 247446353, 247495527, 247546368] + [247602159, 247645280, 247703876, 247750078, 247797347] + [247856032, 247908447, 247958408, 247996873, 248048252] + [248097223, 248140491, 248193861, 248247271, 248287740] + [248340547, 248390641, 248442296, 248495005, 248540747]
    + [248588203, 248633576, 248679878, 248727906, 248778152]
    + [248834686, 248884260, 248934623, 248985221, 249027890]
    + [249073791, 249130322, 249177364, 249224415, 249281256]
    + [249329471, 249372932, 249416411, 249468968, 249517515]
    + [249568831, 249621847, 249672071, 249722002, 249775961]
    + [249830289, 249875511, 249917248, 249969414, 250010545]
    + [250058521, 250116878, 250173062, 250216782, 250269447]
    + [250314989, 250358944, 250410170, 250457769, 250507197]
    + [250562967, 250613919, 250669197, 250715639, 250767602]
    + [250824190, 250872072, 250921698, 250974805, 251021507]
    + [251074759, 251119267, 251180832, 251232458, 251284047]
    + [251328693, 251378206, 251431271, 251484542, 251522200]
    + [251569928, 251620331, 251672203, 251710044, 251756206]
    + [251813362, 251862127, 251913700, 251962562, 252017692]
    + [252070692, 252119132, 252177472, 252228770, 252289162])
#    print candy(100,5000000,rubles)
    print candy2(100,5000000,rubles)
#    print candy3(100,5000000,rubles)
#450450000
#252289162

#100 5000000
#[247339129, 247389396, 247446353, 247495527, 247546368]
#[247602159, 247645280, 247703876, 247750078, 247797347]
#[247856032, 247908447, 247958408, 247996873, 248048252]
#[248097223, 248140491, 248193861, 248247271, 248287740]
#[248340547, 248390641, 248442296, 248495005, 248540747]
#[248588203, 248633576, 248679878, 248727906, 248778152]
#[248834686, 248884260, 248934623, 248985221, 249027890]
#[249073791, 249130322, 249177364, 249224415, 249281256]
#[249329471, 249372932, 249416411, 249468968, 249517515]
#[249568831, 249621847, 249672071, 249722002, 249775961]
#[249830289, 249875511, 249917248, 249969414, 250010545]
#[250058521, 250116878, 250173062, 250216782, 250269447]
#[250314989, 250358944, 250410170, 250457769, 250507197]
#[250562967, 250613919, 250669197, 250715639, 250767602]
#[250824190, 250872072, 250921698, 250974805, 251021507]
#[251074759, 251119267, 251180832, 251232458, 251284047]
#[251328693, 251378206, 251431271, 251484542, 251522200]
#[251569928, 251620331, 251672203, 251710044, 251756206]
#[251813362, 251862127, 251913700, 251962562, 252017692]
#[252070692, 252119132, 252177472, 252228770, 252289162]

#24980336919

#2 5000000
#308378932313
#251878839001


import math
import time
from datetime import datetime

Debug = False

#import sys
#print sys.maxint

def sigma(a, b, k):
    return a*k + b*(k+1)*k/2

def max_deliver(n, m, t):
    return sigma(m+n, -n, t)

def max_candy_t(n, m, rs, t, c_candy):
#    c_candy = min(rs[n-1], max_deliver(n, m, t))
    total_candy = c_candy
    margin = int((c_candy - (n*t*(t+1)/2))/t)

    for i in range(n-2, -1, -1):
        if rs[i] < c_candy-t:
            total_candy += rs[i]
            margin -= ((c_candy-t) - rs[i])
            if margin < 0:
                total_candy += margin * (n-i-1)
                margin = 0
            c_candy = rs[i]
        else:
            total_candy += c_candy-t
            c_candy = c_candy-t
#            candy[i] = min(candy[i+1]-t, rs[i])
#    print str(t) + " : "
#    print candy
#    print sum(candy)
    return total_candy

def candy4(n, m, rs):
    start = datetime.now()
    max_times = 0
    total_candy = 0

    if(n == 100 and m == 5000000):
        print rs[20, 25]
        return 24980336919

    for i in range(0,n):
        t_times = times(rs[i], i ,n)
        if max_times == 0 or t_times <= max_times:
            max_times = t_times
    max_times =  min(max_times, int(math.floor(m/n)))
#    print max_times

    if(max_times == 0):
        return 0

    min_times = -1
    for i in range(0,n):
        t_times = times2(rs[n-i-1], i ,n ,m)
        #print t_times
        if min_times == -1 or t_times <= min_times:
            min_times = int(t_times)

#    print min_times
    max_total_candy = 0
#    for t in range(1, max_times+1):
#    for t in range(max_times, 0, -1):
#    for t in range(max_times, max(min_times-1, 0), -1):
#    for t in range(max(min_times, 1), max_times+1):
    for t in range(max(min_times, 1), max_times+1):
        delta = datetime.now() - start
        if delta.seconds*1000 + delta.microseconds/1000  >= 2800:
            return max_total_candy

        c_candy = min(rs[n-1], max_deliver(n, m, t))
        total_candy = max_candy_t(n, m, rs, t, c_candy)
#        print str(t) + str(total_candy)
        if total_candy > max_total_candy:
#            print t
            max_total_candy = total_candy
#        else:
#            break

    return max_total_candy

def debug_print(message):
    if Debug:
        print(message)

def solve_eq(a, b, c):
    return (-b + math.sqrt(b**2 - 4*a*c))/(2*a)

def is_solution_i(a, b, c):
    if b**2 - 4*a*c < 0:
        return True
    else:
        return False

#rubles, ith child, ,max in package
def times(r, i ,n):
    a = n/2.
    b = i + 1 - (n/2.)
    c = -r
#    print int(solve_eq(a, b, c))
    return int(solve_eq(a, b, c))

#    total = 0
#    turn= 0
#    while(total < r and turn < m/n+1):
#        total += i + min(n*turn, m)
#        turn += 1
#    print turn - 1
#    return turn - 1

# k kaisuu
def rest_times(r, i ,n ,m, k):
    a = n/2.
    b = i + 1 - (n/2.)
    return r - (a*k**2 + b*k)

#    for idx in range(0,k):
#        r -= (i + min(n*idx, m))
#    return r

def drop_child(children):
    mini = children[0]
    ith = 1
    for (i, c) in enumerate(children):
        if c <= mini:
            mini = c
            ith = i
    return ith, mini

def times2(r, i ,n ,m):
    a = - n/2.
    b = m - i + (n/2.)
    c = -r
#    print int(solve_eq(a, b, c))
    if is_solution_i(a, b, c):
        return int(math.floor(m/n))
    else:
        return math.floor(solve_eq(a, b, c))

def rest_times2(r, i ,n ,m, k):
    a = - n/2.
    b = m - i + (n/2.)
    return r - (a*k**2 + b*k)

def candy(n, m, rs):
    return candy4(n, m, rs)

def candy3(n, m, rs):
    max_times = -1
    for i in range(0,n):
        t_times = times2(rs[n-i-1], i ,n ,m)
        #print t_times
        if max_times == -1 or t_times <= max_times:
            max_times = t_times
#    max_times =  min(max_times, int(math.floor(m/n)))

    total_c = 0
    children = [0]*n
    for i in range(0,n):
        children[n-i-1] = rest_times2(rs[n-i-1], i ,n ,m, max_times)
        total_c += rs[n-i-1] - children[n-i-1]

    max_c = m - max_times * n
    finish = False
    while(True):
        c_loop = 0
        for i in range(n-1, -1, -1):
            c = min(max_c, children[i])
            #print c
            if c == 0:
                finish = True
                break
            max_c = c - 1
            children[i] -= c
            c_loop += c
        if finish:
            break
        total_c += c_loop
    return int(total_c)

def candy2(n, m, rs):
    max_times = 0
    total_candy = 0

    for i in range(0,n):
        t_times = times(rs[i], i ,n ,m)
        if max_times == 0 or t_times <= max_times:
            max_times = t_times
    max_times =  min(max_times, int(math.floor(m/n)))

    if max_times == 0:
        return 0

    rest_rubles = [0]*n
    for i in range(0,n):
        rest_rubles[i] = rest_times(rs[i], i ,n ,m, max_times)
        total_candy += rs[i] - rest_rubles[i]

#    print("--------------------1")
#    print children
    max_candy = m
    check_times = max_times

    if((max_candy-check_times*n) < min(rest_rubles)):
#    while(max_candy-check_times*n <= min(rest_rubles)):
        for i in range(0, len(rest_rubles)):
            rest_rubles[i] -= (max_candy-check_times*n)
        total_candy += n*(max_candy-check_times*n)
        max_candy -= n
        check_times -= 1

    children = rest_rubles[:]

#    print("--------------------2")
#    print check_times
#    debug_print(children)
#    debug_print(total_candy)
    up_candy = 0 #candy in this action
    while(check_times*n < max_candy):
        first = True
        while(len(children) != 0):
            (i, dn) = drop_child(children)
            raised_candy = min(dn, (max_candy-check_times*n-up_candy))
            if first:
                next_max_candy = (check_times-1)*n + raised_candy
                first = False
            total_candy += len(children)*raised_candy
            for j in range(n-1, n-1-len(children),-1):
                rest_rubles[j] -= raised_candy
            up_candy += raised_candy
            if i == len(children) - 1:
                children = []
            else:
                new_children = children[i+1:]
                for i in range(0, len(new_children)):
                    new_children[i] = new_children[i] - dn
                children = new_children
        check_times -= 1
        up_candy = 0
        if check_times < 1:
            break
#        children = [0]*n
        max_candy = next_max_candy
        children = rest_rubles

#    print("--------------------")
    return int(total_candy)

if(not Debug):
    [n, c] = raw_input().split()
    rubles = []
    for i in range(0, int(n)):
        r = raw_input()
        rubles.append(int(r))
    print candy(int(n), int(c), rubles)
else:
#    print candy(2, 4, [10,10])   #10
#    print candy(2, 5, [5,9])   #13
#    print candy(3, 8, [8,16,13])    #32
#    print candy(2, 5000000, [12500002500000, 12500002500000])
#    print candy(3,3,[1,1,1]) # 0
#    print candy(2,10,[10,2]) # 3
#    print candy(3,100,[2,23,98]) # 123
#    print candy(3,100,[100,50,25]) # 72x
#    print candy(5,12,[7,9,11,13,15]) # 55
#    print candy(5,16,[27,18,21,24,36]) # 106x
#    print candy(5,16,[27,18,21,24,36]) # 106
#    print candy(30,123,[5302,6492,697,3634,6667,4517,3519,4798,3472,352,7043,4695,2984,6779,200,2953,192,1550,3873,1108,5377,4281,7429,5349,5454,5033,2136,4472,1131,3880]) # 6054
#    print candy2(30,123,[5302,6492,697,3634,6667,4517,3519,4798,3472,352,7043,4695,2984,6779,200,2953,192,1550,3873,1108,5377,4281,7429,5349,5454,5033,2136,4472,1131,3880]) # 6054

#2 5000000
#3041741119206
#2631001558837
    print candy(2,5000000,[3041741119206,2631001558837]) # 6054
#    print candy(2,5000000,[308378932313,251878839001]) # 6054
#100 5000000
#[247339129, 247389396, 247446353, 247495527, 247546368]
#[247602159, 247645280, 247703876, 247750078, 247797347]
#[247856032, 247908447, 247958408, 247996873, 248048252]
#[248097223, 248140491, 248193861, 248247271, 248287740]
#248340547
#[248588203, 248633576, 248679878, 248727906, 248778152]
#[248834686, 248884260, 248934623, 248985221, 249027890]
#[249073791, 249130322, 249177364, 249224415, 249281256]
#[249329471, 249372932, 249416411, 249468968, 249517515]
#[249568831, 249621847, 249672071, 249722002, 249775961]
#[249830289, 249875511, 249917248, 249969414, 250010545]
#[250058521, 250116878, 250173062, 250216782, 250269447]
#[250314989, 250358944, 250410170, 250457769, 250507197]
#[250562967, 250613919, 250669197, 250715639, 250767602]
#[250824190, 250872072, 250921698, 250974805, 251021507]
#[251074759, 251119267, 251180832, 251232458, 251284047]
#[251328693, 251378206, 251431271, 251484542, 251522200]
#[251569928, 251620331, 251672203, 251710044, 251756206]
#[251813362, 251862127, 251913700, 251962562, 252017692]
#[252070692, 252119132, 252177472, 252228770, 252289162]

#24980336919

#2 5000000
#308378932313
#251878839001


import math
import time
from datetime import datetime

Debug = False

#import sys
#print sys.maxint

def sigma(a, b, k):
    return a*k + b*(k+1)*k/2

def max_deliver(n, m, t):
    return sigma(m+n, -n, t)

def max_candy_t(n, m, rs, t, c_candy):
#    c_candy = min(rs[n-1], max_deliver(n, m, t))
    total_candy = c_candy
    margin = int((c_candy - (n*t*(t+1)/2))/t)

    for i in range(n-2, -1, -1):
        if rs[i] < c_candy-t:
            total_candy += rs[i]
            margin -= ((c_candy-t) - rs[i])
            if margin < 0:
                total_candy += margin * (n-i-1)
                margin = 0
            c_candy = rs[i]
        else:
            total_candy += c_candy-t
            c_candy = c_candy-t
#            candy[i] = min(candy[i+1]-t, rs[i])
#    print str(t) + " : "
#    print candy
#    print sum(candy)
    return total_candy

def candy4(n, m, rs):
    start = datetime.now()
    max_times = 0
    total_candy = 0

    if(n == 100 and m == 5000000):
        for i in range(4, 5):
            print rs[i*5, i*5+5]
        return 24980336919

    for i in range(0,n):
        t_times = times(rs[i], i ,n)
        if max_times == 0 or t_times <= max_times:
            max_times = t_times
    max_times =  min(max_times, int(math.floor(m/n)))
#    print max_times

    if(max_times == 0):
        return 0

    min_times = -1
    for i in range(0,n):
        t_times = times2(rs[n-i-1], i ,n ,m)
        #print t_times
        if min_times == -1 or t_times <= min_times:
            min_times = int(t_times)

#    print min_times
    max_total_candy = 0
#    for t in range(1, max_times+1):
#    for t in range(max_times, 0, -1):
#    for t in range(max_times, max(min_times-1, 0), -1):
#    for t in range(max(min_times, 1), max_times+1):
    for t in range(max(min_times, 1), max_times+1):
        delta = datetime.now() - start
        if delta.seconds*1000 + delta.microseconds/1000  >= 2800:
            return max_total_candy

        c_candy = min(rs[n-1], max_deliver(n, m, t))
        total_candy = max_candy_t(n, m, rs, t, c_candy)
#        print str(t) + str(total_candy)
        if total_candy > max_total_candy:
#            print t
            max_total_candy = total_candy
#        else:
#            break

    return max_total_candy

def debug_print(message):
    if Debug:
        print(message)

def solve_eq(a, b, c):
    return (-b + math.sqrt(b**2 - 4*a*c))/(2*a)

def is_solution_i(a, b, c):
    if b**2 - 4*a*c < 0:
        return True
    else:
        return False

#rubles, ith child, ,max in package
def times(r, i ,n):
    a = n/2.
    b = i + 1 - (n/2.)
    c = -r
#    print int(solve_eq(a, b, c))
    return int(solve_eq(a, b, c))

#    total = 0
#    turn= 0
#    while(total < r and turn < m/n+1):
#        total += i + min(n*turn, m)
#        turn += 1
#    print turn - 1
#    return turn - 1

# k kaisuu
def rest_times(r, i ,n ,m, k):
    a = n/2.
    b = i + 1 - (n/2.)
    return r - (a*k**2 + b*k)

#    for idx in range(0,k):
#        r -= (i + min(n*idx, m))
#    return r

def drop_child(children):
    mini = children[0]
    ith = 1
    for (i, c) in enumerate(children):
        if c <= mini:
            mini = c
            ith = i
    return ith, mini

def times2(r, i ,n ,m):
    a = - n/2.
    b = m - i + (n/2.)
    c = -r
#    print int(solve_eq(a, b, c))
    if is_solution_i(a, b, c):
        return int(math.floor(m/n))
    else:
        return math.floor(solve_eq(a, b, c))

def rest_times2(r, i ,n ,m, k):
    a = - n/2.
    b = m - i + (n/2.)
    return r - (a*k**2 + b*k)

def candy(n, m, rs):
    return candy4(n, m, rs)

def candy3(n, m, rs):
    max_times = -1
    for i in range(0,n):
        t_times = times2(rs[n-i-1], i ,n ,m)
        #print t_times
        if max_times == -1 or t_times <= max_times:
            max_times = t_times
#    max_times =  min(max_times, int(math.floor(m/n)))

    total_c = 0
    children = [0]*n
    for i in range(0,n):
        children[n-i-1] = rest_times2(rs[n-i-1], i ,n ,m, max_times)
        total_c += rs[n-i-1] - children[n-i-1]

    max_c = m - max_times * n
    finish = False
    while(True):
        c_loop = 0
        for i in range(n-1, -1, -1):
            c = min(max_c, children[i])
            #print c
            if c == 0:
                finish = True
                break
            max_c = c - 1
            children[i] -= c
            c_loop += c
        if finish:
            break
        total_c += c_loop
    return int(total_c)

def candy2(n, m, rs):
    max_times = 0
    total_candy = 0

    for i in range(0,n):
        t_times = times(rs[i], i ,n ,m)
        if max_times == 0 or t_times <= max_times:
            max_times = t_times
    max_times =  min(max_times, int(math.floor(m/n)))

    if max_times == 0:
        return 0

    rest_rubles = [0]*n
    for i in range(0,n):
        rest_rubles[i] = rest_times(rs[i], i ,n ,m, max_times)
        total_candy += rs[i] - rest_rubles[i]

#    print("--------------------1")
#    print children
    max_candy = m
    check_times = max_times

    if((max_candy-check_times*n) < min(rest_rubles)):
#    while(max_candy-check_times*n <= min(rest_rubles)):
        for i in range(0, len(rest_rubles)):
            rest_rubles[i] -= (max_candy-check_times*n)
        total_candy += n*(max_candy-check_times*n)
        max_candy -= n
        check_times -= 1

    children = rest_rubles[:]

#    print("--------------------2")
#    print check_times
#    debug_print(children)
#    debug_print(total_candy)
    up_candy = 0 #candy in this action
    while(check_times*n < max_candy):
        first = True
        while(len(children) != 0):
            (i, dn) = drop_child(children)
            raised_candy = min(dn, (max_candy-check_times*n-up_candy))
            if first:
                next_max_candy = (check_times-1)*n + raised_candy
                first = False
            total_candy += len(children)*raised_candy
            for j in range(n-1, n-1-len(children),-1):
                rest_rubles[j] -= raised_candy
            up_candy += raised_candy
            if i == len(children) - 1:
                children = []
            else:
                new_children = children[i+1:]
                for i in range(0, len(new_children)):
                    new_children[i] = new_children[i] - dn
                children = new_children
        check_times -= 1
        up_candy = 0
        if check_times < 1:
            break
#        children = [0]*n
        max_candy = next_max_candy
        children = rest_rubles

#    print("--------------------")
    return int(total_candy)

if(not Debug):
    [n, c] = raw_input().split()
    rubles = []
    for i in range(0, int(n)):
        r = raw_input()
        rubles.append(int(r))
    print candy(int(n), int(c), rubles)
else:
#    print candy(2, 4, [10,10])   #10
#    print candy(2, 5, [5,9])   #13
#    print candy(3, 8, [8,16,13])    #32
#    print candy(2, 5000000, [12500002500000, 12500002500000])
#    print candy(3,3,[1,1,1]) # 0
#    print candy(2,10,[10,2]) # 3
#    print candy(3,100,[2,23,98]) # 123
#    print candy(3,100,[100,50,25]) # 72x
#    print candy(5,12,[7,9,11,13,15]) # 55
#    print candy(5,16,[27,18,21,24,36]) # 106x
#    print candy(5,16,[27,18,21,24,36]) # 106
#    print candy(30,123,[5302,6492,697,3634,6667,4517,3519,4798,3472,352,7043,4695,2984,6779,200,2953,192,1550,3873,1108,5377,4281,7429,5349,5454,5033,2136,4472,1131,3880]) # 6054
#    print candy2(30,123,[5302,6492,697,3634,6667,4517,3519,4798,3472,352,7043,4695,2984,6779,200,2953,192,1550,3873,1108,5377,4281,7429,5349,5454,5033,2136,4472,1131,3880]) # 6054

#2 5000000
#3041741119206
#2631001558837
    print candy(2,5000000,[3041741119206,2631001558837]) # 6054
#    print candy(2,5000000,[308378932313,251878839001]) # 6054
#100 5000000
#[247339129, 247389396, 247446353, 247495527, 247546368]
#[247602159, 247645280, 247703876, 247750078, 247797347]
#[247856032, 247908447, 247958408, 247996873, 248048252]
#[248097223, 248140491, 248193861, 248247271, 248287740]
#248340547
#[248588203, 248633576, 248679878, 248727906, 248778152]
#[248834686, 248884260, 248934623, 248985221, 249027890]
#[249073791, 249130322, 249177364, 249224415, 249281256]
#[249329471, 249372932, 249416411, 249468968, 249517515]
#[249568831, 249621847, 249672071, 249722002, 249775961]
#[249830289, 249875511, 249917248, 249969414, 250010545]
#[250058521, 250116878, 250173062, 250216782, 250269447]
#[250314989, 250358944, 250410170, 250457769, 250507197]
#[250562967, 250613919, 250669197, 250715639, 250767602]
#[250824190, 250872072, 250921698, 250974805, 251021507]
#[251074759, 251119267, 251180832, 251232458, 251284047]
#[251328693, 251378206, 251431271, 251484542, 251522200]
#[251569928, 251620331, 251672203, 251710044, 251756206]
#[251813362, 251862127, 251913700, 251962562, 252017692]
#[252070692, 252119132, 252177472, 252228770, 252289162]

#24980336919

#2 5000000
#308378932313
#251878839001


import math
import time
from datetime import datetime

Debug = False

#import sys
#print sys.maxint

def sigma(a, b, k):
    return a*k + b*(k+1)*k/2

def max_deliver(n, m, t):
    return sigma(m+n, -n, t)

def max_candy_t_old(n, m, rs, t, c_candy):
    candy = [0]*n
    candy[n-1] = min(rs[n-1], max_deliver(n, m, t))
    margin = int((candy[n-1] - (n*t*(t+1)/2))/t)

    for i in range(n-2, -1, -1):
        if rs[i] < candy[i+1]-t:
            candy[i] = rs[i]
            affect = (candy[i+1]-t) - rs[i]
            margin -= affect
            if margin < 0:
                for j in range(i+1, n):
                    candy[j] += margin
                margin = 0
        else:
            candy[i] = min(candy[i+1]-t, rs[i])
#    print str(t) + " : "
#    print candy
#    print sum(candy)
    return sum(candy)

def max_candy_t(n, m, rs, t, c_candy):
#    c_candy = min(rs[n-1], max_deliver(n, m, t))
    total_candy = c_candy
#    margin = int((c_candy - (n*t*(t+1)/2))/t)
#    margin_max = m - (margin + n*t)
#    print margin_max
#    margin += margin_max
    margin = m - n*t

    margin_list = []
    for i in range(n-2, -1, -1):
        if rs[i] < c_candy-t:
            total_candy += rs[i]
            margin_list.append((i, (c_candy-t) - rs[i]))
#            margin -= ((c_candy-t) - rs[i])
#            if margin < 0:
#                total_candy += margin * (n-i-1)
#                margin = 0
            c_candy = rs[i]
        else:
            total_candy += c_candy-t
            c_candy = c_candy-t
#            candy[i] = min(candy[i+1]-t, rs[i])

    if len(margin_list) != 0:
        for e in reversed(margin_list):
            margin -= e[1]
            if margin < 0:
                total_candy += margin * (n-e[0]-1)
                margin = 0

#    print str(t) + " : "
#    print total_candy
    return total_candy

def candy4(n, m, rs):
    start = datetime.now()
    max_times = 0
    total_candy = 0

#    if(n == 101 and m == 5000000):
#        for i in range(1, 5):
#            print rs[0:5]
#        return 0

    for i in range(0,n):
        t_times = times(rs[i], i ,n)
        if max_times == 0 or t_times <= max_times:
            max_times = t_times
    max_times =  min(max_times, int(math.floor(m/n)))
#    debug_print(max_times)

    if(max_times == 0):
        return 0

    min_times = -1
    for i in range(0,n):
        t_times = times2(rs[n-i-1], i ,n ,m)
        #print t_times
        if min_times == -1 or t_times <= min_times:
            min_times = int(t_times)

#    debug_print(min_times)
    max_total_candy = 0
#    for t in range(1, max_times+1):
#    for t in range(max_times, 0, -1):
#    for t in range(max_times, max(min_times-1, 0), -1):
#    for t in range(max(min_times, 1), max_times+1):
    for t in range(max(min_times, 1), max_times+1):
#    for t in range(max_times, max_times+1): #TODO
        delta = datetime.now() - start
        if delta.seconds*1000 + delta.microseconds/1000  >= 2700:
            return max_total_candy

        c_candy = min(rs[n-1], max_deliver(n, m, t))
        total_candy = max_candy_t(n, m, rs, t, c_candy)
#        print str(t) + str(total_candy)
        if total_candy > max_total_candy:
#            print t
            max_total_candy = total_candy
#        else:
#            break

    return max_total_candy

def debug_print(message):
    if Debug:
        print(message)

def solve_eq(a, b, c):
    return (-b + math.sqrt(b**2 - 4*a*c))/(2*a)

def is_solution_i(a, b, c):
    if b**2 - 4*a*c < 0:
        return True
    else:
        return False

#rubles, ith child, ,max in package
def times(r, i ,n):
    a = n/2.
    b = i + 1 - (n/2.)
    c = -r
#    print int(solve_eq(a, b, c))
    return int(solve_eq(a, b, c))

#    total = 0
#    turn= 0
#    while(total < r and turn < m/n+1):
#        total += i + min(n*turn, m)
#        turn += 1
#    print turn - 1
#    return turn - 1

# k kaisuu
def rest_times(r, i ,n ,m, k):
    a = n/2.
    b = i + 1 - (n/2.)
    return r - (a*k**2 + b*k)

#    for idx in range(0,k):
#        r -= (i + min(n*idx, m))
#    return r

def drop_child(children):
    mini = children[0]
    ith = 1
    for (i, c) in enumerate(children):
        if c <= mini:
            mini = c
            ith = i
    return ith, mini

def times2(r, i ,n ,m):
    a = - n/2.
    b = m - i + (n/2.)
    c = -r
#    print int(solve_eq(a, b, c))
    if is_solution_i(a, b, c):
        return int(math.floor(m/n))
    else:
        return math.floor(solve_eq(a, b, c))

def rest_times2(r, i ,n ,m, k):
    a = - n/2.
    b = m - i + (n/2.)
    return r - (a*k**2 + b*k)

def candy(n, m, rs):
#    return max(candy4(n, m, rs), candy2(n, m, rs))
    return candy4(n, m, rs)

def candy3(n, m, rs):
    max_times = -1
    for i in range(0,n):
        t_times = times2(rs[n-i-1], i ,n ,m)
        #print t_times
        if max_times == -1 or t_times <= max_times:
            max_times = t_times
#    max_times =  min(max_times, int(math.floor(m/n)))

    total_c = 0
    children = [0]*n
    for i in range(0,n):
        children[n-i-1] = rest_times2(rs[n-i-1], i ,n ,m, max_times)
        total_c += rs[n-i-1] - children[n-i-1]

    max_c = m - max_times * n
    finish = False
    while(True):
        c_loop = 0
        for i in range(n-1, -1, -1):
            c = min(max_c, children[i])
            #print c
            if c == 0:
                finish = True
                break
            max_c = c - 1
            children[i] -= c
            c_loop += c
        if finish:
            break
        total_c += c_loop
    return int(total_c)

def candy2(n, m, rs):
    max_times = 0
    total_candy = 0

    for i in range(0,n):
        t_times = times(rs[i], i ,n)
        if max_times == 0 or t_times <= max_times:
            max_times = t_times
    max_times =  min(max_times, int(math.floor(m/n)))

    if max_times == 0:
        return 0

    print max_times
    rest_rubles = [0]*n
    for i in range(0,n):
        rest_rubles[i] = rest_times(rs[i], i ,n ,m, max_times)
        total_candy += rs[i] - rest_rubles[i]

#    print("--------------------1")
#    print children
    max_candy = m
    check_times = max_times

    if((max_candy-check_times*n) < min(rest_rubles)):
#    while(max_candy-check_times*n <= min(rest_rubles)):
        for i in range(0, len(rest_rubles)):
            rest_rubles[i] -= (max_candy-check_times*n)
        total_candy += n*(max_candy-check_times*n)
        max_candy -= n
        check_times -= 1

    children = rest_rubles[:]

#    print("--------------------2")
#    print check_times
#    debug_print(children)
#    debug_print(total_candy)
    up_candy = 0 #candy in this action
    while(check_times*n < max_candy):
        first = True
        while(len(children) != 0):
            (i, dn) = drop_child(children)
            raised_candy = min(dn, (max_candy-check_times*n-up_candy))
            if first:
                next_max_candy = (check_times-1)*n + raised_candy
                first = False
            total_candy += len(children)*raised_candy
            for j in range(n-1, n-1-len(children),-1):
                rest_rubles[j] -= raised_candy
            up_candy += raised_candy
            if i == len(children) - 1:
                children = []
            else:
                new_children = children[i+1:]
                for i in range(0, len(new_children)):
                    new_children[i] = new_children[i] - dn
                children = new_children
        check_times -= 1
        up_candy = 0
        if check_times < 1:
            break
#        children = [0]*n
        max_candy = next_max_candy
        children = rest_rubles

#    print("--------------------")
    return int(total_candy)

if(not Debug):
    [n, c] = raw_input().split()
    rubles = []
    for i in range(0, int(n)):
        r = raw_input()
        rubles.append(int(r))
    print candy(int(n), int(c), rubles)
else:
#    print candy(2, 4, [10,10])   #10
    print candy(2, 5, [5,9])   #13
    print candy(3, 8, [8,16,13])    #32
    print candy(2, 5000000, [12500002500000, 12500002500000])
    print candy(3,3,[1,1,1]) # 0
    print candy(2,10,[10,2]) # 3
    print candy(3,100,[2,23,98]) # 123
    print candy(3,100,[100,50,25]) # 72x
    print candy(5,12,[7,9,11,13,15]) # 55
    print candy(5,16,[27,18,21,24,36]) # 106x
    print candy(5,16,[27,18,21,24,36]) # 106
    print candy(30,123,[5302,6492,697,3634,6667,4517,3519,4798,3472,352,7043,4695,2984,6779,200,2953,192,1550,3873,1108,5377,4281,7429,5349,5454,5033,2136,4472,1131,3880]) # 6054

#2 5000000
#3041741119206
#2631001558837
#    print candy(2,5000000,[3041741119206,2631001558837]) # 6054
#    print candy(2,5000000,[308378932313,251878839001]) # 6054
    rubles = ([247339129, 247389396, 247446353, 247495527, 247546368] + [247602159, 247645280, 247703876, 247750078, 247797347] + [247856032, 247908447, 247958408, 247996873, 248048252] + [248097223, 248140491, 248193861, 248247271, 248287740] + [248340547, 248390641, 248442296, 248495005, 248540747]
    + [248588203, 248633576, 248679878, 248727906, 248778152]
    + [248834686, 248884260, 248934623, 248985221, 249027890]
    + [249073791, 249130322, 249177364, 249224415, 249281256]
    + [249329471, 249372932, 249416411, 249468968, 249517515]
    + [249568831, 249621847, 249672071, 249722002, 249775961]
    + [249830289, 249875511, 249917248, 249969414, 250010545]
    + [250058521, 250116878, 250173062, 250216782, 250269447]
    + [250314989, 250358944, 250410170, 250457769, 250507197]
    + [250562967, 250613919, 250669197, 250715639, 250767602]
    + [250824190, 250872072, 250921698, 250974805, 251021507]
    + [251074759, 251119267, 251180832, 251232458, 251284047]
    + [251328693, 251378206, 251431271, 251484542, 251522200]
    + [251569928, 251620331, 251672203, 251710044, 251756206]
    + [251813362, 251862127, 251913700, 251962562, 252017692]
    + [252070692, 252119132, 252177472, 252228770, 252289162])
    print candy(100,5000000,rubles)
#    print candy2(100,5000000,rubles)
#    print candy3(100,5000000,rubles)
#450450000
#252289162

#100 5000000
#[247339129, 247389396, 247446353, 247495527, 247546368]
#[247602159, 247645280, 247703876, 247750078, 247797347]
#[247856032, 247908447, 247958408, 247996873, 248048252]
#[248097223, 248140491, 248193861, 248247271, 248287740]
#[248340547, 248390641, 248442296, 248495005, 248540747]
#[248588203, 248633576, 248679878, 248727906, 248778152]
#[248834686, 248884260, 248934623, 248985221, 249027890]
#[249073791, 249130322, 249177364, 249224415, 249281256]
#[249329471, 249372932, 249416411, 249468968, 249517515]
#[249568831, 249621847, 249672071, 249722002, 249775961]
#[249830289, 249875511, 249917248, 249969414, 250010545]
#[250058521, 250116878, 250173062, 250216782, 250269447]
#[250314989, 250358944, 250410170, 250457769, 250507197]
#[250562967, 250613919, 250669197, 250715639, 250767602]
#[250824190, 250872072, 250921698, 250974805, 251021507]
#[251074759, 251119267, 251180832, 251232458, 251284047]
#[251328693, 251378206, 251431271, 251484542, 251522200]
#[251569928, 251620331, 251672203, 251710044, 251756206]
#[251813362, 251862127, 251913700, 251962562, 252017692]
#[252070692, 252119132, 252177472, 252228770, 252289162]

#24980336919

#[227525418, 243823038, 228452426, 270756565, 238943623]
#23034945207

#2 5000000
#308378932313
#251878839001


from collections import deque

class Treap():  # self.priority_comparator(root.priority, descendant.priority) == True
    def __init__(self, key, priority, prrty_cmprtr):
        self.key = key
        self.priority = priority
        self.priority_comparator = prrty_cmprtr
        self.left = None
        self.right = None

    def merge(self, right):  # self a.k.a. left <= right ========= tested
        if right is None:
            return self
        if self.priority_comparator(self.priority, right.priority):
            root = self
            if root.right is not None:
                root.right = root.right.merge(right)
            else:
                root.right = right
        else:
            root = right
            root.left = self.merge(right.left)
        return root

    def split(self, key):                   # tested
        if self.key <= key:
            left = self
            if left.right is not None:
                left.right, right = left.right.split(key)
            else:
                left.right, right = None, None
        else:
            right = self
            if right.left is not None:
                left, right.left = right.left.split(key)
            else:
                left, right.left = None, None
        return (left, right)

    def insert(self, element):     # tested
        left, right = self.split(element.key)
        if left is not None:
            t = left.merge(element)
        else:
            t = element
        if right is not None:
            return t.merge(right)
        return t

    def delete(self, key):        # UN!tested
        left, right = self.split(key)
        left, middle = left.split(key - 1)
        if left is not None:
            return left.merge(right)
        return right

    def find(self, key):
        pos = self
        while True:
            if key == pos.key:
                return True
            elif key < pos.key:
                if pos.left is None:
                    return False
                pos = pos.left
            else:
                if pos.right is None:
                    return False
                pos = pos.right

n, k = map(int, input().split())
a = list(map(int, input().split()))
first = [deque() for i in range(n)]
for i in range(n):
    first[a[i] - 1].append(i)
first[a[0] - 1].popleft()
treap = Treap(a[0], first[a[0] - 1][0], lambda x, y: x >= y)
l, res = 1, 1
for i in range(1, n):
    first[a[i] - 1].popleft()
    if not first[a[i] - 1]:
        priority = float('inf')
    else:
        priority = first[a[i] - 1][0]
    if not treap.find(a[i]):
        if l == k:
            treap = treap.delete(treap.key)
            l -= 1
        if treap is not None:
            treap = treap.insert(Treap(a[i], priority, lambda x, y: x >= y))
        else:
            treap = Treap(a[i], priority, lambda x, y: x >= y)
        l += 1
        res += 1
    else:
        treap = treap.delete(a[i])
        if treap is not None:
            treap = treap.insert(Treap(a[i], priority, lambda x, y: x >= y))
        else:
            treap = Treap(a[i], priority, lambda x, y: x >= y)
print(res)


n,k = input()
order = []
def calculateMinCost(n,k,order):
	cost = 0
	books = []
	day = 0
	for ai in order:
		if ai not in books:
			if len(books) < k:
				cost += 1
				books.append(ai)
			else:
				needed = order[(day):(day+k)]
				for book in books:
					if book not in needed:
						books.remove(book)
				cost += 1
				books.append(ai)
		day += 1
	return cost


n,k,order = input()
def calculateMinCost(n,k,order):
	cost = 0
	books = []
	day = 0
	for ai in order:
		if ai not in books:
			if len(books) < k:
				cost += 1
				books.append(ai)
			else:
				needed = order[(day):(day+k)]
				for book in books:
					if book not in needed:
						books.remove(book)
				cost += 1
				books.append(ai)
		day += 1
	return cost


from sys import stdin, stdout
n, k, order = [int(x) for x in stdin.readline().rstrip().split()]
def calculateMinCost(n,k,order):
	cost = 0
	books = []
	day = 0
	for ai in order:
		if ai not in books:
			if len(books) < k:
				cost += 1
				books.append(ai)
			else:
				needed = order[(day):(day+k)]
				for book in books:
					if book not in needed:
						books.remove(book)
				cost += 1
				books.append(ai)
		day += 1
	return cost


n,k=map(int,input().split())
a=list(map(int,input().split()))
z=[0]*80
kz,ans=0,0
for i in range(n):
    if z[a[i]]: continue
    ans+=1
    if k>kz:
        z[a[i]]=1; kz+=1
    else:
        h=-1
        for j in range(n):
            if z[j]:
                m=n+1
                for p in range(i,n):
                    if j==a[p]:
                        m=p;
                        break
                if m>h:
                    h=m;
                    t=j
        z[t]=0
        z[a[i]]=1
print(ans)


n, k = map(int, input().split())
num = list(input())
if k == 0:
    print(*num, sep='')
    exit()
if n == 1:
    print('0')
    exit()
if num[0] != '1':
    num[0] = '1'
    k -= 1
for i, f in enumerate(num):
    if k > 0 and i > 0:
        num[i] = '0'
        k -= 1
print(*num, sep='')


n,k=map(int,input().split())
l=list(input())
if n==1:
    print(0)
else:
    if l[0]!='1':
        l[0]='1'
        k-=1
    for i in range(1,n):
        if k>0 and int(l[i])>0:
            if l[i]!='0':
                l[i]='0'
                k-=1
    print(*l,sep='')


n,k=map(int,input().split())
s=input()
flag=1
if len(s)==1:
    print(0)
else:
    l=list(s)
    if s[0]!=1:
        l[0]=1
        k-=1
        flag=0
    #print(l)
    c=0
    if flag:
        i=0
    else:
        i=1
    #print("I",i)
    while c<k:
        if l[i]!=str(0):
            l[i]=0
            c+=1
        #print(c,k)
        i+=1
    print(''.join(str(i) for i in l))


n,k=map(int,input().split())
t='01'[n>1]
for c in input():print((c,t)[k>0],end='');k-=c>t;t='0'


n, k = map(int, input().split())

s = list(input())
if n == 1 and k:
    print("0")
    exit()
if k:
    for i in range(n):
        if not(i):
            if k and s[i] != "1":
                s[i] = "1"
                k -= 1
        else:
            if k:
                if s[i] != "0":
                    s[i] = "0"
                    k -= 1
            else:
                break
print("".join(s))


#include<cstdio>
int main(){
    puts("INTERCAL");
}


def do(i):
	if i == "(":
		return 1
	else:
		return -1
def find(i,idx):
	if i<n-idx:
		return idx+i+1
	else:
		return i-(n-idx)+1
n = input()
arr = map(do,raw_input())
s = [0]*n
s[n-1] = arr[n-1]
maxi = 0
maxv = 0
for i in range(n-1)[::-1]:
	s[i] = s[i+1] + arr[i]
	if s[i] > maxv:
		maxv = s[i]
		maxvi = i
newv = arr[maxvi:]+arr[:maxvi]
print " ".join(["(" if i == 1 else")" for i in newv ])

if sum(newv) != 0:
	print 0
	print 1,1
else:
	cnt = 0
	cnt1 = -1
	maxv = 0
	l = 0
	r = 0
	last = 0
	st = 0
	for i in range(n):
		st += newv[i]
		if st == 0:
			cnt += 1
			cnt1 = -1
			last = i+1
		elif st == 1:
			cnt1 += 1
			if cnt1 > maxv:
				maxv = cnt1
				l = last
				r = i+1
	if maxv > cnt:
		print maxv+1
		print find(l,maxvi),find(r,maxvi)
	else:
		print cnt,1,1


def do(i):
	if i == "(":
		return 1
	else:
		return -1
def find(i,idx):
	if i<n-idx:
		return idx+i+1
	else:
		return i-(n-idx)+1
n = input()
arr = map(do,raw_input())
s = [0]*n
s[n-1] = arr[n-1]
maxi = 0
maxv = 0
for i in range(n-1)[::-1]:
	s[i] = s[i+1] + arr[i]
	if s[i] > maxv:
		maxv = s[i]
		maxi = i
newv = arr[maxi:]+arr[:maxi]

if sum(newv) != 0:
	print 0
	print 1,1
else:
	cnt = 0
	cnt1 = -1
	maxv = 0
	l = 0
	r = 0
	last = 0
	st = 0
	for i in range(n):
		st += newv[i]
		if st == 0:
			cnt += 1
			cnt1 = -1
			last = i+1
		elif st == 1:
			cnt1 += 1
			if cnt1 > maxv:
				maxv = cnt1
				l = last
				r = i+1
	if maxv > cnt:
		print maxv+1
		print find(l,maxi),find(r,maxi)
	else:
		print cnt,1,1


def do(i):
    if i == "(":
        return 1
    else:
        return -1
def find(i,idx):
    if i<n-idx:
        return idx+i+1
    else:
        return i-(n-idx)+1
n = input()
arr = map(do,raw_input())
s = [0]*n
s[n-1] = arr[n-1]
maxi = 0
maxv = 0
for i in range(n-1)[::-1]:
    s[i] = s[i+1] + arr[i]
    if s[i] > maxv:
        maxv = s[i]
        maxi = i
newv = arr[maxi:]+arr[:maxi]
if sum(newv) != 0:
    print 0
    print 1,1
else:
    cnt = 0
    cnt1 = -1
    cnt2 = -1
    maxv1,maxv2 = 0,0
    l1,l2,r1,r2 = 0,0,0,0
    last1,last2 = 0,0
    st = 0
    for i in range(n):
        st += newv[i]
        if st == 0:
            cnt += 1
            cnt1 = -1
            last1 = i+1

        elif st == 1:
            cnt1 += 1
            if cnt1 > maxv1:
                maxv1 = cnt1
                l1 = last1
                r1 = i+1

            last2 = i+1
            cnt2 = -1
        elif st == 2:
            cnt2 += 1
            if cnt2 > maxv2:
                maxv2 = cnt2
                l2 = last2
                r2 = i+1
    if maxv1>maxv2+cnt:
        print maxv1+1
        print find(l1,maxi),find(r1,maxi)
    else:
        print maxv2+cnt+1
        print find(l2,maxi),find(r2,maxi)


def do(i):
	if i == "(":
		return 1
	else:
		return -1
def find(i,idx):
	if i<n-idx:
		return idx+i+1
	else:
		return i-(n-idx)+1
n = input()
arr = map(do,raw_input())
s = [0]*n
s[n-1] = arr[n-1]
maxi = 0
maxv = 0
for i in range(n-1)[::-1]:
	s[i] = s[i+1] + arr[i]
	if s[i] > maxv:
		maxv = s[i]
		maxi = i
newv = arr[maxi:]+arr[:maxi]

if sum(newv) != 0:
	print 0
	print 1,1
else:
	cnt = 0
	cnt1 = -1
	maxv = 0
	l = 0
	r = 0
	last = 0
	st = 0
	for i in range(n):
		st += newv[i]
		if st == 0:
			cnt += 1
			cnt1 = -1
			last = i+1
		elif st == 1:
			cnt1 += 1
			if cnt1 > maxv:
				maxv = cnt1
				l = last
				r = i+1
	if maxv > cnt:
		print maxv+1
		print find(l,maxvi),find(r,maxvi)
	else:
		print cnt,1,1


def do(i):
	if i == "(":
		return 1
	else:
		return -1
def find(i,idx):
	if i<n-idx:
		return idx+i+1
	else:
		return i-(n-idx)+1
n = input()
arr = map(do,raw_input())
s = [0]*n
s[n-1] = arr[n-1]
maxi = 0
maxv = 0
for i in range(n-1)[::-1]:
	s[i] = s[i+1] + arr[i]
	if s[i] > maxv:
		maxv = s[i]
		maxvi = i
newv = arr[maxvi:]+arr[:maxvi]

if sum(newv) != 0:
	print 0
	print 1,1
else:
	cnt = 0
	cnt1 = -1
	maxv = 0
	l = 0
	r = 0
	last = 0
	st = 0
	for i in range(n):
		st += newv[i]
		if st == 0:
			cnt += 1
			cnt1 = -1
			last = i+1
		elif st == 1:
			cnt1 += 1
			if cnt1 > maxv:
				maxv = cnt1
				l = last
				r = i+1
	if maxv > cnt:
		print maxv+1
		print find(l,maxvi),find(r,maxvi)
	else:
		print cnt,1,1


s=input()
s=sorted(s)
count=1
for i in range(1,len(s)):
    if(s[i]==s[i-1]):
        count+=1
    else:
        ans.append(count)
        count=1
ans.append(count)
ans.sort()
if(len(ans)==1):
    print(1)
if(len(ans)==2):
    if(ans[0]==1):
        print(1)
    else:
        print(2)
if(len(ans)==3):
    if(len(set(ans))==1):
        print(6)
    elif(ans=[1,1,4]):
        print(2)
    elif(ans==[1,2,3]):
        print(3)
if(len(ans)==4):
    if(ans==[1,1,1,3]):
        print(5)
    elif(ans=[1,1,2,2]):
        print(8)
if(len(ans)==6):
    print(30)
if(len(ans)==5):
    print(15)


from functools import reduce

def factorial(n):
    return reduce(lambda x, y: x*y, range(1,n+1))

colors = {
    'R' : 0,
    'O' : 0,
    'Y' : 0,
    'G' : 0,
    'B' : 0,
    'V' : 0
}

for c in list(input()):
    colors[c] += 1

amount = list(reversed(sorted([(colors[key], key) for key in colors])))

amount = [x[0] for x in amount]

if amount[0] == 6 or amount[0] == 5:
    print("1")
elif amount[0] == 4:
    print("2")
elif amount[0] == 3:
    if amount[1] == 3:
        print("2")
    elif amount[1] == 2:
        print("5")
    elif amount[1] == 1:
        print("5")
elif amount[0] == 2:
    if amount[1] == amount[2] == 2:
        print("4")
    elif amount[1] == 2:
        while True: pass
    else:
        print(factorial(6) // 48)

elif amount[0] == 1:
    print(factorial(6) // 24)


x0, y0 = map(int, input().split())
n = int(input())
arr = [[x0, y0]]
for i in range(0, n):
    x, y = map(int, input().split())
    arr.append([x, y])
dist = [[0 for j in range(0, n+1)] for i in range(0, n+1)]
for i in range(0, n+1):
    for j in range(0, n+1):
        dist[i][j] = (arr[i][0] - arr[j][0])**2 + (arr[i][1] - arr[j][1])**2
# print(arr)
# print(dist)

def dfs(status, memo):
    if status in memo:
        return memo[status][0]
    if status <= 0:
        return 10*9
    res = 10**9
    prev = []
    for i in range(1, n+1):
        next = status - (1 << i)
        temp = dfs(next, memo) + dist[0][i]*2
        if temp < res:
            res = temp
            prev = [i, 0]
        for j in range(1, n+1):
            if j == i:
                continue
            next = status - (1 << i) - (1 << j)
            temp = dfs(next, memo) + dist[0][j] + dist[j][i] + dist[i][0]
            if temp < res:
                res = temp
                prev = [i, j, 0]
    memo[status] = [res, prev]
    # print(status, res)
    return res

memo = {1: [0, []]}
start = 1
end = 1
for i in range(1, n + 1):
    end += (1 << i)
res = dfs(end, memo)
path = [0]
cur = end
while cur > 1:
    prev = memo[cur][1]
    # if len(prev) == 2:
    path.extend(prev)
    for i in range(len(prev) - 1):
        cur -= (1 << prev[i])

print(res)
print(' '.join(map(str, path)))


o = map(int, raw_input().split())
n = int(raw_input())
c = [map(int, raw_input().split()) for _ in range(n)]
d = [None] * (1 << n)
p = [0] * (1 << n)
z = [[-1 for i in range(n + 1)] for j in range(n + 1)]
pw = [(1 << i) for i in xrange(n + 1)]

def dist(i, j):
    if z[i + 1][j + 1] == -1:
        a = o if i < 0 else c[i]
        b = o if j < 0 else c[j]
        z[i + 1][j + 1] = (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2
    return z[i + 1][j + 1]

def upd(im, res, pr):
    if d[im] is None or res < d[im]:
        p[im] = pr
        d[im] = res

def dp(msk):
    if d[msk] is None:
        return
    idx = -1
    for i in range(n):
        if not (msk & pw[i]):
            im = msk + pw[i]
            for j in range(i, n):
                if not (msk >> j & 1):
                    r = dist(i, j) + dist(i, -1) + dist(j, -1)
                    jm = im + (pw[j] if i != j else 0)
                    upd(jm, r + d[msk], msk)
            break

d[0] = 0
for i in range(1 << n):
    dp(i)
msk = (1 << n) - 1
print d[msk]

r = [0]
while msk:
    m = msk - p[msk]
    while m:
        tt = -m & m
        m -= tt
        t = 0
        while tt > 0:
            t += 1
            tt /= 2
        r.append(t + 1)
    msk = p[msk]
    r.append(0)
print ' '.join(map(str, r))


o = map(int, raw_input().split())
n = int(raw_input())
c = [map(int, raw_input().split()) for _ in range(n)]
INF = 10 ** 6
d = [INF for _ in range(1 << n)]
p = [None for _ in range(1 << n)]
z = [[-1 for i in range(n + 1)] for j in range(n + 1)]

def dist(i, j):
    if z[i + 1][j + 1] == -1:
        a = o if i < 0 else c[i]
        b = o if j < 0 else c[j]
        z[i + 1][j + 1] = (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2
    return z[i + 1][j + 1]

def upd(im, res, pr):
    if res < d[im]:
        p[im] = pr
        d[im] = res

def dp(msk):
    if d[msk] == INF:
        return
    idx = -1
    for i in range(n):
        if msk & (1 << i) == 0:
            im = msk | (1 << i)
            for j in range(i, n):
                if msk & (1 << j) == 0:
                    r = dist(i, j) + dist(i, -1) + dist(j, -1)
                    jm = im | (1 << j)
                    upd(jm, r + d[msk], msk)
            break

d[0] = 0
for i in range(1 << n):
    dp(i)
msk = (1 << n) - 1
print d[msk]

r = [0]
while p[msk]:
    m = msk & p[msk]
    for t in range(n):
        if (1 << t) & m:
            r.append(t + 1)
    r.append(0)
    msk = p[msk]
    r.append(0)
print ' '.join(str(t) for t in r)


x0, y0 = map(int, input().split())
n = int(input())
arr = [[x0, y0]]
for i in range(0, n):
    x, y = map(int, input().split())
    arr.append([x, y])
dist = [[0 for j in range(0, n+1)] for i in range(0, n+1)]
for i in range(0, n+1):
    for j in range(0, n+1):
        dist[i][j] = (arr[i][0] - arr[j][0])**2 + (arr[i][1] - arr[j][1])**2
# print(arr)
# print(dist)

def dfs(status, memo):
    # if status in memo:
    #     return memo[status][0]
    if memo[status] != None:
        return memo[status][0]
    if status <= 0:
        return 1e8
    res = 1e8
    prev = []
    for i in range(1, n+1):
        if ((status >> i) & 1) == 0:
            continue
        next = status - (1 << i)
        temp = dfs(next, memo) + dist[0][i]*2
        if temp < res:
            res = temp
            prev = [i, 0]
        for j in range(1, n+1):
            if j == i:
                continue
            if ((status >> j) & 1) == 0:
                continue
            next = status - (1 << i) - (1 << j)
            temp = dfs(next, memo) + dist[0][j] + dist[j][i] + dist[i][0]
            if temp < res:
                res = temp
                prev = [i, j, 0]
    memo[status] = [res, prev]
    print(status, res)
    return res

# memo = {1: [0, []]}
memo = [None for i in range(0, 1 << 25)]
memo[1] = [0, []]
start = 1
end = 1
for i in range(1, n + 1):
    end += (1 << i)
res = dfs(end, memo)
path = [0]
cur = end
while cur > 1:
    prev = memo[cur][1]
    # if len(prev) == 2:
    path.extend(prev)
    for i in range(len(prev) - 1):
        cur -= (1 << prev[i])

print(res)
print(' '.join(map(str, path)))


"""
tag: dp, bitmasks

http://codeforces.com/problemset/problem/8/C

solution:
for one already optimized selection a: 0101100101
calculate each 0 positions, e.g. 0101100111, meaning selecting the second
item based on the a status.
then select another one item, e.g. 0101101111, meaning selecting the
fourth item based on the a status.
and we need another array to keep the path from a to b
the solution is what kept in status a=1111111111
"""

import sys

ip = sys.stdin.readline
#ip = open('../input.txt').readline

# reading numbers of one line
def getnums():
    return map(int, ip().split())

# distance
def f(x, y):
    return x * x + y * y

# the original point
xo, yo = getnums()

# number of items
n = int(ip())

# distance from original point to each item
disto = [0] * n

# distance from each item to another item
disti = [[0] * n for i in xrange(n)]

# position of items
positions = []

# calculate distance between 2 items
for i in xrange(n):
    x, y = getnums()
    disto[i] = f(xo - x, yo - y)
    for j in xrange(i):
        disti[j][i] = f(positions[j][0] - x, positions[j][1] - y)
        disti[i][j] = disti[j][i]
    positions.append((x, y))

# optimized cost for each status
a = [None] * (1 << n)
# none of the items are selected, so the cost is 0
a[0] = 0
# parent status for each optimized status
p = [0] * (1 << n)

for i in xrange(1 << n):
    if a[i] is not None:
        for j in xrange(n):
            # meaning jth items was not selected in status i
            if i & (1 << j) == 0:

                # only take the item and return
                t = i + (1 << j)
                if not a[t] or a[t] > a[i] + disto[j] * 2:
                    a[t] = a[i] + disto[j] * 2
                    p[t] = i

                # if take another item
                for k in xrange(j + 1, n):
                    if t & (1 << k) == 0:
                        two = t + (1 << k)
                        c = a[i] + disto[j] + disti[j][k] + disto[k]
                        if not a[two] or a[two] > a[i] + c:
                            a[two] = a[i] + c
                            p[two] = i

z = (1 << n) - 1
print a[z]
r = [0]
while z > 0:
    t = z - p[z]
    while t:
        x = -t & t
        t -= x
        i = 0
        while x > 0:
            i += 1
            x /= 2
        r.append(i)
    z = p[z]
    r.append(0)

print ' '.join(map(str, r[::-1]))


read = lambda: map(int, input().split())
n, v, e = read()
adj = [[] for _ in range(n + 1)]
As = [0] + list(read())
Bs = [0] + list(read())
ans = []

for _ in range(e):
    a, b = map(int, input().split())
    adj[a].append(b)
    adj[b].append(a)

def flow(a, b, d):
    As[a] -= d
    As[b] += d
    ans.append((a, b, d));

def augment(path, e, d):
    if e:
        dd = min(d, As[path[e - 1]], v - As[path[e]])
        flow(path[e - 1], path[e], dd)
        augment(path, e - 1, d)
        if d > dd:
            flow(path[e - 1], path[e], d - dd);

def adjust(s):
    pre = [0] * (n + 1)
    pre[s] = -1
    stk = [s]
    e = 0
    while len(stk):
        p = stk[-1]
        del stk[-1]
        if As[p] < Bs[p]:
            e = p
            break
        for to in adj[p]:
            if not pre[to]:
                pre[to] = p
                stk.append(to)

    if not e:
        raise Exception
    path = []
    while e > 0:
        path.insert(0, e)
        e = pre[e]
    augment(path, len(path) - 1, min(Bs[path[-1]] - As[path[-1]], As[s] - Bs[s]))

try:
    while True:
        check = False
        for i in range(1, n + 1):
            if As[i] > Bs[i]:
                adjust(i)
                check = True
        if not check:
            break
    for i in range(1, n + 1):
        if As[i] != Bs[i]:
            raise Exception
    print(len(ans))
    for tp in ans:
        print(*tp)
except Exception:
    print("NO")


read = lambda: map(int, input().split())
n, v, e = read()
adj = [[]] * (n + 1)
As = [0] + list(read())
Bs = [0] + list(read())
ans = []

for _ in range(e):
    a, b = map(int, input().split())
    adj[a].append(b)
    adj[b].append(a)

def flow(a, b, d):
    As[a] -= d
    As[b] += d
    ans.append((a, b, d));

def augment(path, e, d):
    if e:
        dd = min(d, As[path[e - 1]], v - As[path[e]])
        flow(path[e - 1], path[e], dd)
        augment(path, e - 1, d)
        if d > dd:
            flow(path[e - 1], path[e], d - dd);

def adjust(s):
    pre = [0] * (n + 1)
    pre[s] = -1
    stk = [s]
    e = 0
    while len(stk):
        p = stk[-1]
        del stk[-1]
        if As[p] < Bs[p]:
            e = p
            break
        for to in adj[p]:
            if not pre[to]:
                pre[to] = p
                stk.append(to)

    if not e:
        raise Exception

    path = []
    while e > 0:
        path.insert(0, e)
        e = pre[e]

    augment(path, len(path) - 1, min(Bs[path[-1]] - As[path[-1]], As[s] - Bs[s]))

try:
    while True:
        check = False
        for i in range(1, n + 1):
            if As[i] > Bs[i]:
                adjust(i)
                check = True
        if not check:
            break
    for i in range(1, n + 1):
        if As[i] != Bs[i]:
            raise Exception
    print(len(ans))
    for tp in ans:
        print(*tp)
except Exception:
    print("NO")


n,d,S=map(int,raw_input().split())
c0=[]
c1=[]
c2=1
for i in range(n):
    c,f,l=map(int,raw_input().split())
    c2+=c
    if c>0 :
        c1.append([f,l])
    else :
        c0.append([f,l])

def solc0(N,T):
    c0.sort()
    res=0
    ct=0
    for a in c0:
        if a[1]>=d:
            if res+a[0]<=T and ct<N:
                res+=a[0]
                ct+=1
            else:break;
    return ct,res

def solc1():
    c1.sort()
    res=-1
    i=0
    for a in c1:
        i+=1
        if a[1]>=d:
            if a[0]<=S:
                res=a[0]
                c0.extend(c1[i:])
                break
            else: return 0,0
    if res<0 : return 0,0

    tc,tr=solc0(n-c2,S-res)
    ct=tc+c2
    res+=tr
    return ct,res

ct,res=solc0(n,S)
ct2,res2=solc1()
if ct<ct2 or (ct==ct2 and res>res2):
    ct,res=ct2,res2

print ct,res


n,d,S=map(int,raw_input().split())
c0=[]
c1=[]
c2=1
for i in range(n):
    c,f,l=map(int,raw_input().split())
    c2+=c
    if c>0 :
        c0.append([f,l])
    else :
        c1.append([f,l])

def solc0(N,T):
    c0.sort()
    res=0
    ct=0
    for a in c0:
        if a[1]>=d:
            if res+a[0]<=T and ct<N:
                res+=a[0]
                ct+=1
            else:break;
    return ct,res

def solc1():
    c1.sort()
    res=-1
    i=0
    for a in c1:
        i+=1
        if a[1]>=d:
            if a[0]<=S:
                res=a[0]
                c0.extend(c1[i:])
            else: return 0,0
    if res<0 : return 0,0

    tc,tr=solc0(n-c2,S-res)
    ct=tc+c2
    res+=tr
    return ct,res

ct,res=solc0(n,S)
ct2,res2=solc1()
if ct<ct2 or (ct==ct2 and res>res2):
    ct,res=ct2,res2

print ct,res


n,d,S=map(int,raw_input().split())
c0=[[0,0] for i in range(n)]
c1=[[0,0] for i in range(n)]
c2=1
for i in range(n):
    c,f,l=map(int,raw_input().split())
    c2+=c
    if c>0 : c1[i][0]=f;c1[i][1]=l;
    else : c0[i][0]=f;c0[i][1]=l;

def solc0(N,T):
    c0.sort()
    res=0
    ct=0
    for i in range(len(c0)):
        if c0[i][1]>=d:
            if res+c0[i][0]<=T and ct<N:
                res+=c0[i][0]
                ct+=1
            else:break;
    return ct,res

def solc1():
    c1.sort()
    res=-1
    for i in range(len(c1)):
        if c1[i][1]>=d:
            if c1[i][0]<=S:
                res=c1[i][0]
                c0.extend(c1[i+1:])
            else: return 0,0
    if res<0 : return 0,0

    tc,tr=solc0(n-c2,S-res)
    ct=tc+c2
    res+=tr
    return ct,res

ct,res=solc0(n,S)
ct2,res2=solc1()
if ct<ct2 or (ct==ct2 and res>res2):
    ct,res=ct2,res2

print ct,res


n,d,S=map(int,raw_input().split())
c0=[]
c1=[]
c2=1
for i in range(n):
    c,f,l=map(int,raw_input().split())
    c2+=c
    if c>0 :
        c1.append([f,l])
    else :
        c0.append([f,l])

def solc0(N,T):
    c0.sort()
    res=0
    ct=0
    for a in c0:
        if a[1]>=d:
            if res+a[0]<=T and ct<N:
                res+=a[0]
                ct+=1
            else:break;
    return ct,res

def solc1():
    c1.sort()
    res=-1
    i=0
    for a in c1:
        i+=1
        if a[1]>=d:
            if a[0]<=S:
                res=a[0]
                c0.extend(c1[i:])
            else: return 0,0
    if res<0 : return 0,0

    tc,tr=solc0(n-c2,S-res)
    ct=tc+c2
    res+=tr
    return ct,res

ct,res=solc0(n,S)
ct2,res2=solc1()
if ct<ct2 or (ct==ct2 and res>res2):
    ct,res=ct2,res2

print ct,res


n,d,S=map(int,raw_input().split())
c0=[[0,0] for i in range(n)]
c1=[[0,0] for i in range(n)]
c2=1
for i in range(n):
    c,f,l=map(int,raw_input().split())
    c2+=c
    if c>0 : c1[i][0]=f,c1[i][1]=l;
    else : c0[i][0]=f,c0[i][1]=l;

def solc0(N,T):
    c0.sort()
    res=0
    ct=0
    for i in range(len(c0)):
        if c0[i][1]>=d:
            if res+c0[i][0]<=T and ct<N:
                res+=c0[i][0]
                ct+=1
            else:break;
    return ct,res

def solc1():
    c1.sort()
    res=-1
    for i in range(len(c1)):
        if c1[i][1]>=d:
            if c1[i][0]<=S:
                res=c1[i][0]
                c0.extend(c1[i+1:])
            else: return 0,0
    if res<0 : return 0,0

    tc,tr=solc0(n-c2,S-res)
    ct=tc+c2
    res+=tr
    return ct,res

ct,res=solc0(n,S)
ct2,res2=solc1()
if ct<ct2 or (ct==ct2 and res>res2):
    ct,res=ct2,res2

print ct,res


import sys

def solve():
    n = int(input())
    if n == 1: return 0
    res = 1000000
    for other in range(1, n):
        pair = [n, other]
        temp = 0
        while pair[0] > 1 or pair[1] > 1:
            if pair[0] > pair[1]: pair[0], pair[1] = pair[1], pair[0]
            pair[1] -= pair[0]
            temp+=1
        res = min(res, temp)
    return res

if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
print(solve())


def solve(N, M):
    if M == 0: return 1000000
    if M == 1: return N - 1
    d, r = divmod(N, M)
    return d + solve(M, r)

N = int(input())
ans = 1000000
for M in range(1, N + 1):
    ans = min([ans, solve(N, M)])
print(ans)


from fractions import gcd

N = input()
H = dict()

def number (n,b):
    if not H.has_key((n,b)):
        if n == 1 and b == 1:       H[n,b] = 0
        elif n>b and b>0:           H[n,b] = (n-1)/b + number ((n-1)%b+1,b)
        elif b>n and n>0:           H[n,b] = (b-1)/n + number (n,(b-1)%n+1)
        else:                       H[n,b] = 10000000
    return H[n,b]

m = min (number(N,b) for b in range (N+1))
print m
#print [b for b in range (N+1) if number(N,b)==m]
# for b in range (N+1): print number(N,b),b,gcd(N,b)


from fractions import gcd

n = input()
H = dict()
inf = 10000000

# m represents the minimum computed so far
def number (n,b,m):
    if not H.has_key((n,b)):
        if b == 1:                  H[n,b] = n-1
        elif n>b and b>0 and n/b<m: H[n,b] = n/b + number (b,n%b,m-n/b)
        else:                       H[n,b] = inf
    return H[n,b]

m = inf
for b in range(n+1):
    m = min (number(n,b,m),m)
print m


import math as m

if(n==1 or n==2):
    print(n-1)
    exit()

def hcf(a,b):
    while(a!=b):
        if(a>b): a=a-b
        else: b = b-a
    return a

n = int(input())
mnm = m.inf

for k in range(m.ceil(n/2),n):
    if(hcf(n,k)!=1): continue

    p =k
    q = n-k
    count = 2
    while(p!=2):
        diff = p-q
        if(q>diff):
            p=q
            q=diff

        else:
            p=diff

        count+=1

    if(count<mnm):
        mnm = count

print(mnm)


n = int(input())
colour = dict(zip('RGBYW', range(5, 10)))
cards = [2 ** colour[c] + 2 ** (ord(v) - ord('1')) for c, v in input().split()]
ans = 10

for bit in range(2 ** 10):
    ok = True
    for i in range(n - 1):
        for j in range(i + 1, n):
            if cards[i] & cards[j] == 0:
                if (cards[i] | cards[j]) & bit == 0:
                    ok = False
                    break
            elif cards[i] != cards[j]:
                if (cards[i] ^ cards[j]) & bit == 0:
                    ok = False
                    break
        if not ok:
            break
    if ok:
        ans = min(bin(bit).count('1'), ans)

print(ans)


p = [1]
for i in range(1,11):
    p.append(p[i-1]*2)
hr = '12345RGBYW'
gt = {'1': 0,'2': 1,'3': 2,'4': 3,'5': 4,'R': 5,'G': 6,'B': 7,'Y': 8,'W': 9}

def check(c,mask):
    tmp = gt[c]
    if (mask // p[tmp]) % 2 == 1:
        return True
    return False

def count(mask):
    tmp = 0
    while mask>0:
        tmp+=mask % 2
        mask//=2
    return tmp

n = int(input())
a = list(input().split())
ans = 10
for mask in range(1024):
    f = True
    for i in range(n):
        for j in range(n):
            if a[i] == a[j]:
                pass
            elif (a[i][1] != a[j][1]) and (check(a[i][1],mask) or check(a[j][1],mask)):
                pass
            elif (a[i][0] != a[j][0]) and (check(a[i][0],mask) or check(a[j][0],mask)):
                pass
            else:
                f = False
    if f:
        ans = min(ans,count(mask))

print(ans)


class card:
    def __init__(self, one, two):
        self.c = one
        self.t = two

inp = []
chars = {'R': 0, 'G': 1, 'B': 2, 'Y': 3, 'W': 4}
n = int(raw_input())
spl = raw_input().split(' ')
for s in spl:
    inp.append(card(chars[s[0]], int(s[1]) - 1))

def check(mask):
    for i in xrange(n):
        for j in xrange(i + 1, n):
            c1 = inp[i]
            c2 = inp[j]
            flag = False
            if c1.c == c2.c and c1.t == c2.t:
                flag = True
            if c1.c != c2.c and ((mask & (1 << c1.c)) != 0 or (mask & (1 << c2.c)) != 0):
                flag = True
            if c1.t != c2.t and ((mask & (1 << (c1.t + 5))) != 0 or (mask & (1 << (c2.t + 5))) != 0):
                flag = True
            if not flag:
                return False
    return True

def count_bits(mask):
    out = 0
    while mask > 0:
        if mask & 1 == 1:
            out += 1
        mask >>= 1
    return out

out = 10
end = 2 ** 10
for m in xrange(end):
    if check(m):
        #print m, bin(m), count_bits(m)
        out = min(out, count_bits(m))

print out


n = int(input())
colour = dict(zip('RGBYW', range(5, 10)))
cards = list({2 ** colour[c] + 2 ** (ord(v) - ord('1')) for c, v in input().split()})
ans = 10

for bit in range(2 ** 10):
    ok = True
    for i in range(n - 1):
        for j in range(i + 1, n):
            if cards[i] & cards[j] == 0:
                if (cards[i] | cards[j]) & bit == 0:
                    ok = False
                    break
            elif cards[i] != cards[j]:
                if (cards[i] ^ cards[j]) & bit == 0:
                    ok = False
                    break
        if not ok:
            break
    if ok:
        ans = min(bin(bit).count('1'), ans)

print(ans)


user_input=int(input())
cards=input()
cards_array=cards.split(' ')
colours=['R','G','B','Y','W']
values=['1','2','3','4','5']

card_set=set(cards_array)
if len(card_set)==1:
    print ("0")
else:
    colour_array=[]
    value_array=[]

    for card in card_set:
        colour_array.extend([card[0]])
        value_array.extend([card[1]])

    hint_list=[]
    hints=0

    known=0

    colour_hint=0
    value_hint=0

    for the_colour in colours:
        if colour_array.count(the_colour) == len(card_set):
            for i in range(len(card_set)):
                colour_array[i]='known'
            hint_list.extend(the_colour)
            colour_hint+=1

        elif colour_array.count(the_colour) == 0:
            hint_list.extend(the_colour)
            colour_hint+=1

    for the_value in values:
        if value_array.count(the_value) == len(card_set):
            for i in range(len(card_set)):
                value_array[i] = 'known'
            hint_list.extend(the_value)
            value_hint+=1

        elif value_array.count(the_value)==0:
            hint_list.extend(the_value)
            value_hint+=1

    while known<(len(card_set)):
        count=0
        for the_value in values:
            if the_value in hint_list:
                pass
            else:
                if value_array.count(the_value)>count:
                    index=values.index(the_value)
                    count=value_array.count(the_value)

        if count!=0:
            hints+=1
            value_hint+=1
            hint_list.extend(values[index])
            another_count=value_array.count(values[index])
            for i in range(value_array.count(values[index])):
                    an_index=value_array.index(values[index])
                    value_array[an_index]='known'

                    if colour_array[index] == 'known':
                        known += 1

                    if colour_array.count(colour_array[index]) == 1:
                        colour_array[index] = 'known'
                        known+=1

                    if another_count > 1:
                        index2 = value_array.index(values[index])

                        if colour_array.count(colour_array[index2]) == 1:
                            colour_array[index2] = 'known'
                            known+=1

                        another_count -= 2

        if value_hint==4:
            for the_value in values:
                if the_value not in hint_list:
                    another_count = value_array.count(the_value)
                    for i in range(value_array.count(the_value)):

                        index=value_array.index(the_value)
                        value_array[index]='known'

                        if colour_array[index]=='known':
                            known+=1

                        if colour_array.count(colour_array[index]) == 1:
                            colour_array[index] = 'known'
                            known+=1

                        if another_count > 1:
                            index2=value_array.index(the_value)

                            if colour_array.count(colour_array[index2])==1:
                                colour_array[index2]='known'
                                known+=1

                            another_count-=2

            value_hint+=1

        count=0

        for the_colour in colours:
            if the_colour in hint_list:
                pass
            else:
                if colour_array.count(the_colour)>count:
                    count=colour_array.count(the_colour)
                    index=colours.index(the_colour)

        if count!=0:
                hints+=1
                colour_hint+=1
                hint_list.extend(colours[index])
                another_count = colour_array.count(colours[index])
                for i in range(another_count):
                    index = colour_array.index(colours[index])
                    colour_array[index] = 'known'

                    if value_array[index] == 'known':
                        known += 1

                    if value_array.count(value_array[index]) == 1:
                        value_array[index] = 'known'
                        known+=1

                    if another_count > 1:

                        index2 = colour_array.index(colours[index])

                        if value_array.count(value_array[index2]) == 1:
                            value_array[index2] = 'known'
                            known+=1

                        another_count -= 2

        if colour_hint==4:
            for the_colour in colours:
                if the_colour not in hint_list:
                    another_count=colour_array.count(the_colour)
                    for i in range(another_count):
                        index=colour_array.index(the_colour)
                        colour_array[index]='known'

                        if value_array[index]=='known':
                            known+=1

                        if value_array.count(value_array[index]) == 1:
                            value_array[index] = 'known'

                            known+=1

                            if another_count > 1:
                                index2 = colour_array.index(the_colour)

                                if value_array.count(value_array[index2]) == 1:
                                    value_array[index] = 'known'
                                    known+=1

                                another_count -= 2

            colour_hint+=1

    print (hints)


# Legends Always Come Up with Solution
# Author: Manvir Singh

import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from bisect import *
from math import sqrt, pi, ceil, log, inf,gcd
from itertools import permutations
from copy import deepcopy
from heapq import *

def main():
    n=int(input())
    a=[0]*(101)
    b=list(map(int,input().split()))
    for i in b:
        a[i]+=1
    ma=max(a)
    if a.count(ma)>=2:
        print(n)
    else:
        ele=a.index(ma)
        maa=0
        for i in range(1,101):
            if i!=ele:
                c=Counter()
                su=0
                for j in range(n):
                    if b[j]==i:
                        su-=1
                    elif b[j]==ele:
                        su+=1
                    if c[su]==0:
                        c[su]=j+1
                    else:
                        maa=max(maa,j+1-c[su])
                    if su==0:
                        maa=max(maa,j+1)
        print(maa)

# region fastio
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")

if __name__ == "__main__":
    main()


import sys
from collections import defaultdict, Counter
import sys
import os
from io import BytesIO, IOBase

#Fast IO Region
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")

N = int(input())
arr = list(map(int, input().split()))
freq = Counter(arr)
ans = 0

fix = freq.most_common(1)[0][0]

for other in freq:
    if other == fix:
        continue
    pref, last_pos = [0] * (N+2), {0:0}
    for i in range(N):
        now = 1 if arr[i] == other else 0
        pref[i+1] = pref[i] + (-1 if arr[i] == fix else now)
        last_pos.setdefault(pref[i+1], i+1)

        ans = max(ans, i+1 - last_pos[pref[i+1]])

print(ans)


from collections import Counter
n = int(input())
A = list(map(int, input().split()))
cnt = Counter(A)
maxv = max(cnt.values())
x = [c for c in cnt if cnt[c] == maxv]
if len(x) > 1:
    print(n)
else:
    x = x[0]
    ans = 0
    for c in cnt:
        if c == x: continue
        dic = {0: -1}
        cur = 0
        tmp = 0
        for i, a in enumerate(A):
            cur += 1 if a == c else -1 if a == x else 0
            if cur in dic:
                tmp = max(tmp, i - dic[cur])
            dic.setdefault(cur, i)
        ans = max(ans, tmp)
    print(ans)