def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[]
it=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**32:
            flag=1
    elif s[0]=="end":
        if len(V)>0:
            if V[-1]>0:
                it/=V[-1]
            V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long long unsigned int llui;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector< vector<int> > vvi;
typedef set<int> si;
typedef map<string, int> msi;
typedef map<int, int> mii;

#define INF 1000000000
#define MOD 1000000007
#define EPS 1e-9
#define DEBUG(x) cout<< '>' << #x << ':' << x << "\n";
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SORT(x) sort((x).begin(),(x).end())
#define UNIQUE(x) SORT(x),(x).erase(unique((x).begin(),(x).end()),(x).end())
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define SZ(a) int((a).size())
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a,b) memset(a,b,sizeof(a))

#define FOR(i,a,b) for (int i = int(a); i < int(b); i++)
#define FORD(i,a,b) for(int i=int(b)-1; i>=int(a); i--)
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REP1(i,n) for (int i = 1; i <= int(n); i++)
#define REV(i, n) for (int i = int(n)-1; i >=0 ; i--)

#define sd(n) scanf("%d",&n)
#define dsd(n) int n; scanf("%d",&n)
#define sd2(a,b) scanf("%d%d",&a,&b)
#define dsd2(a,b) int a,b; scanf("%d%d",&a,&b)
#define sd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define dsd3(a,b,c) int a,b,c; scanf("%d%d%d",&a,&b,&c)
#define pd(n) printf("%d\n",n)
#define pds(n) printf("%d ",n)
#define sll(n) scanf("%lld",&n)
#define dsll(n) ll n; scanf("%lld",&n)
#define sll2(a,b) scanf("%lld%lld",&a,&b)
#define dsll2(a,b) ll a,b; scanf("%lld%lld",&a,&b)
#define sll3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define dsll3(a,b,c) ll a,b,c; scanf("%lld%lld%lld",&a,&b,&c)
#define pll(n) printf("%lld\n",n)
#define plls(n) printf("%lld ",n)

#define trace(x) cout << '>' << #x << ':' << x << "\n"
#define trace2(x,y) cout<< '>' << #x << ':' << x << " | " << #y << ':' << y << "\n"
#define trace3(a,b,c) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define trace4(a,b,c,d) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"

#define PR(a,n) { cout<<#a<<" = "; REP(_,n) cout<<a[_]<<' '; cout<<"\n"; }
#define PR1(a,n) { cout<<#a<<" = "; REP1(_,n) cout<<a[_]<<' '; cout<<"\n"; }
#define _W(a,n) { REP(_,n) cout<<a[_]<<' '; cout<<"\n"; }

inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }

#define LSOne(S) (S&(-S))

const double pi = acos(-1);
const int oo = 0x3f3f3f3f;

int cmp(double x, double y = 0, double tol = EPS) {
    return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}

template<class T> bool uin(T &a,T b) {return a > b ? (a=b, true):false;}
template<class T> bool uax(T &a,T b) {return a < b ? (a=b, true):false;}

/////////////////////////////////////////////////////////////////////

const int MAX_N = 5*1010*100;
//const int MAXN = 510;
//int arr[MAXN][MAXN];

int main(){
    dsd(N);
    ll x=0,it=1;
    vi V;
    ll maxn=2ll*INT_MAX+1;
    bool flag=false;
    REP(i,N){
        string s;
        cin>>s;
        if(s=="for"){
            dsd(n);
            if(it>maxn) n=1;
            V.PB(n);
            it*=n;
        }else if(s=="add"){
            if(it>maxn) flag=true;
            else if(it>maxn-x) flag=true;
            x+=it;
        }else if(s=="end"){
            it/=V.back();
            V.pop_back();
        }
    }
    if(flag){
        return !printf("OVERFLOW!!!\n");
    }
    pll(x);
    return 0;
}


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[]
it=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1])
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**31:
            flag=1
    elif s[0]=="end":
        it/=V[-1]
        k=len(V)-1
        V.pop(k)
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[]
it=100
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**32:
            flag=1
    elif s[0]=="end":
        if len(V)>0:
            if V[-1]>0:
                it/=V[-1]
            V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x)//100)
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[]
it=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1])
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**32:
            flag=1
    elif s[0]=="end":
        it/=V[-1]
        k=len(V)-1
        V.pop(k)
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[]
it=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**32:
            flag=1
    elif s[0]=="end":
        it/=V[-1]
        V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[]
it=1.0
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**32:
            flag=1
    elif s[0]=="end":
        if len(V)>0:
            if V[-1]>0:
                it/=V[-1]
            V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


N=int(input())
V=[1]
it=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**32:
            flag=1
            break
    elif s[0]=="end":
        it=it//V[-1]
        V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long long unsigned int llui;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector< vector<int> > vvi;
typedef set<int> si;
typedef map<string, int> msi;
typedef map<int, int> mii;

#define INF 1000000000
#define MOD 1000000007
#define EPS 1e-9
#define DEBUG(x) cout<< '>' << #x << ':' << x << "\n";
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SORT(x) sort((x).begin(),(x).end())
#define UNIQUE(x) SORT(x),(x).erase(unique((x).begin(),(x).end()),(x).end())
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define SZ(a) int((a).size())
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a,b) memset(a,b,sizeof(a))

#define FOR(i,a,b) for (int i = int(a); i < int(b); i++)
#define FORD(i,a,b) for(int i=int(b)-1; i>=int(a); i--)
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REP1(i,n) for (int i = 1; i <= int(n); i++)
#define REV(i, n) for (int i = int(n)-1; i >=0 ; i--)

#define sd(n) scanf("%d",&n)
#define dsd(n) int n; scanf("%d",&n)
#define sd2(a,b) scanf("%d%d",&a,&b)
#define dsd2(a,b) int a,b; scanf("%d%d",&a,&b)
#define sd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define dsd3(a,b,c) int a,b,c; scanf("%d%d%d",&a,&b,&c)
#define pd(n) printf("%d\n",n)
#define pds(n) printf("%d ",n)
#define sll(n) scanf("%lld",&n)
#define dsll(n) ll n; scanf("%lld",&n)
#define sll2(a,b) scanf("%lld%lld",&a,&b)
#define dsll2(a,b) ll a,b; scanf("%lld%lld",&a,&b)
#define sll3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define dsll3(a,b,c) ll a,b,c; scanf("%lld%lld%lld",&a,&b,&c)
#define pll(n) printf("%lld\n",n)
#define plls(n) printf("%lld ",n)

#define trace(x) cout << '>' << #x << ':' << x << "\n"
#define trace2(x,y) cout<< '>' << #x << ':' << x << " | " << #y << ':' << y << "\n"
#define trace3(a,b,c) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define trace4(a,b,c,d) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"

#define PR(a,n) { cout<<#a<<" = "; REP(_,n) cout<<a[_]<<' '; cout<<"\n"; }
#define PR1(a,n) { cout<<#a<<" = "; REP1(_,n) cout<<a[_]<<' '; cout<<"\n"; }
#define _W(a,n) { REP(_,n) cout<<a[_]<<' '; cout<<"\n"; }

inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }

#define LSOne(S) (S&(-S))

const double pi = acos(-1);
const int oo = 0x3f3f3f3f;

int cmp(double x, double y = 0, double tol = EPS) {
    return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}

template<class T> bool uin(T &a,T b) {return a > b ? (a=b, true):false;}
template<class T> bool uax(T &a,T b) {return a < b ? (a=b, true):false;}

/////////////////////////////////////////////////////////////////////

const int MAX_N = 5*1010*100;
//const int MAXN = 510;
//int arr[MAXN][MAXN];

int main(){
    return 0;
}


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[1]
it=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**32:
            flag=1
            break
    elif s[0]=="end":
        it/=V[-1]
        V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[]
it=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**32:
            flag=1
    elif s[0]=="end":
        try:
            it/=V[-1]
        except:
            print("WA")
        V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[1]
it=100
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it=n*V[-1]
        V.append(it)
    elif s[0]=="add":
        x+=V[-1]
        if x>=2**32:
            flag=1
    elif s[0]=="end":
        V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


N=int(input())
V=[1]
it=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**32:
            flag=1
            break
    elif s[0]=="end":
        it=int(it)
        it=it//V[-1]
        V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[]
it=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**32:
            flag=1
    elif s[0]=="end":
        if len(V)>0:
            it/=V[-1]
            V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

import math

N=int(input())
V=[1]
it=1
den=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        t=math.gcd(it,den)
        it/=t
        den/=t
        x+=(it/den)
        if x>=2**32:
            flag=1
            break
    elif s[0]=="end":
        den*=V[-1]
        V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[]
it=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1])
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**31:
            flag=1
            break
    elif s[0]=="end":
        it/=V[-1]
        k=len(V)-1
        V.pop(k)
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[]
it=100.0
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**32:
            flag=1
    elif s[0]=="end":
        if len(V)>0:
            if V[-1]>0:
                it/=V[-1]
            V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x)//100)
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

import math

N=int(input())
V=[1]
it=1
den=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        it=int(it)
        den=int(den)
        t=math.gcd(it,den)
        it/=t
        den/=t
        x+=(it/den)
        if x>=2**32:
            flag=1
            break
    elif s[0]=="end":
        den*=V[-1]
        V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

import math

N=int(input())
V=[1]
it=1
den=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        if it>2**32:
            n=1
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**32:
            flag=1
            break
    elif s[0]=="end":
        it/=V[-1]
        V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[1]
it=1
den=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=(it/den)
        if x>=2**32:
            flag=1
            break
    elif s[0]=="end":
        den*=V[-1]
        V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1


l=int(input())
x=[]
ans=0
po=(2**32)-1
for i in range(l):
    p=input()
    if p[0]=='f':
        x.append(int(p[4:]))
    elif p[0]=='e':
        x.pop()
    else:
        if len(x)==0:
            ans+=1
        else:
            r=1
            for j in x:
                r*=j
            ans+=r
    if ans>po:
        ans="OVERFLOW!!!"
        break
    # print(x)
print(ans)


l=int(input())
x=[1]
ans=0
po=(2**32)-1
for i in range(l):
    p=input()
    if p[0]=='f':
        t=x[-1]*int(p[4:])
        if t>po:
            t=po+1
        x.append(t)
    elif p[0]=='e':
        x.pop()
    else:
        if len(x)==1:
            ans+=1
        else:
            ans+=x[-1]
            if ans > po:
                ans = "OVERFLOW!!!"
                break

    if ans>po:
        ans="OVERFLOW!!!"
        break
    # print(x)
print(ans)


l=int(input())
x=[1]
ans=0
po=(2**32)-1
for i in range(l):
    p=input()
    if p[0]=='f':
        t=x[-1]*int(p[4:])
        if t>po:
            t+=1
        x.append(t)
    elif p[0]=='e':
        x.pop()
    else:
        if len(x)==1:
            ans+=1
        else:
            ans+=x[-1]
            if ans > po:
                ans = "OVERFLOW!!!"
                break

    if ans>po:
        ans="OVERFLOW!!!"
        break
    # print(x)
print(ans)


#include<bits/stdc++.h>
using namespace std;
const int L=200001;
int arr[L];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i,j;
        int r,mx = 0,M;
        for(i=0; i<n; i++) scanf("%d",&arr[i]);
        i--;
        if(i==0) goto out;
        sort(arr,arr+n);
        j=n-2;

        M=arr[j]<<1;

        while(arr[j]>mx)
        {
            i=n-1;
            for(int x=arr[j]<<1; x<=M; x+=arr[j])
            {
                while(arr[i]>=x) i--;
                r=arr[i] % arr[j];
                if(r>mx) mx=r;
            }
            if(j==0) break;
            j--;
        }
out:
       printf("%d\n",mx);
    }
}


#include<bits/stdc++.h>
using namespace std;
const int L=200001;
int arr[L];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i,j;
        int r,mx = 0,M;
        for(i=0; i<n; i++) scanf("%d",&arr[i]);
        if(n==1) goto out;
        sort(arr,arr+n);

        M=arr[n-1]<<1;
        j=n-2;

        while(arr[j]>mx)
        {

            for(int x=arr[j]<<1; x<=M; x+=arr[j])
            {
                i=n-1;
                while(arr[i]>=x) i--;
                r=arr[i] % arr[j];
                if(r>mx) mx=r;
            }
            if(j==0) break;
            j--;
        }
out:
       printf("%d\n",mx);
    }
}


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

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        vector<int> arr(n);
        int i,j;
        int r,mx = 0,M;
        for(i=0; i<n; i++) scanf("%d",&arr[i]);
        if(n==1) goto out;
        sort(arr.begin(),arr.end());

        M=arr[n-1]<<1;
        j=n-2;

        while(arr[j]>mx)
        {

            for(int x=arr[j]<<1; x<=M; x+=arr[j])
            {
                i=n-1;
                while(arr[i]>=x) i--;
                r=arr[i] % arr[j];
                if(r>mx) mx=r;
                if(i==n-1) break;
            }
            if(j==0) break;
            j--;
        }
out:
       printf("%d\n",mx);
    }
}


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

int main(){
    int n;
    while(cin>>n){
        vector<int> s;
        vector<int>::iterator iti,itj;
        for(int temp,i=0;i<n;i++){
            cin>>temp;
            s.push_back(temp);
        }
        sort(s.begin(),s.end());
        int r,mx = 0,M;
        itj=s.end();
        --itj;
        M=*itj <<1;
        while(*itj>mx){
            for(int x=*(itj)<<1;x<=M;x+=*itj){
                iti=s.end(); iti--;
                while(*(iti)>=x) iti--;
                r=*iti % *itj;
                if(r>mx) mx=r;
            }
            if(itj==s.begin()) break;
            itj--;
        }

        cout<<mx<<endl;
    }
}


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

int main(){
    int n;
    while(cin>>n){
        set<int> s;
        set<int>::iterator it1,it2;
        for(int temp,i=0;i<n;i++){
            cin>>temp;
            s.insert(temp);
        }
        int r,mx = 0;
        for(it1=s.end();true;){
                --it1;
                if(*it1<=mx||it1==s.begin()) break;
            for(it2=it1;it2!=s.begin();){
                --it2;
                if(*it2<=mx) break;
                r=*it1 % *it2;
                if(r>mx) mx=r;
            }
        }
        cout<<mx<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
const int L=200001;
vector<int> arr(L);

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i,j;
        int r,mx = 0,M;
        for(i=0; i<n; i++) scanf("%d",&arr[i]);
        if(n==1) goto out;
        sort(arr.begin(),arr.begin()+n);

        M=arr[n-1]<<1;
        j=n-2;

        while(arr[j]>mx)
        {

            for(int x=arr[j]<<1; x<=M; x+=arr[j])
            {
                i=n-1;
                while(arr[i]>=x) i--;
                r=arr[i] % arr[j];
                if(r>mx) mx=r;
            }
            if(j==0) break;
            j--;
        }
out:
       printf("%d\n",mx);
    }
}


#include<bits/stdc++.h>
using namespace std;
const int L=2e6+1;
const int M=1e6;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        vector<bool> ok(L,false);
        vector<int> l(L,0);
        for (int x,i=1; i<=n; i++)
        {
            scanf("%d",&x);
            ok[x]=true;
        }
        for (int i=1; i<L; i++) {if (ok[i]) l[i]=i;
            else l[i]=l[i-1];}
        int ans=0;
        for (int i=1; i<=M; i++)
            if (ok[i])
            {
                int j=2;
                while (i*(j-1)<M)
                {
                    if (l[i*j-1]>i) ans=max(ans,l[i*j-1]%i);
                    j++;
                }
            }
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int L=2e6;
const int M=1e6;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        vector<bool> ok(L,false);
        vector<int> l(L,0);
        for (int x,i=1; i<=n; i++)
        {
            scanf("%d",&x);
            ok[x]=true;
        }
        for (int i=1; i<L; i++)
        {
            if (ok[i]) l[i]=i;
            else l[i]=l[i-1];
        }
        int ans=0;
        for (int i=2; i<M; i++)
            if (ok[i])
            {
                int j=2;
                while (i*(j-1)<M)
                {
                    if (l[i*j-1]>i) ans=max(ans,l[i*j-1]%i);
                    j++;
                }
            }
        printf("%d\n",ans);
    }
    return 0;
}


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

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        vector<int> arr(n);
        int i,j;
        int r,mx = 0,M;
        for(i=0; i<n; i++) scanf("%d",&arr[i]);
        if(n==1) goto out;
        sort(arr.begin(),arr.end());

        M=arr[n-1]<<1;
        j=n-2;

        while(arr[j]>mx)
        {

            for(int x=arr[j]<<1; x<=M; x+=arr[j])
            {
                i=n-1;
                while(arr[i]>=x) i--;
                r=arr[i] % arr[j];
                if(r>mx) mx=r;
            }
            if(j==0) break;
            j--;
        }
out:
       printf("%d\n",mx);
    }
}


#include<bits/stdc++.h>
using namespace std;
const int L=1e6+1;
const int M=1e6;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        vector<bool> ok(L,false);
        vector<int> l(L,0);
        for (int x,i=1; i<=n; i++)
        {
            scanf("%d",&x);
            ok[x]=true;
        }
        for (int i=1; i<L; i++)
        {
            if (ok[i]) l[i]=i;
            else l[i]=l[i-1];
        }
        int ans=0;
        for (int i=1; i<=M; i++)
            if (ok[i])
            {
                int j=2;
                while (i*(j-1)<M)
                {
                    if (l[i*j-1]>i) ans=max(ans,l[i*j-1]%i);
                    j++;
                }
            }
        printf("%d\n",ans);
    }
    return 0;
}


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

int main(){
    int n;
    while(cin>>n){
        set<int> s;
        set<int>::iterator it1,it2;
        for(int temp,i=0;i<n;i++){
            cin>>temp;
            s.insert(temp);
        }
        int r,mx = 0;
        for(it1=s.begin();it1!=s.end();++it1){
            for(it2=it1;it2!=s.end();++it2){
                r=*it2 % *it1;
                if(r>mx) mx=r;
            }
        }
        cout<<mx<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
const int L=200001;
int arr[L];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i,j;
        int r,mx = 0,M;
        for(i=0; i<n; i++) scanf("%d",&arr[i]);
        if(i==1) goto out;
        sort(arr,arr+i);

        j=n-2;

        M=arr[j]<<1;

        while(arr[j]>mx)
        {
            i=n-1;
            for(int x=arr[j]<<1; x<=M; x+=arr[j])
            {
                while(arr[i]>=x) i--;
                r=arr[i] % arr[j];
                if(r>mx) mx=r;
            }
            if(j==0) break;
            j--;
        }
out:
       printf("%d\n",mx);
    }
}


#include<bits/stdc++.h>
using namespace std;
const int L=200001;
int arr[L];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i,j;
        int r,mx = 0,M;
        for(i=0; i<n; i++) scanf("%d",&arr[i]);
        if(--i==0) goto out;
        sort(arr,arr+i);

        j=n-2;

        M=arr[j]<<1;

        while(arr[j]>mx)
        {
            i=n-1;
            for(int x=arr[j]<<1; x<=M; x+=arr[j])
            {
                while(arr[i]>=x) i--;
                r=arr[i] % arr[j];
                if(r>mx) mx=r;
            }
            if(j==0) break;
            j--;
        }
out:
       printf("%d\n",mx);
    }
}


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

int main(){
    int n;
    while(cin>>n){
        set<int> s;
        set<int>::iterator iti,itj;
        for(int temp,i=0;i<n;i++){
            cin>>temp;
            s.insert(temp);
        }
        int r,mx = 0,M;
        itj=s.end();
        --itj;
        M=*itj <<1;
        while(true){
            for(int x=*(itj)<<1;x<=M;x+=*itj){
                iti=s.end(); iti--;
                while(*(iti)>=x) iti--;
                r=*iti % *itj;
                if(r>mx) mx=r;
            }
            if(*itj<=mx||itj==s.begin()) break;
            itj--;
        }

        cout<<mx<<endl;
    }
}


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

int main(){
    int n;
    while(cin>>n){
        vector<int> v(n);
        for(int i=0;i<n;i++) cin>>v[i];
        sort(v.begin(),v.end());
        int r,mx = 0;
        for(int i=n-1;i>0;i--){
            for(int j=i-1;j>=0;j--){
                r=v[i]%v[j];
                if(r>mx) mx=r;
            }
        }
        cout<<mx<<endl;
    }
}


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

int main(){
    int n;
    while(cin>>n){
        set<int> s;
        set<int>::iterator iti,itj;
        for(int temp,i=0;i<n;i++){
            cin>>temp;
            s.insert(temp);
        }
        int r,mx = 0,M;
        itj=s.end();
        --itj;
        M=*itj <<1;
        cout<<M<<endl;
        while(true){
            for(int x=*(itj)<<1;x<=M;x+=*itj){
                iti=s.end(); iti--;
                while(*(iti)>=x) iti--;
                r=*iti % *itj;
                if(r>mx) mx=r;
            }
            if(*itj<=mx||itj==s.begin()) break;
            itj--;
        }

        cout<<mx<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
const int L=2e6+2;
const int M=1e6+1;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        vector<bool> ok(L,false);
        vector<int> l(L,0);
        for (int x,i=1; i<=n; i++)
        {
            scanf("%d",&x);
            ok[x]=true;
        }
        for (int i=1; i<L; i++)
        {
            if (ok[i]) l[i]=i;
            else l[i]=l[i-1];
        }
        int ans=0;
        for (int i=2; i<M; i++)
            if (ok[i])
            {
                int j=2;
                while (i*(j-1)<M)
                {
                    if (l[i*j-1]>i) ans=max(ans,l[i*j-1]%i);
                    if(ans=i-1) break;
                    j++;
                }
            }
        printf("%d\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<ld,ld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define MAXINT ((~0) ^ (1 << 31))
#define MAXLL ((~0) ^ ((ll)1 << 63))
#define INF ((int)2e9)
#define INFL ((ll)9e18)
#define FOR(i,j,k) for(int i = j; i < k; i++)
#define REP(i,j,k) for(int i = j; i >= k; i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()

//srand (time(NULL));

using namespace std;

setmax(1e6+10);

set<int> st;
set<int>::iterator it,it2;
int n,ans;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n;
    FOR(i,0,n)
    {
	int k;
	cin >> k;
	st.insert(k);
    }
    for(it = st.begin(); it != st.end(); it++)
    {
	for(int now = *it * 2; now < maxn; now += *it)
	{
	  /*  if(*it == 3 && now == 6)
	    {
		cout << "*";
	    }*/
	    it2 = st.upper_bound(now-1);
	    if(it2 != st.begin()) it2--;
	    if(*it2 > *it)ans = max(ans, *it2 % *it);
	}
    }
    cout << ans;
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<ld,ld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define MAXINT ((~0) ^ (1 << 31))
#define MAXLL ((~0) ^ ((ll)1 << 63))
#define INF ((int)2e9)
#define INFL ((ll)9e18)
#define FOR(i,j,k) for(int i = j; i < k; i++)
#define REP(i,j,k) for(int i = j; i >= k; i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()

//srand (time(NULL));

using namespace std;

setmax(2e6+10);

set<int> st;
set<int>::iterator it,it2;
int n,ans;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n;
    FOR(i,0,n)
    {
	int k;
	cin >> k;
	st.insert(k);
    }
    for(it = st.begin(); it != st.end(); it++)
    {
	//cout << "*";
	for(int now = (*it); now < maxn; now += *it)
	{
//	    cout << now << endl;
	  /*  if(*it == 3 && now == 6)
	    {
		cout << "*";
	    }*/
	    it2 = st.lower_bound(now);
	    if(it2 != st.begin()) it2--;
	    if(*it2 > *it)ans = max(ans, *it2 % *it);
	 //   cout << *it2 << " " << *it << endl;
	}
    }
    cout << ans;
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<ld,ld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define MAXINT ((~0) ^ (1 << 31))
#define MAXLL ((~0) ^ ((ll)1 << 63))
#define INF ((int)2e9)
#define INFL ((ll)9e18)
#define FOR(i,j,k) for(int i = j; i < k; i++)
#define REP(i,j,k) for(int i = j; i >= k; i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()

//srand (time(NULL));

using namespace std;

setmax(2e5+10);

set<int> st;
set<int>::iterator it,it2;
int n,ans;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n;
    FOR(i,0,n)
    {
	int k;
	cin >> k;
	st.insert(k);
    }
    for(it = st.begin(); it != st.end(); it++)
    {
	for(int now = *it; now < maxn; now += *it)
	{
	  /*  if(*it == 3 && now == 6)
	    {
		cout << "*";
	    }*/
	    it2 = st.lower_bound(now);
	    if(it2 != st.begin()) it2--;
	    if(*it2 > *it)ans = max(ans, *it2 % *it);
	}
    }
    cout << ans;
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<ld,ld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define MAXINT ((~0) ^ (1 << 31))
#define MAXLL ((~0) ^ ((ll)1 << 63))
#define INF ((int)2e9)
#define INFL ((ll)9e18)
#define FOR(i,j,k) for(int i = j; i < k; i++)
#define REP(i,j,k) for(int i = j; i >= k; i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()

//srand (time(NULL));

using namespace std;

setmax(2e6+10);

set<int> st;
set<int>::iterator it,it2;
int n,ans;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n;
    FOR(i,0,n)
    {
	int k;
	cin >> k;
	st.insert(k);
    }
    for(it = st.begin(); it != st.end(); it++)
    {
	int t = *it;
	for(int now = t; now < maxn; now += t)
	{
	    it2 = st.lower_bound(now);
	    if(it2 != st.begin()) it2--;
	    if(*it2 > t)ans = max(ans, *it2 - now + t);
	}
    }
    cout << ans;
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<ld,ld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define MAXINT ((~0) ^ (1 << 31))
#define MAXLL ((~0) ^ ((ll)1 << 63))
#define INF ((int)2e9)
#define INFL ((ll)9e18)
#define FOR(i,j,k) for(int i = j; i < k; i++)
#define REP(i,j,k) for(int i = j; i >= k; i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()

//srand (time(NULL));

using namespace std;

setmax(1e6+10);

set<int> st;
set<int>::iterator it,it2;
int n,ans;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n;
    FOR(i,0,n)
    {
	int k;
	cin >> k;
	st.insert(k);
    }
    for(it = st.begin(); it != st.end(); it++)
    {
	for(int now = *it * 2; now < maxn; now += *it)
	{
	  /*  if(*it == 3 && now == 6)
	    {
		cout << "*";
	    }*/
	    it2 = st.lower_bound(now-1);
	    if(it2 != st.begin()) it2--;
	    if(*it2 > *it)ans = max(ans, *it2 % *it);
	}
    }
    cout << ans;
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<ld,ld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define MAXINT ((~0) ^ (1 << 31))
#define MAXLL ((~0) ^ ((ll)1 << 63))
#define INF ((int)2e9)
#define INFL ((ll)9e18)
#define FOR(i,j,k) for(int i = j; i < k; i++)
#define REP(i,j,k) for(int i = j; i >= k; i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()

//srand (time(NULL));

using namespace std;

setmax(1e6+10);

set<int> st;
set<int>::iterator it,it2;
int n,ans;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n;
    FOR(i,0,n)
    {
	int k;
	cin >> k;
	st.insert(k);
    }
    for(it = st.begin(); it != st.end(); it++)
    {
	for(int now = *it * 2; now < maxn; now += *it)
	{
	  /*  if(*it == 3 && now == 6)
	    {
		cout << "*";
	    }*/
	    it2 = st.lower_bound(now);
	    if(it2 != st.begin()) it2--;
	    if(*it2 > *it)ans = max(ans, *it2 % *it);
	}
    }
    cout << ans;
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<ld,ld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define MAXINT ((~0) ^ (1 << 31))
#define MAXLL ((~0) ^ ((ll)1 << 63))
#define INF ((int)2e9)
#define INFL ((ll)9e18)
#define FOR(i,j,k) for(int i = j; i < k; i++)
#define REP(i,j,k) for(int i = j; i >= k; i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()

//srand (time(NULL));

using namespace std;

setmax(2e6+10);

set<int> st;
set<int>::iterator it,it2;
int n,ans;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n;
    FOR(i,0,n)
    {
	int k;
	cin >> k;
	st.insert(k);
    }
    for(it = st.begin(); it != st.end(); it++)
    {
	int t = *it;
	if(t == 1) continue;
	for(int now = t; now < maxn; now += t)
	{
	    it2 = st.lower_bound(now);
	    if(it2 != st.begin()) it2--;
	    if(*it2 > t)ans = max(ans, *it2 - now + t);
	    if(now > maxn/2) break;
	}
    }
    cout << ans;
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<ld,ld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define MAXINT ((~0) ^ (1 << 31))
#define MAXLL ((~0) ^ ((ll)1 << 63))
#define INF ((int)2e9)
#define INFL ((ll)9e18)
#define FOR(i,j,k) for(int i = j; i < k; i++)
#define REP(i,j,k) for(int i = j; i >= k; i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()

//srand (time(NULL));

using namespace std;

setmax(1e6+10);

set<int> st;
set<int>::iterator it,it2;
int n,ans;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n;
    FOR(i,0,n)
    {
	int k;
	cin >> k;
	st.insert(k);
    }
    for(it = st.begin(); it != st.end(); it++)
    {
	for(int now = *it; now < maxn; now += *it)
	{
	  /*  if(*it == 3 && now == 6)
	    {
		cout << "*";
	    }*/
	    it2 = st.lower_bound(now);
	    if(it2 != st.begin()) it2--;
	    if(*it2 > *it)ans = max(ans, *it2 % *it);
	}
    }
    cout << ans;
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<ld,ld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define MAXINT ((~0) ^ (1 << 31))
#define MAXLL ((~0) ^ ((ll)1 << 63))
#define INF ((int)2e9)
#define INFL ((ll)9e18)
#define FOR(i,j,k) for(int i = j; i < k; i++)
#define REP(i,j,k) for(int i = j; i >= k; i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()

//srand (time(NULL));

using namespace std;

setmax(2e6+10);

int n,ans, pr[maxn];
bool num[maxn];

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n;
    FOR(i,0,n)
    {
	int k;
	cin >> k;
	num[k] = true;
    }
    FOR(i,0,maxn)
    {
	if(num[i]) pr[i] = i;
	else pr[i] = pr[i-1];
    }
    FOR(i,0,maxn) if(num[i])
    {
	for(int j = 2*i; j < maxn; j+=i)
	{
	    if(pr[j-1] > i)ans = max(ans,pr[j-1]%i);
	}
    }
    cout << ans;
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<ld,ld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define MAXINT ((~0) ^ (1 << 31))
#define MAXLL ((~0) ^ ((ll)1 << 63))
#define INF ((int)2e9)
#define INFL ((ll)9e18)
#define FOR(i,j,k) for(int i = j; i < k; i++)
#define REP(i,j,k) for(int i = j; i >= k; i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()

//srand (time(NULL));

using namespace std;

setmax(2e6+10);

set<int> st;
set<int>::iterator it,it2;
int n,ans;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n;
    FOR(i,0,n)
    {
	int k;
	cin >> k;
	st.insert(k);
    }
    for(it = st.begin(); it != st.end(); it++)
    {
	int t = *it;
	if(t == 1) continue;
	for(int now = t; now < maxn; now += t)
	{
	    it2 = st.lower_bound(now);
	    if(it2 != st.begin()) it2--;
	    if(*it2 > t)ans = max(ans, *it2 - now + t);
	}
    }
    cout << ans;
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<ld,ld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define MAXINT ((~0) ^ (1 << 31))
#define MAXLL ((~0) ^ ((ll)1 << 63))
#define INF ((int)2e9)
#define INFL ((ll)9e18)
#define FOR(i,j,k) for(int i = j; i < k; i++)
#define REP(i,j,k) for(int i = j; i >= k; i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
#define all(a) a.begin(),a.end()

//srand (time(NULL));

using namespace std;

setmax(2e6+10);

int n,ans, pr[maxn];
bool num[maxn];

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);

    cin >> n;
    FOR(i,0,n)
    {
	int k;
	cin >> k;
	num[k] = true;
    }
    FOR(i,0,maxn)
    {
	if(num[i]) pr[i] =i;
	else pr[i] = pr[i-1];
    }
    FOR(i,0,maxn) if(num[i])
    {
	for(int j = 2*i; j < maxn; j+=i)
	{
	    if(pr[j] > i)ans = max(ans,pr[j]%i);
	}
    }
    cout << ans;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<queue>
#include<stack>

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;

using namespace std;

int n, ans, a[200001], p[2000001], mx;

int main(){
	sd(n);
	for(int i = 0; i < n; i++){
		sd(a[i]);
		mx = max(mx,a[i]);
	}

	sort(a, a+n);

	for(int i = a[0]+1, j = 0; i <= 2*mx; i++){
		while(j < n and a[j] < i) j++;
		j--;
		p[i] = a[j];
	}

	for(int i = 0; i < n; i++){
		if(i > 0 and a[i] == a[i-1]) continue;
		for(int j = 2*a[i]; j <= 2*mx; j += a[i]){
			ans = max(ans, p[j]%a[i]);
		}
	}

	tr(ans);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<queue>
#include<stack>

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;

using namespace std;

int n, ans, a[200001], p[2000001], mx;

int main(){
	sd(n);
	for(int i = 0; i < n; i++){
		sd(a[i]);
		mx = max(mx,a[i]);
	}

	sort(a, a+n);

	p[a[0]] = 0;

	for(int i = a[0]+1, j = 0; i <= 2*mx and j < n; i++){
		while(j < n and a[j] < i) j++;
		j--;
		p[i] = a[j];
	}

	for(int i = 0; i < n; i++){
		for(int j = 2*a[i]; j <= 2*mx; j += a[i]){
			if(p[j] == p[j-a[i]]) break;
			ans = max(ans, p[j]%a[i]);
		}
	}

	tr(ans);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<queue>
#include<stack>

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;

using namespace std;

int n, ans, a[200001], p[2000001], mx;

int main(){
	sd(n);
	for(int i = 0; i < n; i++){
		sd(a[i]);
		mx = max(mx,a[i]);
	}

	sort(a, a+n);

	p[a[0]] = 0;

	for(int i = a[0]+1, j = 0; i <= 2000000 and j < n; i++){
		while(j < n and a[j] < i){
			j++;
		}
		j--;

//		if(i <= 10)
//		tr3(i,j,a[j]);

		p[i] = a[j];
	}

	for(int i = 0; i < n; i++){
		for(int j = a[i]+a[i]; j <= 2*mx; j += a[i]){
//			tr3(j,p[j],a[i]);
			ans = max(ans, p[j]%a[i]);
		}
	}

	tr(ans);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<queue>
#include<stack>

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;

using namespace std;

int n, ans, a[100001], p[1000001], mx;

int main(){
	sd(n);
	for(int i = 0; i < n; i++){
		sd(a[i]);
		mx = max(mx,a[i]);
	}

	sort(a, a+n);

	p[a[0]] = 0;

	for(int i = a[0]+1, j = 0; i <= 1000000 and j < n; i++){
		while(j < n and a[j] < i){
			j++;
		}
		j--;

//		if(i <= 10)
//		tr3(i,j,a[j]);

		p[i] = a[j];
	}

	for(int i = 0; i < n; i++){
		for(int j = a[i]+a[i]; j <= mx+a[i]; j += a[i]){
//			tr3(j,p[j],a[i]);
			ans = max(ans, p[j]%a[i]);
		}
	}

	tr(ans);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<queue>
#include<stack>

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;

using namespace std;

int n, ans, a[100001], p[1000001], mx;

int main(){
	sd(n);
	for(int i = 0; i < n; i++){
		sd(a[i]);
		mx = max(mx,a[i]);
	}

	sort(a, a+n);

	p[a[0]] = 0;

	for(int i = a[0]+1, j = 0; i <= 1000000 and j < n; i++){
		while(j < n and a[j] < i){
			j++;
		}
		j--;

//		if(i <= 10)
//		tr3(i,j,a[j]);

		p[i] = a[j];
	}

	for(int i = 0; i < n; i++){
		for(int j = a[i]+a[i]; j <= min(mx+a[i],1000000); j += a[i]){
//			tr3(j,p[j],a[i]);
			ans = max(ans, p[j]%a[i]);
		}
	}

	tr(ans);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<queue>
#include<stack>

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;

using namespace std;

int n, ans, a[200001], p[2000001], mx;

int main(){
	sd(n);
	for(int i = 0; i < n; i++){
		sd(a[i]);
		mx = max(mx,a[i]);
	}

	sort(a, a+n);

	for(int i = a[0]+1, j = 0; i <= 2*mx; i++){
		while(j < n and a[j] < i) j++;
		j--;
		p[i] = a[j];
	}

	for(int i = 0; i < n; i++){
		for(int j = 2*a[i]; j <= 2*mx; j += a[i]){
			if(p[j] == p[j-a[i]]) break;
			ans = max(ans, p[j]%a[i]);
		}
	}

	tr(ans);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<queue>
#include<stack>

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;

using namespace std;

int n, ans, a[100001], p[2000001], mx;

int main(){
	sd(n);
	for(int i = 0; i < n; i++){
		sd(a[i]);
		mx = max(mx,a[i]);
	}

	sort(a, a+n);

	p[a[0]] = 0;

	for(int i = a[0]+1, j = 0; i <= 2000000 and j < n; i++){
		while(j < n and a[j] < i){
			j++;
		}
		j--;

//		if(i <= 10)
//		tr3(i,j,a[j]);

		p[i] = a[j];
	}

	for(int i = 0; i < n; i++){
		for(int j = a[i]+a[i]; j <= min(mx+a[i],2000000); j += a[i]){
//			tr3(j,p[j],a[i]);
			ans = max(ans, p[j]%a[i]);
		}
	}

	tr(ans);

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<queue>
#include<stack>

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;

using namespace std;

int n, x, mx, v, vv, ans;
vector<int> a;
vector<int>::iterator it;

int main(){
	sd(n);
	for(int i = 0; i < n; i++){
		sd(x);
		a.pb(x);
		mx = max(mx,x);
	}

	sort(a.begin(), a.end());

	for(vector<int>::iterator j = a.begin(); j != a.end(); j++){
		it = j;
		vv = v = *j;
		vv += v;
		while(vv <= mx+v){
			it = lower_bound(it, a.end(), vv);
			it--;

//			tr2(*it, vv);

			ans = max(ans, *it%v);
			vv += v;
		}
	}

	tr(ans);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>

using namespace std;

#define Filesx

#define rdi(i)          scanf("%d", &i)
#define rdii(i, j)      scanf("%d%d", &i, &j)
#define rep(i, v)       for(int i=0;i<v.size();++i)
#define lp(i, n)        for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)    for(int i=(j);i<(int)(n);++i)
#define all(v)          ((v).begin()), ((v).end())
#define clr(v, d)       memset(v, d, sizeof(v))
#define lpd(i, j, n)    for(int i=(j);i>=(int)(n);--i)

typedef long long         ll;
typedef unsigned long long   ull;
const ll OO = 1e8;
const ll OOL = 1e18;
const ll MOD = 1e9 + 7;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

#define pb                  push_back
#define MP                  make_pair
typedef long double       ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef pair<int, int>    pii;
typedef pair<ll, ll>      pll;
typedef vector<pair<int, int> > vii;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>        vl;
typedef vector<vector<ll> >     vvl;
typedef vector<pair<ll, ll> >   vll;

ull gcd(ull a, ull b) { return (b == 0 ? a : gcd(b, a % b)); }
ull lcm(ull a, ull b) { return (a * (b / gcd(a, b))); }
ull power(ull a, ull b){ if (b == 0) return 1; ull ans = power(a, b / 2); ans = ans * ans; if (b % 2 != 0) ans *= a; return ans; }

vi a;
int vis[1001000];

int main() {
#ifdef Files
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	//ios::sync_with_stdio(false);

	int n; rdi(n);
	a.resize(n);
	lp(i, n) rdi(a[i]);
	sort(all(a));
	int maxi = 0;
	lp(i, n){
		if (a[i] == 1) continue;
		if (vis[i]) continue;
		vis[i] = 1;
		int mult = 2;
		while (a[i] * mult < a[n - 1] * 2){
			vis[a[i] * mult] = 1;
			vi::iterator it = lower_bound(all(a), a[i] * mult);
			if (it != a.begin()) it--;
			maxi = max(maxi, (*it) % a[i]);
			mult++;
		}
	}
	printf("%d\n", maxi);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>

using namespace std;

#define Filesx

#define rdi(i)          scanf("%d", &i)
#define rdii(i, j)      scanf("%d%d", &i, &j)
#define rep(i, v)       for(int i=0;i<v.size();++i)
#define lp(i, n)        for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)    for(int i=(j);i<(int)(n);++i)
#define all(v)          ((v).begin()), ((v).end())
#define clr(v, d)       memset(v, d, sizeof(v))
#define lpd(i, j, n)    for(int i=(j);i>=(int)(n);--i)

typedef long long         ll;
typedef unsigned long long   ull;
const ll OO = 1e8;
const ll OOL = 1e18;
const ll MOD = 1e9 + 7;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

#define pb                  push_back
#define MP                  make_pair
typedef long double       ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef pair<int, int>    pii;
typedef pair<ll, ll>      pll;
typedef vector<pair<int, int> > vii;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>        vl;
typedef vector<vector<ll> >     vvl;
typedef vector<pair<ll, ll> >   vll;

ull gcd(ull a, ull b) { return (b == 0 ? a : gcd(b, a % b)); }
ull lcm(ull a, ull b) { return (a * (b / gcd(a, b))); }
ull power(ull a, ull b){ if (b == 0) return 1; ull ans = power(a, b / 2); ans = ans * ans; if (b % 2 != 0) ans *= a; return ans; }

int a[200100];

int main() {
#ifdef Files
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	//ios::sync_with_stdio(false);

	int n; rdi(n);
	lp(i, n) rdi(a[i]);
	sort(a, a + n);
	int maxi = 0;
	vi facts;
	lp(i, n){
		if (a[i] == 1) continue;
		if (i && a[i] == a[i - 1]) continue;
		int add = 1;
		rep(j, facts){
			int num = facts[j];
			int mult = a[i] / num * num;
			maxi = max(maxi, a[i] - mult);
			if (a[i] % num == 0) add = 0;
		}
		if (add) facts.push_back(a[i]);
	}
	printf("%d\n", maxi);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>

using namespace std;

#define Filesx

#define rdi(i)          scanf("%d", &i)
#define rdii(i, j)      scanf("%d%d", &i, &j)
#define rep(i, v)       for(int i=0;i<v.size();++i)
#define lp(i, n)        for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)    for(int i=(j);i<(int)(n);++i)
#define all(v)          ((v).begin()), ((v).end())
#define clr(v, d)       memset(v, d, sizeof(v))
#define lpd(i, j, n)    for(int i=(j);i>=(int)(n);--i)

typedef long long         ll;
typedef unsigned long long   ull;
const ll OO = 1e8;
const ll OOL = 1e18;
const ll MOD = 1e9 + 7;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

#define pb                  push_back
#define MP                  make_pair
typedef long double       ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef pair<int, int>    pii;
typedef pair<ll, ll>      pll;
typedef vector<pair<int, int> > vii;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>        vl;
typedef vector<vector<ll> >     vvl;
typedef vector<pair<ll, ll> >   vll;

ull gcd(ull a, ull b) { return (b == 0 ? a : gcd(b, a % b)); }
ull lcm(ull a, ull b) { return (a * (b / gcd(a, b))); }
ull power(ull a, ull b){ if (b == 0) return 1; ull ans = power(a, b / 2); ans = ans * ans; if (b % 2 != 0) ans *= a; return ans; }

int a[200100];

int main() {
#ifdef Files
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	//ios::sync_with_stdio(false);

	int n; rdi(n);
	lp(i, n) rdi(a[i]);
	sort(a, a + n);
	int maxi = 0;
	set<int> st;
	lp(i, n){
		if (a[i] == 1) continue;
		if (i && a[i] == a[i - 1]) continue;
		int add = 1;
		for (set<int>::iterator it = st.begin(); it != st.end(); it++){
			int num = (*it);
			int mult = a[i] / num * num;
			maxi = max(maxi, a[i] - mult);
			if (a[i] % num == 0) add = 0;
		}
		if (add) st.insert(a[i]);
	}
	printf("%d\n", maxi);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>

using namespace std;

#define Filesx

#define rdi(i)          scanf("%d", &i)
#define rdii(i, j)      scanf("%d%d", &i, &j)
#define rep(i, v)       for(int i=0;i<v.size();++i)
#define lp(i, n)        for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)    for(int i=(j);i<(int)(n);++i)
#define all(v)          ((v).begin()), ((v).end())
#define clr(v, d)       memset(v, d, sizeof(v))
#define lpd(i, j, n)    for(int i=(j);i>=(int)(n);--i)

typedef long long         ll;
typedef unsigned long long   ull;
const ll OO = 1e8;
const ll OOL = 1e18;
const ll MOD = 1e9 + 7;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

#define pb                  push_back
#define MP                  make_pair
typedef long double       ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef pair<int, int>    pii;
typedef pair<ll, ll>      pll;
typedef vector<pair<int, int> > vii;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>        vl;
typedef vector<vector<ll> >     vvl;
typedef vector<pair<ll, ll> >   vll;

ull gcd(ull a, ull b) { return (b == 0 ? a : gcd(b, a % b)); }
ull lcm(ull a, ull b) { return (a * (b / gcd(a, b))); }
ull power(ull a, ull b){ if (b == 0) return 1; ull ans = power(a, b / 2); ans = ans * ans; if (b % 2 != 0) ans *= a; return ans; }

vi a;
int vis[10001000];

int main() {
#ifdef Files
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	//ios::sync_with_stdio(false);

	int n; rdi(n);
	a.resize(n);
	lp(i, n) rdi(a[i]);
	sort(all(a));
	int maxi = 0;
	lp(i, n){
		if (a[i] == 1) continue;
		if (vis[a[i]]) continue;
		vis[i] = 1;
		int mult = 2;
		while (a[i] * (mult - 1) < a[n - 1]){
			vis[a[i] * mult] = 1;
			vi::iterator it = lower_bound(all(a), a[i] * mult);
			it--;
			maxi = max(maxi, (*it) % a[i]);
			mult++;
		}
	}
	printf("%d\n", maxi);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>

using namespace std;

#define Filesx

#define rdi(i)          scanf("%d", &i)
#define rdii(i, j)      scanf("%d%d", &i, &j)
#define rep(i, v)       for(int i=0;i<v.size();++i)
#define lp(i, n)        for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)    for(int i=(j);i<(int)(n);++i)
#define all(v)          ((v).begin()), ((v).end())
#define clr(v, d)       memset(v, d, sizeof(v))
#define lpd(i, j, n)    for(int i=(j);i>=(int)(n);--i)

typedef long long         ll;
typedef unsigned long long   ull;
const ll OO = 1e8;
const ll OOL = 1e18;
const ll MOD = 1e9 + 7;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

#define pb                  push_back
#define MP                  make_pair
typedef long double       ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef pair<int, int>    pii;
typedef pair<ll, ll>      pll;
typedef vector<pair<int, int> > vii;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>        vl;
typedef vector<vector<ll> >     vvl;
typedef vector<pair<ll, ll> >   vll;

ull gcd(ull a, ull b) { return (b == 0 ? a : gcd(b, a % b)); }
ull lcm(ull a, ull b) { return (a * (b / gcd(a, b))); }
ull power(ull a, ull b){ if (b == 0) return 1; ull ans = power(a, b / 2); ans = ans * ans; if (b % 2 != 0) ans *= a; return ans; }

vi a;
int vis[10001000];

int main() {
#ifdef Files
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	//ios::sync_with_stdio(false);

	int n; rdi(n);
	a.resize(n);
	lp(i, n) rdi(a[i]);
	sort(all(a));
	int maxi = 0;
	lp(i, n){
		if (a[i] == 1) continue;
		if (vis[i]) continue;
		vis[i] = 1;
		int mult = 2;
		while (a[i] * (mult - 1) < a[n - 1]){
			vis[a[i] * mult] = 1;
			vi::iterator it = lower_bound(all(a), a[i] * mult);
			it--;
			maxi = max(maxi, (*it) % a[i]);
			mult++;
		}
	}
	printf("%d\n", maxi);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>

using namespace std;

#define Filesx

#define rdi(i)          scanf("%d", &i)
#define rdii(i, j)      scanf("%d%d", &i, &j)
#define rep(i, v)       for(int i=0;i<v.size();++i)
#define lp(i, n)        for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)    for(int i=(j);i<(int)(n);++i)
#define all(v)          ((v).begin()), ((v).end())
#define clr(v, d)       memset(v, d, sizeof(v))
#define lpd(i, j, n)    for(int i=(j);i>=(int)(n);--i)

typedef long long         ll;
typedef unsigned long long   ull;
const ll OO = 1e8;
const ll OOL = 1e18;
const ll MOD = 1e9 + 7;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

#define pb                  push_back
#define MP                  make_pair
typedef long double       ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef pair<int, int>    pii;
typedef pair<ll, ll>      pll;
typedef vector<pair<int, int> > vii;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>        vl;
typedef vector<vector<ll> >     vvl;
typedef vector<pair<ll, ll> >   vll;

ull gcd(ull a, ull b) { return (b == 0 ? a : gcd(b, a % b)); }
ull lcm(ull a, ull b) { return (a * (b / gcd(a, b))); }
ull power(ull a, ull b){ if (b == 0) return 1; ull ans = power(a, b / 2); ans = ans * ans; if (b % 2 != 0) ans *= a; return ans; }

int a[200100];

int main() {
#ifdef Files
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);

	int n; cin >> n;
	lp(i, n) cin >> a[i];
	sort(a, a + n);
	int maxi = 0;
	set<int> st;
	lp(i, n){
		if (a[i] == 1) continue;
		if (i && a[i] == a[i - 1]) continue;
		int add = 1;
		for (set<int>::iterator it = st.begin(); it != st.end(); it++){
			int num = (*it);
			int mult = a[i] / num * num;
			maxi = max(maxi, a[i] - mult);
			if (a[i] % num == 0) add = 0;
		}
		if (add) st.insert(a[i]);
	}
	cout << maxi << endl;

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>

using namespace std;

#define Filesx

#define rdi(i)          scanf("%d", &i)
#define rdii(i, j)      scanf("%d%d", &i, &j)
#define rep(i, v)       for(int i=0;i<v.size();++i)
#define lp(i, n)        for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)    for(int i=(j);i<(int)(n);++i)
#define all(v)          ((v).begin()), ((v).end())
#define clr(v, d)       memset(v, d, sizeof(v))
#define lpd(i, j, n)    for(int i=(j);i>=(int)(n);--i)

typedef long long         ll;
typedef unsigned long long   ull;
const ll OO = 1e8;
const ll OOL = 1e18;
const ll MOD = 1e9 + 7;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

#define pb                  push_back
#define MP                  make_pair
typedef long double       ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef pair<int, int>    pii;
typedef pair<ll, ll>      pll;
typedef vector<pair<int, int> > vii;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>        vl;
typedef vector<vector<ll> >     vvl;
typedef vector<pair<ll, ll> >   vll;

ull gcd(ull a, ull b) { return (b == 0 ? a : gcd(b, a % b)); }
ull lcm(ull a, ull b) { return (a * (b / gcd(a, b))); }
ull power(ull a, ull b){ if (b == 0) return 1; ull ans = power(a, b / 2); ans = ans * ans; if (b % 2 != 0) ans *= a; return ans; }

vi a;
int vis[10001000];

int main() {
#ifdef Files
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	//ios::sync_with_stdio(false);

	int n; rdi(n);
	a.resize(n);
	lp(i, n) rdi(a[i]);
	sort(all(a));
	int maxi = 0;
	lp(i, n){
		if (a[i] == 1) continue;
		if (vis[a[i]]) continue;
		vis[a[i]] = 1;
		int mult = 2;
		while (a[i] * (mult - 1) <= a[n - 1]){
			vis[a[i] * mult] = 1;
			vi::iterator it = lower_bound(all(a), a[i] * mult);
			it--;
			maxi = max(maxi, (*it) % a[i]);
			mult++;
		}
	}
	printf("%d\n", maxi);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>

using namespace std;

#define Filesx

#define rdi(i)          scanf("%d", &i)
#define rdii(i, j)      scanf("%d%d", &i, &j)
#define rep(i, v)       for(int i=0;i<v.size();++i)
#define lp(i, n)        for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)    for(int i=(j);i<(int)(n);++i)
#define all(v)          ((v).begin()), ((v).end())
#define clr(v, d)       memset(v, d, sizeof(v))
#define lpd(i, j, n)    for(int i=(j);i>=(int)(n);--i)

typedef long long         ll;
typedef unsigned long long   ull;
const ll OO = 1e8;
const ll OOL = 1e18;
const ll MOD = 1e9 + 7;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

#define pb                  push_back
#define MP                  make_pair
typedef long double       ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef pair<int, int>    pii;
typedef pair<ll, ll>      pll;
typedef vector<pair<int, int> > vii;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>        vl;
typedef vector<vector<ll> >     vvl;
typedef vector<pair<ll, ll> >   vll;

ull gcd(ull a, ull b) { return (b == 0 ? a : gcd(b, a % b)); }
ull lcm(ull a, ull b) { return (a * (b / gcd(a, b))); }
ull power(ull a, ull b){ if (b == 0) return 1; ull ans = power(a, b / 2); ans = ans * ans; if (b % 2 != 0) ans *= a; return ans; }

vi a;
int vis[1001000];

int main() {
#ifdef Files
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	//ios::sync_with_stdio(false);

	int n; rdi(n);
	a.resize(n);
	lp(i, n) rdi(a[i]);
	sort(all(a));
	int maxi = 0;
	lp(i, n){
		if (a[i] == 1) continue;
		if (vis[i]) continue;
		vis[i] = 1;
		int mult = 1;
		while (a[i] * mult < a[n - 1]){
			vis[a[i] * mult] = 1;
			vi::iterator it = lower_bound(all(a), a[i] * mult);
			if (it != a.begin()) it--;
			maxi = max(maxi, (*it) % a[i]);
			mult++;
		}
	}
	printf("%d\n", maxi);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>

using namespace std;

#define Filesx

#define rdi(i)          scanf("%d", &i)
#define rdii(i, j)      scanf("%d%d", &i, &j)
#define rep(i, v)       for(int i=0;i<v.size();++i)
#define lp(i, n)        for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)    for(int i=(j);i<(int)(n);++i)
#define all(v)          ((v).begin()), ((v).end())
#define clr(v, d)       memset(v, d, sizeof(v))
#define lpd(i, j, n)    for(int i=(j);i>=(int)(n);--i)

typedef long long         ll;
typedef unsigned long long   ull;
const ll OO = 1e8;
const ll OOL = 1e18;
const ll MOD = 1e9 + 7;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

#define pb                  push_back
#define MP                  make_pair
typedef long double       ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef pair<int, int>    pii;
typedef pair<ll, ll>      pll;
typedef vector<pair<int, int> > vii;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>        vl;
typedef vector<vector<ll> >     vvl;
typedef vector<pair<ll, ll> >   vll;

ull gcd(ull a, ull b) { return (b == 0 ? a : gcd(b, a % b)); }
ull lcm(ull a, ull b) { return (a * (b / gcd(a, b))); }
ull power(ull a, ull b){ if (b == 0) return 1; ull ans = power(a, b / 2); ans = ans * ans; if (b % 2 != 0) ans *= a; return ans; }

vi a;
int vis[2001000];

int main() {
#ifdef Files
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	//ios::sync_with_stdio(false);

	int n; rdi(n);
	a.resize(n);
	lp(i, n) rdi(a[i]);
	sort(all(a));
	int maxi = 0;
	lp(i, n){
		if (a[i] == 1) continue;
		if (vis[i]) continue;
		vis[i] = 1;
		int mult = 2;
		while (a[i] * mult < a[n - 1] * 2){
			vis[a[i] * mult] = 1;
			vi::iterator it = lower_bound(all(a), a[i] * mult);
			if (it != a.begin()) it--;
			maxi = max(maxi, (*it) % a[i]);
			mult++;
		}
	}
	printf("%d\n", maxi);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>

using namespace std;

#define Filesx

#define rdi(i)          scanf("%d", &i)
#define rdii(i, j)      scanf("%d%d", &i, &j)
#define rep(i, v)       for(int i=0;i<v.size();++i)
#define lp(i, n)        for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)    for(int i=(j);i<(int)(n);++i)
#define all(v)          ((v).begin()), ((v).end())
#define clr(v, d)       memset(v, d, sizeof(v))
#define lpd(i, j, n)    for(int i=(j);i>=(int)(n);--i)

typedef long long         ll;
typedef unsigned long long   ull;
const ll OO = 1e8;
const ll OOL = 1e18;
const ll MOD = 1e9 + 7;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

#define pb                  push_back
#define MP                  make_pair
typedef long double       ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef pair<int, int>    pii;
typedef pair<ll, ll>      pll;
typedef vector<pair<int, int> > vii;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>        vl;
typedef vector<vector<ll> >     vvl;
typedef vector<pair<ll, ll> >   vll;

ull gcd(ull a, ull b) { return (b == 0 ? a : gcd(b, a % b)); }
ull lcm(ull a, ull b) { return (a * (b / gcd(a, b))); }
ull power(ull a, ull b){ if (b == 0) return 1; ull ans = power(a, b / 2); ans = ans * ans; if (b % 2 != 0) ans *= a; return ans; }

vi a;
int vis[10001000];

int main() {
#ifdef Files
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	//ios::sync_with_stdio(false);

	int n; rdi(n);
	a.resize(n);
	lp(i, n) rdi(a[i]);
	sort(all(a));
	int maxi = 0;
	lp(i, n){
		if (a[i] == 1) continue;
		if (vis[a[i]]) continue;
		vis[a[i]] = 1;
		int mult = 2;
		int num = a[i];
		while (num * (mult - 1) <= a[n - 1]){
			vis[num * mult] = 1;
			vi::iterator it = lower_bound(all(a), num * mult);
			vi::iterator it2 = it;
			if(it != a.begin()) it--;
			maxi = max(maxi, (*it) % num);
			if (it2 != a.end() && (*it2) > num && (*it2) % num == 0) num = (*it2), mult = 1, vis[num] = 1;
			mult++;
		}
	}
	printf("%d\n", maxi);

	return 0;
}


#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>

using namespace std;

#define Filesx

#define rdi(i)          scanf("%d", &i)
#define rdii(i, j)      scanf("%d%d", &i, &j)
#define rep(i, v)       for(int i=0;i<v.size();++i)
#define lp(i, n)        for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)    for(int i=(j);i<(int)(n);++i)
#define all(v)          ((v).begin()), ((v).end())
#define clr(v, d)       memset(v, d, sizeof(v))
#define lpd(i, j, n)    for(int i=(j);i>=(int)(n);--i)

typedef long long         ll;
typedef unsigned long long   ull;
const ll OO = 1e8;
const ll OOL = 1e18;
const ll MOD = 1e9 + 7;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

#define pb                  push_back
#define MP                  make_pair
typedef long double       ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef pair<int, int>    pii;
typedef pair<ll, ll>      pll;
typedef vector<pair<int, int> > vii;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>        vl;
typedef vector<vector<ll> >     vvl;
typedef vector<pair<ll, ll> >   vll;

ull gcd(ull a, ull b) { return (b == 0 ? a : gcd(b, a % b)); }
ull lcm(ull a, ull b) { return (a * (b / gcd(a, b))); }
ull power(ull a, ull b){ if (b == 0) return 1; ull ans = power(a, b / 2); ans = ans * ans; if (b % 2 != 0) ans *= a; return ans; }

vi a;
int vis[10001000];

int main() {
#ifdef Files
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	//ios::sync_with_stdio(false);

	int n; rdi(n);
	a.resize(n);
	lp(i, n) rdi(a[i]);
	sort(all(a));
	int maxi = 0;
	lp(i, n){
		if (a[i] == 1) continue;
		if (vis[a[i]]) continue;
		vis[a[i]] = 1;
		int mult = 2;
		while (a[i] * (mult - 1) < a[n - 1]){
			vis[a[i] * mult] = 1;
			vi::iterator it = lower_bound(all(a), a[i] * mult);
			it--;
			maxi = max(maxi, (*it) % a[i]);
			mult++;
		}
	}
	printf("%d\n", maxi);

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n, x, y, ans;
set<int> s;
set<int>::iterator it, jt;

int main(int argc, char **argv) {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &x);
		s.insert(x);
	}
	int z = *--s.end();
	for(it = s.begin() ; it!=s.end() ; ++it){
		x = *it;
		for(int i = 2 ;  ; ++i){
			y = x*i;
			if(y > z+x) break;
			jt = s.lower_bound(y);
			--jt;
			ans = max(ans, *jt%x);
			if(ans == x-1)
				break;
		}
	}
	printf("%d", ans);
	return 0;
}


#include<iostream>
#include<set>

using namespace std;

int main()
{
	int n,x,z,max=0;
	cin>>n;
	set<int> s;
	for(int i=0 ; i<n ; i++){
		cin>>x;
		s.insert(x);
	}
	int size = s.size();
	int *a=new int[size];
	int y=0;
	for(set<int>::iterator i = s.begin() ; i!=s.end() ; i++){
		a[y++]=*i;
	}
	for(int i=size-1 ; i>0 ; i--)
		for(int j=i-1 ; j>=0 ; j--)
		{

			z = a[i]%a[j];
			if(z>max)
				max = z;
			if(max>(a[y-1]/2))
				break;
		}
	cout<<max<<endl;
	return 0;
}


#include<iostream>
#include<set>

using namespace std;

int main()
{
	int n,x,z,max=0;
	cin>>n;
	set<int> s;
	for(int i=0 ; i<n ; i++){
		cin>>x;
		s.insert(x);
	}
	int size = s.size();
	int *a=new int[size];
	int y=0;
	for(set<int>::iterator i = s.begin() ; i!=s.end() ; i++){
		a[y++]=*i;
	}
	for(int i=size-1 ; i>0 ; i--)
		for(int j=i-1 ; j>=0 ; j--)
		{

			z = a[i]%a[j];
			if(z>max)
				max = z;
			if(a[j]*2>a[y-1] && (max+1)>=(a[y-1]/2+a[y-1]%2))
				break;
		}
	cout<<max<<endl;
	return 0;
}


#include<iostream>
#include<set>

using namespace std;

int main()
{
	int n,x,z,max=0;
	cin>>n;
	set<int> s;
	for(int i=0 ; i<n ; i++){
		cin>>x;
		s.insert(x);
	}
	int size = s.size();
	int *a=new int[size];
	int y=0;
	for(set<int>::iterator i = s.begin() ; i!=s.end() ; i++){
		a[y++]=*i;
	}
	for(int i=size-1 ; i>0 ; i--)
		for(int j=i-1 ; j>=0 ; j--)
		{

			z = a[i]%a[j];
			if(z>max)
				max = z;
			if(a[i]*2>a[y-1] && (max+1)>=(a[y-1]/2+a[y-1]%2))
				break;
		}
	cout<<max<<endl;
	return 0;
}


#include<iostream>
#include<set>

using namespace std;

int main()
{
	int n,x,z,max=0;
	cin>>n;
	set<int> s;
	for(int i=0 ; i<n ; i++){
		cin>>x;
		s.insert(x);
	}
	int size = s.size();
	int *a=new int[size];
	int y=0;
	for(set<int>::iterator i = s.begin() ; i!=s.end() ; i++){
		a[y++]=*i;
	}
	for(int i=size-1 ; i>0 ; i--)
		for(int j=i-1 ; j>=0 ; j--)
		{

			z = a[i]%a[j];
			if(z>max)
				max = z;
			if(max+1>=(a[y-1]/2))
				break;
		}
	cout<<max<<endl;
	return 0;
}


#include<iostream>
#include<set>

using namespace std;

int main()
{
	int n,x,z,max=0;
	cin>>n;
	set<int> s;
	for(int i=0 ; i<n ; i++){
		cin>>x;
		s.insert(x);
	}
	int size = s.size();
	int *a=new int[size];
	int y=0;
	for(set<int>::iterator i = s.begin() ; i!=s.end() ; i++){
		a[y++]=*i;
	}
	for(int i=size-1 ; i>0 ; i--)
		for(int j=i-1 ; j>=0 ; j--)
		{

			z = a[i]%a[j];
			if(z>max)
				max = z;
			if(a[i]*2>a[y-1] && (max+1)>=a[y-1]/2)
				break;
		}
	cout<<max<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;

int main(){
	int n,x,i;
	cin>>n;
	int *a = new int[n];
	for(int i=0 ; i<n ; i++)
		cin>>a[i];
	sort(a,a+n);
	x = a[n-1]/2;
	for(i=0 ; i<n ; i++)
		if(a[i]>x)
			break;
	cout<<a[n-1]%a[i]<<endl;
    return 0;
}


#include<iostream>
#include<set>

using namespace std;

int main()
{
	int n,x,z,max=0;
	cin>>n;
	set<int> s;
	for(int i=0 ; i<n ; i++){
		cin>>x;
		s.insert(x);
	}
	int size = s.size();
	int *a=new int[size];
	int y=0;
	for(set<int>::iterator i = s.begin() ; i!=s.end() ; i++){
		a[y++]=*i;
	}
	for(int i=size-1 ; i>0 ; i--)
		for(int j=i-1 ; j>=0 ; j--)
		{
			z = a[i]%a[j];
			if(z>max)
				max = z;
		}
	cout<<max<<endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n, x, y, ans;
set<int> s;
set<int>::iterator it, jt;

int main(int argc, char **argv) {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &x);
		s.insert(x);
	}
	int z = *--s.end();
	for(it = s.begin() ; it!=s.end() ; ++it){
		x = *it;
		y = x;
		while(1){
			y += x;
			if(y > z+x) break;
			jt = s.lower_bound(y);
			--jt;
			ans = max(ans, *jt%x);
			if(ans == x-1)
				break;
		}
	}
	printf("%d", ans);
	return 0;
}


#include <iostream>
#include<algorithm>
using namespace std;

int main() {

	int x;
	cin>>x;
	int *a = new int[x];
	for(int i=0 ; i<x ; i++)
		cin>>a[i];
	sort(a,a+x);
	int max = 0;
	for(int i=x-1 ; i>0 ; i--)
		for(int j=i-1 ; j>=0 ; j--)
			if(a[i]%a[j]>max)
				max = a[i]%a[j];
	cout<<max<<endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n, x, y, ans;
set<int> s;
set<int>::iterator it, jt;

int main(int argc, char **argv) {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &x);
		s.insert(x);
	}
	bool close = 0;
	int z = *--s.end();
	for(it = s.begin() ; it!=s.end() ; ++it){
		x = *it;
		close = 0;
		for(int i = 2 ;  ; ++i){
			y = x*i;
			if(close) break;
			if(y > z) close = 1;
			jt = s.lower_bound(y);
			--jt;
			ans = max(ans, *jt%x);
			if(ans == x-1)
				break;
		}
	}
	printf("%d", ans);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n, x, y, ans;
set<int> s;
set<int>::iterator it, jt;

int main(int argc, char **argv) {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &x);
		s.insert(x);
	}
	int z = *--s.end();
	for(it = s.begin() ; it!=s.end() ; ++it){
		x = *it;
		for(int i = 2 ;  ; ++i){
			y = x*i;
			if(y > 2*z ) break;
			jt = s.lower_bound(y);
			--jt;
			ans = max(ans, *jt%x);
			if(ans == x-1)
				break;
		}
	}
	printf("%d", ans);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n, mx, a[200100];

int main(int argc, char **argv) {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", a + i);
	sort(a, a + n);
	for (int i = 0; i < n; ++i) {
		int idx = upper_bound(a, a + i, a[i] / 2) - a;
		if (idx < i)
			mx = max(mx, a[i] % a[idx]);
	}
	printf("%d\n", mx);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 100007;

int n;
int a[MAX_N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	int ans = 0, now = 0;
	for (int i = n - 1; i >= 0; --i) {
		int l = 0, r = 0;
		for (int j = 1; j <= 1000000 && a[i] * (j - 1) <= 1000000; ++j) {
			l = lower_bound(a, a + n, j * a[i]) - a;
		//	printf("%d %d %d\n", a[i], a[l - 1], l - 1);
			if (l > 0 && l > i) now = a[l - 1] % a[i];
			if (now > ans) ans = now;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 100007;

int n;
int a[MAX_N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	n = unique(a, a + n) - a;

	int ans = 0, now = 0;
	for (int i = n - 1; i >= 0; --i) {
		int l = 0, r = 0;
		for (int j = 1; j <= 1000000 && a[i] * j <= a[n - 1] + 100; ++j) {
			l = upper_bound(a, a + n, j * a[i]) - a;
			//printf("here %d %d\n", l, a[l]);
			if (l < n && l > 0 && l > i && a[l] != a[i] * j) ans = max(ans, a[l] % a[i]);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 200007;

int n;
int a[MAX_N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
		//a[i] = 1000000 - i;
	}
	sort(a, a + n);
	n = unique(a, a + n) - a;
   // printf("%d %d %d\n", n, a[0], a[n - 1]);
	int ans = 0, now = 0;
	for (int i = n - 1; i >= 0; --i) {
		int l = 0, r = 0;
		for (int j = 1; a[i] * j <= 1700000; ++j) {
			l = upper_bound(a, a + n, j * a[i]) - a;
			if (l >= n) l = n - 1;
		//	printf("here %d %d %d\n", i, l, a[l]);
			if (l - 1 > i && a[i] * j == a[l - 1]) ans = max(ans, a[l - 2] % a[i]);
			else if (l < n && l > 0 && l > i) ans = max(ans, a[l] % a[i]);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 200007;

int n;
int a[MAX_N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
		//a[i] = 1000000 - i;
	}
	sort(a, a + n);
	n = unique(a, a + n) - a;
   // printf("%d %d %d\n", n, a[0], a[n - 1]);
	int ans = 0, now = 0;
	for (int i = n - 1; i >= 0; --i) {
		int l = 0, r = 0;
		for (int j = 1; a[i] * j <= 1800000; ++j) {
			l = upper_bound(a, a + n, j * a[i]) - a;
			if (l >= n) l = n - 1;
		//	printf("here %d %d %d\n", i, l, a[l]);
			if (l - 2 > i && a[i] * j == a[l - 1]) ans = max(ans, a[l - 2] % a[i]);
			else if (l < n && l > i) ans = max(ans, a[l] % a[i]);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 100007;

int n;
int a[MAX_N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	n = unique(a, a + n) - a;

	int ans = 0, now = 0;
	for (int i = n - 1; i >= 0; --i) {
		int l = 0, r = 0;
		for (int j = 2; j <= 1000000 && a[i] * j <= 2000000; ++j) {
			l = lower_bound(a, a + n, j * a[i]) - a;
			if (l > 0 && l > i) ans = max(ans, a[l - 1] % a[i]);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1e9;
const double eps = 1e-6;
const int N = 300000;
const int M = 1000010;
int cas = 1;

int a[N];
bool vis[M];
int num[M];
int n,mxnum,ans;

void run()
{
    int x;
    mxnum=0;
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        if(mxnum<x) mxnum=x;
        vis[x]=1;
    }
    for(int i=1;i<=mxnum;i++)
    {
        if(vis[i]) num[i]=i;
        else num[i]=num[i-1];
    }
    ans=0;
    for(int aj=1;aj<=mxnum;aj++)
    {
        if(!vis[aj]) continue;
        if(ans<mxnum%aj) ans=mxnum%aj;
        for(int p=aj*2;p<=mxnum;p+=aj)
        {
            if(ans < num[p-1]%aj)
                ans = num[p-1]%aj;
        }
    }
    printf("%d\n",ans);
}

int main()
{
    #ifdef LOCAL
//    freopen("case.txt","r",stdin);
    #endif
    while(scanf("%d",&n)!=EOF)
        run();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 200007;

int n;
int a[MAX_N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		//scanf("%d", &a[i]);
		a[i] = i;
	}
	sort(a, a + n);
	n = unique(a, a + n) - a;

	int ans = 0, now = 0;
	for (int i = n - 1; i >= 0; --i) {
		int l = 0, r = 0;
		for (int j = 1; j <= 1000000 && a[i] * j <= a[n - 1] + 100; ++j) {
			l = upper_bound(a, a + n, j * a[i]) - a;
			if (l >= n) l = n - 1;
		//	printf("here %d %d %d\n", i, l, a[l]);
			if (l - 1 > i && a[i] * j == a[l - 1]) ans = max(ans, a[l - 2] % a[i]);
			else if (l < n && l > 0 && l > i) ans = max(ans, a[l] % a[i]);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 100007;

int n;
int a[MAX_N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	n = unique(a, a + n) - a;
	int ans = 0, now = 0;
	for (int i = n - 1; i >= 0; --i) {
		int l = 0, r = 0;
		for (int j = 1; j <= 1000000 && a[i] * (j - 1) <= 1000000; ++j) {
			l = lower_bound(a, a + n, j * a[i]) - a;
			if (l > 0 && l > i) now = a[l - 1] % a[i];
			if (now > ans) ans = now;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 100007;

int n;
int a[MAX_N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	n = unique(a, a + n) - a;

	int ans = 0, now = 0;
	for (int i = n - 1; i >= 0; --i) {
		int l = 0, r = 0;
		for (int j = 2; j <= 1000000 && a[i] * j <= 2500000; ++j) {
			l = lower_bound(a, a + n, j * a[i]) - a;
			if (l > 0 && l > i) ans = max(ans, a[l - 1] % a[i]);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 100007;

int n;
int a[MAX_N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	n = unique(a, a + n) - a;

	int ans = 0, now = 0;
	for (int i = n - 1; i >= 0; --i) {
		int l = 0, r = 0;
		for (int j = 1; j <= 1000000 && a[i] * j <= a[n - 1] + 100; ++j) {
			l = upper_bound(a, a + n, j * a[i]) - a;
			if (l >= n) l = n - 1;
		//	printf("here %d %d %d\n", i, l, a[l]);
			if (l - 1 > i && a[i] * j == a[l - 1]) ans = max(ans, a[l - 2] % a[i]);
			else if (l < n && l > 0 && l > i) ans = max(ans, a[l] % a[i]);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 200007;

int n;
int a[MAX_N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
		//a[i] = i;
	}
	sort(a, a + n);
	n = unique(a, a + n) - a;

	int ans = 0, now = 0;
	for (int i = n - 1; i >= 0; --i) {
		int l = 0, r = 0;
		for (int j = 1; j <= 1000000 && a[i] * j <= a[n - 1] + 100; ++j) {
			l = upper_bound(a, a + n, j * a[i]) - a;
			if (l >= n) l = n - 1;
		//	printf("here %d %d %d\n", i, l, a[l]);
			if (l - 1 > i && a[i] * j == a[l - 1]) ans = max(ans, a[l - 2] % a[i]);
			else if (l < n && l > 0 && l > i) ans = max(ans, a[l] % a[i]);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 200007;

int n;
int a[MAX_N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
		//a[i] = i;
	}
	sort(a, a + n);
	n = unique(a, a + n) - a;

	int ans = 0, now = 0;
	for (int i = n - 1; i >= 0; --i) {
		int l = 0, r = 0;
		for (int j = 1; j <= 1000000 && a[i] * j <= 1500000; ++j) {
			l = upper_bound(a, a + n, j * a[i]) - a;
			if (l >= n) l = n - 1;
		//	printf("here %d %d %d\n", i, l, a[l]);
			if (l - 1 > i && a[i] * j == a[l - 1]) ans = max(ans, a[l - 2] % a[i]);
			else if (l < n && l > 0 && l > i) ans = max(ans, a[l] % a[i]);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 100007;

int n;
int a[MAX_N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	n = unique(a, a + n) - a;

	int ans = 0, now = 0;
	for (int i = n - 1; i >= 0; --i) {
		int l = 0, r = 0;
		for (int j = 2; j <= 1000000 && a[i] * j <= 3000000; ++j) {
			l = lower_bound(a, a + n, j * a[i]) - a;
			if (l > 0 && l > i) ans = max(ans, a[l - 1] % a[i]);
		}
	}
	printf("%d\n", ans);
	return 0;
}


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;

typedef long long ll;

const int N = 200007;

#define Lson(x) (x<<1)
#define Rson(x) (x<<1|1)
#define L(x) tree[x].l
#define R(x) tree[x].r

int n, a[N];

int work(){
    int ans = 0;
    for(int i = n; i ; i--)
    {
        if(a[i] <= ans)break;
        for(int j = 1; j < a[i]; j++)
        {
            int tmp = a[i]/(j+1);
            while(a[i]/tmp > j)tmp++;
            if(tmp <= ans)break;
            int id = lower_bound(a+1, a+1+n, tmp) - a;
            if(id == i)continue;
            ans = max(ans, a[i]%a[id]);
            if(ans == a[i]-1)return ans;
        }
    }
    return ans;
}

int main(){
    while (cin>>n){
        for(int i = 1; i <= n; i++){
            cin>>a[i];
            if(a[i] == 1)n--, i--;
        }
        if(n == 0){puts("0"); continue;}
        sort(a+1, a+1+n);
        n = unique(a+1, a+n+1) - a-1;
   //     for(int i = 1; i <= n; i++)pt(a[i]),putchar(' ');puts("");
        cout<<work()<<endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 100007;

int n;
int a[MAX_N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	n = unique(a, a + n) - a;
	int ans = 0, now = 0;
	for (int i = n - 1; i >= 0; --i) {
		int l = 0, r = 0;
		for (int j = 1; j <= 1000000 && a[i] * (j - 1) <= 1000000; ++j) {
			l = lower_bound(a, a + n, j * a[i]) - a;
			if (now > ans) ans = now;
		}
	}
	printf("%d\n", ans);
	return 0;
}