CodeEff/ECCO huggingface dataset:
# Original solution
from collections import defaultdict
n = int(eval(input()))
A = [int(x) for x in input().split()]
even = A[0::2]
odd = A[1::2]
EC = defaultdict(int)
EC[0] = 0
OC = defaultdict(int)
OC[0] = 0
for e in even:
  EC[e] += 1
for o in odd:
  OC[o] += 1
E = sorted([[v, k] for k, v in list(EC.items())], reverse=True)
O = sorted([[v, k] for k, v in list(OC.items())], reverse=True)
if E[0][1] != O[0][1]:
  print((n - E[0][0] - O[0][0]))
elif E[1][0] >= O[1][0]:
  print((n - E[1][0] - O[0][0]))
else:
  print((n - E[0][0] - O[1][0]))

#sample code.

# Optimized solution
n=int(eval(input()))
v=list(map(int,input().split()))
a=v[0::2]
b=v[1::2]
c=dict()
d=dict()

for i in a:
  if i in c:
    c[i]+=1
  else:
    c[i]=1
for j in b:
  if j in d:
    d[j]+=1
  else:
    d[j]=1

e=sorted([[w,v] for v,w in list(c.items())],reverse=True)
f=sorted([[w,v] for v,w in list(d.items())],reverse=True)

if len(set(a))==len(set(b))==1 and e[0][1]==f[0][1] and e[0][0]==f[0][0]:
  print((min(len(a),len(b))))
elif e[0][1]!=f[0][1]:
  print((n-e[0][0]-f[0][0]))
elif e[0][0]>f[0][0]:
  print((n-e[0][0]-f[1][0]))
elif e[0][0]<f[0][0]:
  print((n-e[1][0]-f[0][0]))
elif e[0][0]==f[0][0] and e[0][1]==f[0][1]:
  if e[1][0]>f[1][0]:
    print((n-e[1][0]-f[0][0]))
  else:
    print((n-e[0][0]-f[1][0]))
# Original solution
n = int(eval(input()))
v = list(input().split())
from collections import Counter
x, y = list(Counter(v[::2]).items()), list(Counter(v[1::2]).items())
x.sort(key=lambda i:i[1], reverse=True)
y.sort(key=lambda i:i[1], reverse=True)
r = []
for a, b in x[:max(len(x), 3)]:
    for aa, bb in y[:max(len(y), 3)]:
        if a == aa:
            r.append(n-max(b, bb))
        else:
            r.append(n-b-bb)
print((min(r)))

# Optimized solution
n = int(eval(input()))
v = list(input().split())
from collections import Counter
x, y = list(Counter(v[::2]).most_common(3)), list(Counter(v[1::2]).most_common(3))
x.sort(key=lambda i:i[1], reverse=True)
y.sort(key=lambda i:i[1], reverse=True)
r = []
for a, b in x[:max(len(x), 3)]:
    for aa, bb in y[:max(len(y), 3)]:
        if a == aa:
            r.append(n-max(b, bb))
        else:
            r.append(n-b-bb)
print((min(r)))
# Original solution
n = int(eval(input()))
l = list(map(int, input().split()))

if n == 2:
    if l[0] == l[1]:
        print((1))
    else:
        print((0))
else:
    a = []
    b = []
    for i in range(n):
        if i % 2 == 0:
            a.append(l[i])
        else:
            b.append(l[i])

    da = {}
    for e in a:
        if e not in da:
            da[e] = (1,e)
        else:
            da[e] = (da[e][0]+1,da[e][1])

    db = {}
    for e in b:
        if e not in db:
            db[e] = (1,e)
        else:
            db[e] = (db[e][0]+1,db[e][1])

    da[-1] = (0,-1)
    db[-1] = (0,-1)

    la = list(reversed(sorted(list(da.values()))))
    lb = list(reversed(sorted(list(db.values()))))
    #print(la)
    #print(lb)



    if la[0][1] == lb[0][1]:
        ans = n- (la[0][0] + max(la[1][0],lb[1][0]))
    else:
        ans = n - (la[0][0] + lb[0][0])
    print(ans)


# Optimized solution
n = int(eval(input()))
l = list(map(int, input().split()))

a = []
b = []

for i in range(n):
    if i % 2 == 0:
        a.append(l[i])
    else:
        b.append(l[i])

def createMyList(li):
    d = {-1:(0,-1)}
    for e in li:
        if e not in d:
            d[e] = (1,e)
        else:
            d[e] = (d[e][0] + 1, d[e][1])
    return list(reversed(sorted(list(d.values()))))

la = createMyList(a)
lb = createMyList(b)
#print(la)
#print(lb)

if la[0][1] == lb[0][1]:
    ans = n- max(lb[0][0] + la[1][0], la[0][0] +lb[1][0])
else:
    ans = n - (la[0][0] + lb[0][0])
print(ans)
# Original solution
N=int(eval(input()))
v=[int(i) for i in input().split()]
if N==2:
    if v[0]==v[1]:
        print((1))
        exit()
    else:
        print((0))
        exit()
A=dict()
B=dict()
L=0
R=0
def inc(D,k):
    if k in D:
        D[k]+=1
    else:
        D[k]=1
for i in range(N):
    if i%2==0:
        L+=1
        inc(A,v[i])
    else:
        R+=1
        inc(B,v[i])
X=[(L,-1)]
Y=[(R,-2)]
for i in A:
    X.append((L-A[i],i))
for i in B:
    Y.append((R-B[i],i))
X.sort(reverse=True)
Y.sort(reverse=True)
while(True):
    if X[-1][1]!=Y[-1][1]:
        print((X[-1][0]+Y[-1][0]))
        exit()
    else:
        vl=X[-1][0]+Y[-2][0]
        vr=X[-2][0]+Y[-1][0]
        if vl<vr:
            Y.pop()
            R-=1
        else:
            X.pop()
            L-=1

    

# Optimized solution
N=int(eval(input()))
v=[int(i) for i in input().split()]
if N==2:
    if v[0]==v[1]:
        print((1))
        exit()
    else:
        print((0))
        exit()
A=dict()
B=dict()
L=0
R=0
def inc(D,k):
    if k in D:
        D[k]+=1
    else:
        D[k]=1
for i in range(N):
    if i%2==0:
        L+=1
        inc(A,v[i])
    else:
        R+=1
        inc(B,v[i])
X=[(L,-1)]
Y=[(R,-2)]
for i in A:
    X.append((L-A[i],i))
for i in B:
    Y.append((R-B[i],i))
X.sort(reverse=True)
Y.sort(reverse=True)
if X[-1][1]!=Y[-1][1]:
    print((X[-1][0]+Y[-1][0]))
else:
    vl=X[-1][0]+Y[-2][0]
    vr=X[-2][0]+Y[-1][0]
    print((min(vl,vr)))
# Original solution
from collections import Counter
n=int(eval(input()))
V=list(map(int,input().split()))
A=V[::2]
B=V[1::2]
a=Counter(A)
b=Counter(B)
x=a.most_common(2)
y=b.most_common(2)
if x[0][0]!=y[0][0]:
  print(((n//2-x[0][1])+(n//2-y[0][1])))
else:
  if len(x)>1 and len(y)>1:
    if x[0][1]>y[0][1]:
      print(((n//2-x[0][1])+(n//2-y[1][1])))
    elif x[0][1]==y[0][1]:
      if x[1][1]>y[1][1]:
        print(((n//2-x[1][1])+(n//2-y[0][1])))
      else:
        print(((n//2-x[0][1])+(n//2-y[1][1])))
    else:
      print(((n//2-x[1][1])+(n//2-y[0][1])))
  elif len(x)>1:
    print((n//2-x[1][1]))
  elif len(y)>1:
    print((n//2-y[1][1]))
  else:
    print((n//2))

# Optimized solution
from collections import Counter
n=int(eval(input()))
V=list(map(int,input().split()))
A=V[::2]
B=V[1::2]
a=Counter(A)
b=Counter(B)
x=a.most_common(2)
y=b.most_common(2)
if x[0][0]!=y[0][0]:
  print(((n//2-x[0][1])+(n//2-y[0][1])))
else:
  if len(x)>1 and len(y)>1:
    s=(n//2-x[0][1])+(n//2-y[1][1])
    t=(n//2-x[1][1])+(n//2-y[0][1])
    print((min(s,t)))
  elif len(x)>1:
    print((n//2-x[1][1]))
  elif len(y)>1:
    print((n//2-y[1][1]))
  else:
    print((n//2))
# Original solution
# -*- coding: utf-8 -*-
n = int(eval(input()))
vv = list(map(int, input().split()))
d1 = {}
d2 = {}

for i in range(n):
    v = vv[i]
    if i%2==0:
        if v not in d2:
            d2[v] = 0
        d2[v] += 1
    else:
        if v not in d1:
            d1[v] = 0
        d1[v] += 1

res = 10**7
for v1 in list(d1.keys()):
    for v2 in list(d2.keys()):
        if v1==v2:
            continue
        tmp = 0
        for k in list(d1.keys()):
            if k!=v1:
                tmp += d1[k]
        for k in list(d2.keys()):
            if k!=v2:
                tmp += d2[k]
        res = min(res, tmp)
if res==10**7:
    print((n//2))
else:
    print(res)


# Optimized solution
# -*- coding: utf-8 -*-
n = int(eval(input()))
vv = list(map(int, input().split()))
d1 = {}
d2 = {}

for i in range(n):
    v = vv[i]
    if i%2==0:
        if v not in d2:
            d2[v] = 0
        d2[v] += 1
    else:
        if v not in d1:
            d1[v] = 0
        d1[v] += 1

dl1 = sorted(list(d1.keys()), reverse=True, key=lambda k:d1[k])
dl2 = sorted(list(d2.keys()), reverse=True, key=lambda k:d2[k])
i1 = 0
i2 = 0

if len(dl1)==1 and len(dl2)==1:
    if dl1[0]==dl2[0]:
        print((n//2))
    else:
        print((0))
    exit()
elif len(dl1)==1:
    print((sum(sorted(list(d2.values()), reverse=True)[1:])))
    exit()
elif len(dl2)==1:
    print((sum(sorted(list(d1.values()), reverse=True)[1:])))
    exit()


if dl1[0]!=dl2[0]:
    res = 0
    res += sum(sorted(list(d1.values()), reverse=True)[1:])
    res += sum(sorted(list(d2.values()), reverse=True)[1:])
    print(res)
else:
    res1 = 0
    l = sorted(list(d1.values()), reverse=True)
    res1 += l[0]
    if len(l)>2:
        res1 += sum(l[2:])
    res1 += sum(sorted(list(d2.values()), reverse=True)[1:])

    res2 = 0
    l = sorted(list(d2.values()), reverse=True)
    res2 += l[0]
    if len(l)>2:
        res2 += sum(l[2:])
    res2 += sum(sorted(list(d1.values()), reverse=True)[1:])
    print((min(res1,res2)))
# Original solution
from collections import Counter
N = int(eval(input()))
A = list(map(int, input().split()))
c1 = Counter(A[::2])
c2 = Counter(A[1::2])
c1[0] = 0
c2[0] = 0
c1m = max(list(c1.items()), key=lambda x:x[1])
c2m = max(list(c2.items()), key=lambda x:x[1])
c1c2 = max(c for k,c in list(c2.items()) if c1m[0]!=k)
c2c1 = max(c for k,c in list(c1.items()) if c2m[0]!=k)
print((min(N - c1m[1] - c1c2, N - c2m[1] - c2c1)))

# Optimized solution
from collections import Counter
n = int(eval(input()))
V = list(map(int, input().split()))
c1 = Counter(V[::2])
c2 = Counter(V[1::2])
c1[0] = 0
c2[0] = 0
(x, cx), (_, cxx) = c1.most_common(2)
(y, cy), (_, cyy) = c2.most_common(2)
if x == y:
    print((n - max(cx + cyy, cy + cxx)))
else:
    print((n - cx - cy))
# Original solution
maxn=10**5+10
n=int(eval(input()))
a=[None]*(n+10)
b=[0]*maxn
c=[0]*maxn
a[1:n+1]=input().split()

for i in range(1,n+1):
    a[i]=int(a[i])
    if i%2==0:
        b[a[i]]+=1
    else:
        c[a[i]]+=1

max1=max2=maxp=0
for i in range(1,maxn):
    if b[i]>=max1:
        max2=max1
        maxp=i
        max1=b[i]
    elif b[i]>=max2:
        max2=b[i]

ans=n
for i in range(1,maxn):
    res=((n+1)//2-c[i])
    if i==maxp:
        res+=n//2-max2
    else:
        res+=n//2-max1
    ans=min(ans,res)

print(ans)

# Optimized solution
maxn=10**5+10
n=int(eval(input()))
b=[0]*maxn
c=[0]*maxn
a=input().split()

for i in range(0,n):
    a[i]=int(a[i])
    if i%2==0:
        b[a[i]]+=1
    else:
        c[a[i]]+=1

max1=max(b)
maxp=b.index(max1)
del b[maxp]
max2=max(b)

ans=0
for i in range(0,maxn):
    ans=max(ans,c[i]+(max2 if i==maxp else max1))

print((n-ans))
# Original solution
n = int(eval(input()))
nums = list(map(int, input().split()))

nums1 = nums[0::2]
nums2 = nums[1::2]

bug1 = [0] * 1000000
bug2 = [0] * 1000000

for num in nums1:
    bug1[num] += 1

for num in nums2:
    bug2[num] += 1

max_v_1 = max(bug1)
max_i_1 = bug1.index(max_v_1)
max_v_2 = max(bug2)
max_i_2 = bug2.index(max_v_2)

if max_i_1 == max_i_2:
    bug1[max_i_1] = 0
    bug2[max_i_2] = 0
    sec_max_v_1 = max(bug1)
    sec_max_v_2 = max(bug2)
    print((n - min(max_v_1, max_v_2) - max(sec_max_v_1, sec_max_v_2)))
else:
    print((n - max_v_1 - max_v_2))

# Optimized solution
n = int(eval(input()))
nums = list(map(int, input().split()))

nums1 = nums[0::2]
nums2 = nums[1::2]

bug1 = [0] * 100001
bug2 = [0] * 100001

for num in nums1:
    bug1[num] += 1

for num in nums2:
    bug2[num] += 1

max_v_1 = max(bug1)
max_i_1 = bug1.index(max_v_1)
max_v_2 = max(bug2)
max_i_2 = bug2.index(max_v_2)

if max_i_1 == max_i_2:
    bug1[max_i_1] = 0
    bug2[max_i_2] = 0
    sec_max_v_1 = max(bug1)
    sec_max_v_2 = max(bug2)
    print((n - min(max_v_1, max_v_2) - max(sec_max_v_1, sec_max_v_2)))
else:
    print((n - max_v_1 - max_v_2))
# Original solution
n=int(eval(input()))
v=list(map(int,input().split()))
a=[[0,i]for i in range(10**5+1)]
b=[[0,i]for i in range(10**5+1)]
for t in sorted(v[0::2]):a[t][0]+=1
for t in sorted(v[1::2]):b[t][0]+=1
c=sorted(a)[::-1]
d=sorted(b)[::-1]
print((n-c[0][0]-[max(c[1][0],d[1][0]),d[0][0]][c[0][1]!=d[0][1]]))

# Optimized solution
n=int(eval(input()))
*v,=list(map(int,input().split()))
a=[[0,i]for i in range(10**5+1)]
b=[[0,i]for i in range(10**5+1)]
for t,u in zip(sorted(v[::2]),sorted(v[1::2])):a[t][0]+=1;b[u][0]+=1
a.sort(reverse=1)
b.sort(reverse=1)
print((n-a[0][0]-[max(a[1][0],b[1][0]),b[0][0]][a[0][1]!=b[0][1]]))
# Original solution
n=int(eval(input()))
v=list(map(int,input().split()))
w=sorted(a for a in v[0::2])
x=sorted(a for a in v[1::2])
a=[[0,i]for i in range(10**5+1)]
b=[[0,i]for i in range(10**5+1)]
for t in w:
    a[t][0]+=1
for t in x:
    b[t][0]+=1
c=sorted(a)
d=sorted(b)
e=c[len(c)-1][0]
i=c[len(c)-1][1]
f=d[len(d)-1][0]
g=c[len(c)-1][0]
h=d[len(d)-1][0]
for t in range(len(c)):
    if i!=d[len(d)-1][1]:break
    e=c[len(c)-t-1][0]
    i=c[len(c)-t-1][1]
i=d[len(d)-1][1]
for t in range(len(d)):
    if i!=c[len(c)-1][1]:break
    g=d[len(d)-t-1][0]
    i=d[len(d)-t-1][1]
print((min(n-e-f,n-g-h)))

# Optimized solution
from collections import*
n,*v=list(map(int,open(0).read().split()))
a=Counter(v[::2]).most_common()
b=Counter(v[1::2]).most_common()
if a[0][0]==b[0][0]:
    if len(a)>1and len(b)>1:
        print((min(n-a[0][1]-b[1][1],n-a[1][1]-b[0][1])))
    elif len(a)>1:
        print((n-a[1][1]-b[0][1]))
    elif len(b)>1:
        print((n-a[0][1]-b[1][1]))
    else:
        print((n//2))
else:
    print((n-a[0][1]-b[0][1]))
# Original solution
from collections import Counter as C
n = int(eval(input()))
v = [int(a) for a in input().split()]

k1 = C(v[::2]).most_common()[0][0]
v1 = C(v[::2]).most_common()[0][1]

if v1 == n/2:
    k2 = 0
    v2 = 0
else:
    k2 = C(v[::2]).most_common()[1][0]
    v2 = C(v[::2]).most_common()[1][1]

k3 = C(v[1::2]).most_common()[0][0]
v3 = C(v[1::2]).most_common()[0][1]

if v3 == n/2:
    k4 = 0
    v4 = 0
else:
    k4 = C(v[1::2]).most_common()[1][0]
    v4 = C(v[1::2]).most_common()[1][1]

if k1 != k3:
    print((n - v1 - v3))
else:
    print((min(n - v1 - v4, n - v2 - v3)))

# Optimized solution
from collections import Counter as C
n = int(eval(input()))
v = [int(a) for a in input().split()]

a=C(v[::2])
b=C(v[1::2])
a[0]=b[0]=0
a=a.most_common()
b=b.most_common()
if a[0][0] != b[0][0]:
    print((n-a[0][1]-b[0][1]))
else:
    print((min(n-a[1][1]-b[0][1],n-a[0][1]-b[1][1])))
# Original solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


def mat_mul_opt(_mat_info):
    r, c = list(map(int, _mat_info[0]))
    rc_info.extend([r, c])
    for each in _mat_info[1:]:
        rc_info.append(int(each[1]))

    for l in range(2, mat_num + 1):
        for i in range(1, mat_num - l + 2):
            j = i + l - 1
            ans[i][j] = 1e7
            for k in range(i, j):
                ans[i][j] = min(ans[i][j], ans[i][k] + ans[k + 1][j] +
                                rc_info[i - 1] * rc_info[k] * rc_info[j])

    return ans[1][mat_num]


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    mat_num = int(_input[0])
    mat_info = tuple([x.split() for x in _input[1:]])

    rc_info = []
    ans = [[0] * (mat_num + 1) for _ in range(mat_num + 1)]
    print((mat_mul_opt(mat_info)))

# Optimized solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


def mat_mul_opt(_mat_info):
    rc_info.extend(list(map(int, _mat_info[0])))
    rc_info.extend(list([int(x[1]) for x in _mat_info[1:]]))

    for l in range(2, mat_num + 1):
        for i in range(1, mat_num - l + 2):
            j = i + l - 1
            ans[i][j] = 1e7
            for k in range(i, j):
                ans[i][j] = min(ans[i][j], ans[i][k] + ans[k + 1][j] +
                                rc_info[i - 1] * rc_info[k] * rc_info[j])

    return ans[1][mat_num]


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    mat_num = int(_input[0])
    mat_info = tuple([x.split() for x in _input[1:]])

    rc_info = []
    ans = [[0] * (mat_num + 1) for _ in range(mat_num + 1)]
    print((mat_mul_opt(mat_info)))
# Original solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


def mat_mul_opt(_mat_info):
    rc_info = list(map(int, _mat_info[0]))
    rc_info.extend(list([int(x[1]) for x in _mat_info[1:]]))

    for l in range(2, mat_num + 1):
        for i in range(1, mat_num - l + 2):
            j = i + l - 1
            ans[i][j] = 1e7
            for k in range(i, j):
                ans[i][j] = min(ans[i][j], ans[i][k] + ans[k + 1][j] +
                                rc_info[i - 1] * rc_info[k] * rc_info[j])

    return ans[1][mat_num]


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    mat_num = int(_input[0])
    mat_info = tuple([x.split() for x in _input[1:]])
    # assert len(mat_info) == mat_num

    ans = [[0] * (mat_num + 1) for _ in range(mat_num + 1)]
    print((mat_mul_opt(mat_info)))

# Optimized solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


def mat_mul_opt(_mat_info):
    rc_info = list(map(int, _mat_info[0]))
    rc_info.extend(list([int(x[1]) for x in _mat_info[1:]]))

    ans = [[0] * (mat_num + 1) for _ in range(mat_num + 1)]

    for l in range(2, mat_num + 1):
        for i in range(1, mat_num - l + 2):
            j = i + l - 1
            ans[i][j] = 1e7
            for k in range(i, j):
                ans[i][j] = min(ans[i][j], ans[i][k] + ans[k + 1][j] +
                                rc_info[i - 1] * rc_info[k] * rc_info[j])

    return ans[1][mat_num]


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    mat_num = int(_input[0])
    mat_info = tuple([x.split() for x in _input[1:]])
    # assert len(mat_info) == mat_num

    print((mat_mul_opt(mat_info)))
# Original solution
def s():
 n=int(eval(input()))+1
 e=[input().split()for _  in[0]*~-n]
 p=[int(e[0][0])]+list(int(x[1])for x in e)
 m=[[0]*n for _ in[0]*n]
 for l in range(2,n):
  for i in range(1,n-l+1):
   j=i+l-1;m[i][j]=1e6
   for k in range(i,j):m[i][j]=min(m[i][j],m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j])
 print((m[1][n-1]))
if'__main__'==__name__:s()


# Optimized solution
def s():
 n=int(eval(input()))+1
 e=[input().split()for _  in[0]*~-n]
 p=[int(e[0][0])]+list(int(x[1])for x in e)
 m=[[0]*n for _ in[0]*n]
 for i in range(n):
  for r in range(n-i-1):
   c=r+i+1
   for j in range(r+1,c):
    x=m[r][j]+m[j][c]+p[r]*p[j]*p[c]
    if 1>m[r][c]or m[r][c]>x:m[r][c]=x
 print((m[r][c]))
if'__main__'==__name__:s()
# Original solution
import sys
def s():
 n=int(eval(input()))+1
 e=list([x.split() for x in sys.stdin])
 p=[int(e[0][0])]+list(int(x[1])for x in e)
 m=[[0]*n for _ in[0]*n]
 for i in range(n):
  for r in range(n-i-1):
   c=r+i+1
   for j in range(r+1,c):
    x=m[r][j]+m[j][c]+p[r]*p[j]*p[c]
    if 1>m[r][c]or m[r][c]>x:m[r][c]=x
 print((m[r][c]))
if'__main__'==__name__:s()


# Optimized solution
def s():
 n=int(eval(input()))+1
 e=[input().split()for _  in[0]*~-n]
 p=[int(e[0][0])]+list(int(x[1])for x in e)
 m=[[0]*n for _ in[0]*n]
 for i in range(n):
  for r in range(n-i-1):
   c=r+i+1
   for j in range(r+1,c):
    x=m[r][j]+m[j][c]+p[r]*p[j]*p[c]
    if 1>m[r][c]or m[r][c]>x:m[r][c]=x
 print((m[r][c]))
if'__main__'==__name__:s()
# Original solution
def s():
 n=int(eval(input()))+1
 e=[input().split()for _  in[0]*~-n]
 p=[int(e[0][0])]+list(int(x[1])for x in e)
 m=[[0]*n for _ in[0]*n]
 for i in range(n):
  for r in range(n-i-1):
   c=r+i+1
   for j in range(r+1,c):
    x=m[r][j]+m[j][c]+p[r]*p[j]*p[c]
    if 1>m[r][c]or m[r][c]>x:m[r][c]=x
 print((m[r][c]))
s()


# Optimized solution
def s():
 n=int(eval(input()))+1
 e=[input().split()for _  in[0]*~-n]
 p=[int(e[0][0])]+list(int(x[1])for x in e)
 m=[[0]*n for _ in[0]*n]
 for i in range(n):
  for r in range(n-i-1):
   c=r+i+1
   for j in range(r+1,c):
    x=m[r][j]+m[j][c]+p[r]*p[j]*p[c]
    if 1>m[r][c]or m[r][c]>x:m[r][c]=x
 print((m[r][c]))
if'__main__'==__name__:s()
# Original solution
def s():
 n=int(eval(input()))+1
 e=[input().split()for _  in[0]*~-n]
 p=[int(e[0][0])]+list(int(x[1])for x in e)
 m=[[0]*n for _ in[0]*n]
 for i in range(n):
  for r in range(n-i-1):
   c=r+i+1
   for j in range(r+1,c):
    x=m[r][j]+m[j][c]+p[r]*p[j]*p[c]
    if 1>m[r][c]or m[r][c]>x:m[r][c]=x
 print((m[r][c]))
if'__main__'==__name__:s()


# Optimized solution
def s():
 n=int(eval(input()))+1
 e=[input().split()for _  in[0]*~-n]
 p=[int(e[0][0])]+list(int(x[1])for x in e)
 m=[[0]*n for _ in[0]*n]
 for i in range(n):
  for r in range(n-i-1):
   c=r+i+1
   for j in range(r+1,c):
    x=m[r][j]+m[j][c]+p[r]*p[j]*p[c]
    if not 0<m[r][c]<=x:m[r][c]=x
 print((m[r][c]))
if'__main__'==__name__:s()
# Original solution
N = eval(input())
M = [list(map(int, input().split())) for i in range(N)]
cache = {}
for i in range(N):
    cache[(i, i+1)] = 0
def dfs(left, right):#[left, right)
    if (left, right) in cache:
        return cache[(left, right)]
    min_cost = float("inf")
    for mid in range(left+1, right):
        tmp = M[left][0] * M[mid-1][1] * M[right-1][1]
        tmp += dfs(left, mid)
        tmp += dfs(mid, right)
        if min_cost > tmp:
            min_cost = tmp
    return min_cost
print(dfs(0, len(M)))

# Optimized solution
N = eval(input())
M = [list(map(int, input().split())) for i in range(N)]
cache = {}
for i in range(N):
    cache[(i, i+1)] = 0
def dfs(left, right):
    if (left, right) in cache:
        return cache[(left, right)]
    min_cost = float("inf")
    for mid in range(left+1, right):
        tmp = M[left][0] * M[mid-1][1] * M[right-1][1]
        tmp += dfs(left, mid)
        tmp += dfs(mid, right)
        if min_cost > tmp:
            min_cost = tmp
    cache[(left, right)] = min_cost
    return min_cost
print(dfs(0, len(M)))
# Original solution
import sys

def get_min_cost(n, p):
	m = [[0 for x in range(n+1)] for x in range(n+1)]
	for s in range(n):
		i = 1
		for j in range(s+2, n+1):
			m[i][j] = sys.maxsize
			for k in range(i, j):
				m[i][j] = min(m[i][j], m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j])
			i+=1	
	return m[1][n]

n = int(eval(input()))
p = [0] * (n+1)
j = 0
for i in range(n):
	a = list(map(int, input().split()))
	if i == 0:
		p[j] = a[0]
		j += 1
	p[j] = a[1]
	j += 1
print((get_min_cost(n, p)))
		
		


# Optimized solution

def mcm(p,n):
	m=[[0 for x in range(n+1)] for x in range(n+1)]
	for s in range(n):
		for i,j in zip(list(range(1,n+1)),list(range(s+2,n+1))):
			m[i][j] = 1 << 30
			for k in range(i,j):
				m[i][j]=min(m[i][j],m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j])
	return m[1][n]

n=int(eval(input()))
p=[0]*(n+1)
for i in range(n-1):
	t=list(map(int,input().split()))
	p[i]=t[0];
t=list(map(int,input().split()))
p[n-1]=t[0]
p[n]=t[1]

ans=mcm(p,n)
print(ans)
# Original solution
import sys, itertools

n = int(sys.stdin.readline())
if n==1:
    print((0))
    quit()

MM = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]

amin = 0
m = MM[0][:]
for i in range(n-1):
    amin += m[0]*m[1]*MM[i+1][1]
    m[1]=MM[i+1][1]


for t in itertools.permutations(list(range(n-1)), n-2):

    mm=[m[:] for m in MM]
    o = list(t)
    ans=0
    for _ in range(n-2):
        i = o.pop(0)
        ans += mm[i][0]*mm[i+1][1]*mm[i][1]
        if ans>amin: break
        mm[i][1] = mm[i+1][1]
        mm.pop(i+1)
        
        for j in range(len(o)):
            if o[j]>i: o[j] -=1
    else:
        ans += mm[0][0]*mm[0][1]*mm[1][1]
        if amin>ans: amin=ans


print(amin)


# Optimized solution
import sys

n = int(sys.stdin.readline())

m = list(map(int, sys.stdin.readline().split()))
for _ in range(n-1):
    _, a = sys.stdin.readline().split()
    m.append(int(a))

c = [[-1 for _ in range(n)] for _ in range(n)]

def val(a, b):
    if a==b: return 0
    if c[a][b]>0: return c[a][b]
    c[a][b]=val(a+1,b)+m[a-1]*m[a]*m[b]
    for k in range(a+1, b):
        v=val(a,k)+val(k+1,b)+m[a-1]*m[k]*m[b]
        if c[a][b]>v: c[a][b]=v
    return c[a][b]

print((val(0, n-1)))
# Original solution
n = int(eval(input()))
data = []
for i in range(n):
    data.append(list(map(int, input().split())))

dp = [0 for _ in range(n)]
for i in range(n-1):
    dp[i] = data[i][0] * data[i][1] * data[i+1][1]



def mul(i, j):
    if i == j:
        return 0
    elif i == j - 1:
        return dp[i]
    num = 100000
    for k in range(i, j):
        num = min(num, mul(i, k) + mul(k+1, j) + data[i][0] * data[k][1] * data[j][1])
    return num

print((mul(0, n-1)))



# Optimized solution
n = int(eval(input()))
data = []
for i in range(n):
    data.append(list(map(int, input().split())))

memo = []
for _ in range(n):
    memo.append([-1]*n)


def mul(i, j):
    if i == j:
        return 0
    if memo[i][j] >= 0:
        return memo[i][j]
    num = 100000
    for k in range(i, j):
        num = min(num, mul(i, k) + mul(k+1, j) + data[i][0] * data[k][1] * data[j][1])
    memo[i][j] = num
    return memo[i][j]

print((mul(0, n-1)))