#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define f first
#define s second
#define tup pair<pii,int>
#define ordk order_of_key
// const int M= 998244353;
const int M = 1e9+7;
const int MXN=2e6+10;

int power(int a,int b) {int res=1;a%=M;while(b){if((b&1))res=(res*a)%M;a=(a*a)%M;b>>=1;}return res;}
inline void print(int a,int b){ cout << a << ' ' << b << endl;}
inline void print(int a,int b,int c){ cout << a << ' ' << b <<' '<< c << ' ' << endl;}

int lb[MXN];

int32_t main(){
	fast;
	int n;
	cin>>n;
	vi a(n);
	for(int i=0;i<n;++i)
		cin>>a[i];
	sort(a.begin(),a.end());
	int ans = a[n-1]%a[0];
	for(int i=0;i<n;++i)
		lb[a[i]] = a[i];
	for(int i=1;i<2e6+1;++i)
		lb[i] = max(lb[i],lb[i-1]);

	for(int i=0;i<n;++i){
		for(int j=a[i]*2;j<=2*a[n-1];j+=a[i]){
			if(lb[j] >= a[i] && lb[j]%a[i] > ans)ans=lb[j]%a[i];
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define f first
#define s second
#define tup pair<pii,int>
#define ordk order_of_key
// const int M= 998244353;
const int M = 1e9+7;
const int MXN=2e6+10;

int power(int a,int b) {int res=1;a%=M;while(b){if((b&1))res=(res*a)%M;a=(a*a)%M;b>>=1;}return res;}
inline void print(int a,int b){ cout << a << ' ' << b << endl;}
inline void print(int a,int b,int c){ cout << a << ' ' << b <<' '<< c << ' ' << endl;}

int lb[MXN];
int vis[MXN];

int32_t main(){
	fast;
	int n;
	cin>>n;
	vi a(n);
	for(int i=0;i<n;++i)
		cin>>a[i];
	sort(a.begin(),a.end());
	int ans = a[n-1]%a[0];
	for(int i=0;i<n;++i)
		lb[a[i]] = a[i];
	for(int i=1;i<2e6+1;++i){
		vis[i]=0;
		lb[i] = max(lb[i],lb[i-1]);
	}
	int up = (a[n-1]<<1),d0;
	for(int i=0;i<n;++i){
		d0 = (a[i]<<1);
		for(int j=d0;j<=up;j+=a[i]){
			if(lb[j] >= a[i])ans=max(ans,lb[j]%a[i]);
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define f first
#define s second
#define tup pair<pii,int>
#define ordk order_of_key
// const int M= 998244353;
const int M = 1e9+7;
const int MXN=2e6+10;

int power(int a,int b) {int res=1;a%=M;while(b){if((b&1))res=(res*a)%M;a=(a*a)%M;b>>=1;}return res;}
inline void print(int a,int b){ cout << a << ' ' << b << endl;}
inline void print(int a,int b,int c){ cout << a << ' ' << b <<' '<< c << ' ' << endl;}

int lb[MXN];

int32_t main(){
	fast;
	int n;
	cin>>n;
	vi a(n);
	for(int i=0;i<n;++i)
		cin>>a[i];
	sort(a.begin(),a.end());
	int ans = a[n-1]%a[0];
	for(int i=0;i<n;++i)
		lb[a[i]] = a[i];
	for(int i=1;i<2e6+1;++i)
		lb[i] = max(lb[i],lb[i-1]);

	for(int i=0;i<n;++i){
		int cur = a[i];
		while(cur +a[i]<= 2*a[n-1]){
			cur+= a[i];
			int tmp = lb[cur];
			if(tmp >= a[i])
				ans = max(ans,tmp%a[i]);
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define f first
#define s second
#define tup pair<pii,int>
#define ordk order_of_key
// const int M= 998244353;
const int M = 1e9+7;
const int MXN=2e6+10;

int power(int a,int b) {int res=1;a%=M;while(b){if((b&1))res=(res*a)%M;a=(a*a)%M;b>>=1;}return res;}
inline void print(int a,int b){ cout << a << ' ' << b << endl;}
inline void print(int a,int b,int c){ cout << a << ' ' << b <<' '<< c << ' ' << endl;}

int lb[MXN];

int32_t main(){
	fast;
	int n;
	cin>>n;
	vi a(n);
	for(int i=0;i<n;++i)
		cin>>a[i];
	sort(a.begin(),a.end());
	int ans = a[n-1]%a[0];
	for(int i=0;i<n;++i)
		lb[a[i]] = a[i];
	for(int i=1;i<2e6+1;++i)
		lb[i] = max(lb[i],lb[i-1]);

	for(int i=0;i<n;++i){
		int cur = a[i];
		while(cur <= 2*a[n-1]){
			cur+= a[i];
			int tmp = lb[cur];
			if(tmp >= a[i])
				ans = max(ans,tmp%a[i]);
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define f first
#define s second
#define tup pair<pii,int>
#define ordk order_of_key
// const int M= 998244353;
const int M = 1e9+7;
const int MXN=2e6+10;

int power(int a,int b) {int res=1;a%=M;while(b){if((b&1))res=(res*a)%M;a=(a*a)%M;b>>=1;}return res;}
inline void print(int a,int b){ cout << a << ' ' << b << endl;}
inline void print(int a,int b,int c){ cout << a << ' ' << b <<' '<< c << ' ' << endl;}

int lb[MXN];
int vis[MXN];

int32_t main(){
	fast;
	int n;
	cin>>n;
	vi a,b(n);
	for(int i=0;i<n;++i)
		cin>>b[i];
	sort(b.begin(),b.end());
	a.pb(b[0]);
	for(int i=1;i<n;++i){
		if(a.back()!=b[i])a.pb(b[i]);
	}
	n=a.size();
	int ans = a[n-1]%a[0];
	for(int i=0;i<n;++i)
		lb[a[i]] = a[i];
	for(int i=1;i<2e6+1;++i){
		vis[i]=0;
		lb[i] = max(lb[i],lb[i-1]);
	}
	int up = (a[n-1]<<1),d0;
	for(int i=0;i<n;++i){
		d0 = (a[i]<<1);
		for(int j=d0;j<=up;j+=a[i]){
			if(lb[j] >= a[i])ans=max(ans,lb[j]%a[i]);
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define f first
#define s second
#define tup pair<pii,int>
#define ordk order_of_key
// const int M= 998244353;
const int M = 1e9+7;
const int MXN=2e6+10;

int power(int a,int b) {int res=1;a%=M;while(b){if((b&1))res=(res*a)%M;a=(a*a)%M;b>>=1;}return res;}
inline void print(int a,int b){ cout << a << ' ' << b << endl;}
inline void print(int a,int b,int c){ cout << a << ' ' << b <<' '<< c << ' ' << endl;}

int lb[MXN];
int vis[MXN];

int32_t main(){
	fast;
	int n;
	cin>>n;
	vi a,b(n);
	for(int i=0;i<n;++i)
		cin>>b[i];
	sort(b.begin(),b.end());
	a.pb(b[0]);
	for(int i=1;i<n;++i){
		if(a.back()!=b[i])a.pb(b[i]);
	}
	n=a.size();
	int ans = a[n-1]%a[0];
	for(int i=0;i<n;++i)
		lb[a[i]+1] = a[i];
	for(int i=1;i<2e6+1;++i){
		vis[i]=0;
		lb[i] = max(lb[i],lb[i-1]);
	}
	int up = (a[n-1]<<1),d0;
	for(int i=0;i<n;++i){
		d0 = (a[i]<<1);
		for(int j=d0;j<=up;j+=a[i]){
			if(lb[j] >= a[i])ans=max(ans,lb[j]%a[i]);
		}
	}

	cout << ans << endl;

	return 0;
}


/*
No matter how long a night, dawn will always break.

Simular saying ... The darkest hour is just before the dawn.
 ***************************************   ************************8   **********************************
*/
#include <bits/stdc++.h>
using namespace std;

#define int         long long
#define double      long double
#define pb          push_back
#define pf          push_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define endl        '\n'
#define sz(x)	    (int)(x).size()
#define fo(i,l,u)   for(i=l;i<u;i++)
#define rfo(i,l,u)  for(i=l;i>=u;i--)
#define allfo(s)    for(auto it=(s).begin();it!=(s).end();it++)
#define _init(b)     memset(b,-1,sizeof(b))
#define _init0(b)     memset(b,0,sizeof(b))
#define MOD         1000000007
#define hell        998244353
#define output(x) cout << (x ? "YES" : "NO")<<endl;

int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
bool mod(double a,double b) {return a/b - floor(a/b);}

 int last[1000001];

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
   int i,j,x,n;

	set<int,greater<int>> s;
	cin>>n;
   int ans=0;
   fo(i,0,n){
   	cin>>x;
   	s.insert(x);

   }
   vi v;
   int mx=0;
   allfo(s){
   	x=*it;
   	last[x]=x;
   	v.pb(x);
   	mx=max(mx,x);
   }

   fo(i,1,1000001){
   	last[i]=max(last[i],last[i-1]);
   }
   fo(i,0,v.size()){
   	x=v[i];
   	for(j=x+x;j<mx;j+=x){

   			ans=max(ans,last[j]%x);

   	}
   	if(last[mx-1]>x)
   	ans=max(ans,last[mx-1]%x);
   	ans=max(ans,mx%x);
   }
   cout<<ans;

    return 0;
}


/*
No matter how long a night, dawn will always break.

Simular saying ... The darkest hour is just before the dawn.
 ***************************************   ************************8   **********************************
*/
#include <bits/stdc++.h>
using namespace std;

#define int         long long
#define double      long double
#define pb          push_back
#define pf          push_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define endl        '\n'
#define sz(x)	    (int)(x).size()
#define fo(i,l,u)   for(i=l;i<u;i++)
#define rfo(i,l,u)  for(i=l;i>=u;i--)
#define allfo(s)    for(auto it=(s).begin();it!=(s).end();it++)
#define _init(b)     memset(b,-1,sizeof(b))
#define _init0(b)     memset(b,0,sizeof(b))
#define MOD         1000000007
#define hell        998244353
#define output(x) cout << (x ? "YES" : "NO")<<endl;

int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
bool mod(double a,double b) {return a/b - floor(a/b);}

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
   int i,j,x,n;

	set<int,greater<int>> s;
	cin>>n;
   int ans=0;
   fo(i,0,n){
   	cin>>x;
   	s.insert(x);

   }
   allfo(s){
   	x=*it;
   	for(j=x+x;j<=2000000;j+=x){
   		auto iit=s.upper_bound(j);
   		if(iit!=s.end()){
   			ans=max(ans,*iit%x);
   		}
   	}
   }
   cout<<ans;

    return 0;
}


/*
No matter how long a night, dawn will always break.

Simular saying ... The darkest hour is just before the dawn.
 ***************************************   ************************8   **********************************
*/
#include <bits/stdc++.h>
using namespace std;

#define int         long long
#define double      long double
#define pb          push_back
#define pf          push_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define endl        '\n'
#define sz(x)	    (int)(x).size()
#define fo(i,l,u)   for(i=l;i<u;i++)
#define rfo(i,l,u)  for(i=l;i>=u;i--)
#define allfo(s)    for(auto it=(s).begin();it!=(s).end();it++)
#define _init(b)     memset(b,-1,sizeof(b))
#define _init0(b)     memset(b,0,sizeof(b))
#define MOD         1000000007
#define hell        998244353
#define output(x) cout << (x ? "YES" : "NO")<<endl;

int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
bool mod(double a,double b) {return a/b - floor(a/b);}

 int last[1000001];

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
   int i,j,x,n;

	set<int,greater<int>> s;
	cin>>n;
   int ans=0;
   fo(i,0,n){
   	cin>>x;
   	s.insert(x);

   }
   vi v;
   int mx=0;
   allfo(s){
   	x=*it;
   	last[x]=x;
   	v.pb(x);
   	mx=max(mx,x);
   }

   fo(i,1,1000001){
   	last[i]=max(last[i],last[i-1]);
   }
   fo(i,0,v.size()){
   	x=v[i];
   	for(j=x+x;j<mx;j+=x){

   			ans=max(ans,last[j]%x);

   	}
   	ans=max(ans,last[mx-1]%x);
   	ans=max(ans,mx%x);
   }
   cout<<ans;

    return 0;
}


/*
No matter how long a night, dawn will always break.

Simular saying ... The darkest hour is just before the dawn.
 ***************************************   ************************8   **********************************
*/
#include <bits/stdc++.h>
using namespace std;

#define int         long long
#define double      long double
#define pb          push_back
#define pf          push_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define endl        '\n'
#define sz(x)	    (int)(x).size()
#define fo(i,l,u)   for(i=l;i<u;i++)
#define rfo(i,l,u)  for(i=l;i>=u;i--)
#define allfo(s)    for(auto it=(s).begin();it!=(s).end();it++)
#define _init(b)     memset(b,-1,sizeof(b))
#define _init0(b)     memset(b,0,sizeof(b))
#define MOD         1000000007
#define hell        998244353
#define output(x) cout << (x ? "YES" : "NO")<<endl;

int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
bool mod(double a,double b) {return a/b - floor(a/b);}

 int last[1000000];

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
   int i,j,x,n;

	set<int,greater<int>> s;
	cin>>n;
   int ans=0;
   fo(i,0,n){
   	cin>>x;
   	s.insert(x);

   }
   vi v;
   int mx=0;
   allfo(s){
   	x=*it;
   	last[x]=x;
   	v.pb(x);
   	mx=max(mx,x);
   }

   fo(i,1,1000001){
   	last[i]=max(last[i],last[i-1]);
   }
   fo(i,0,v.size()){
   	x=v[i];
   	for(j=x+x;j<mx;j+=x){

   			ans=max(ans,last[j]%x);

   	}
   	ans=max(ans,last[mx]%x);
   }
   cout<<ans;

    return 0;
}


/*
No matter how long a night, dawn will always break.

Simular saying ... The darkest hour is just before the dawn.
 ***************************************   ************************8   **********************************
*/
#include <bits/stdc++.h>
using namespace std;

#define int         long long
#define double      long double
#define pb          push_back
#define pf          push_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define endl        '\n'
#define sz(x)	    (int)(x).size()
#define fo(i,l,u)   for(i=l;i<u;i++)
#define rfo(i,l,u)  for(i=l;i>=u;i--)
#define allfo(s)    for(auto it=(s).begin();it!=(s).end();it++)
#define _init(b)     memset(b,-1,sizeof(b))
#define _init0(b)     memset(b,0,sizeof(b))
#define MOD         1000000007
#define hell        998244353
#define output(x) cout << (x ? "YES" : "NO")<<endl;

int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
bool mod(double a,double b) {return a/b - floor(a/b);}

 int last[1000001];

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
   int i,j,x,n;

	set<int,greater<int>> s;
	cin>>n;
   int ans=0;
   fo(i,0,n){
   	cin>>x;
   	s.insert(x);

   }
   vi v;
   int mx=0;
   allfo(s){
   	x=*it;
   	last[x]=x;
   	v.pb(x);
   	mx=max(mx,x);
   }

   fo(i,1,1000001){
   	last[i]=max(last[i],last[i-1]);
   }
   fo(i,0,v.size()){
   	x=v[i];
   	for(j=x+x;j<mx;j+=x){

   			ans=max(ans,last[j]%x);

   	}
   	ans=max(ans,last[mx]%x);
   }
   cout<<ans;

    return 0;
}


/*
No matter how long a night, dawn will always break.

Simular saying ... The darkest hour is just before the dawn.
 ***************************************   ************************8   **********************************
*/
#include <bits/stdc++.h>
using namespace std;

#define int         long long
#define double      long double
#define pb          push_back
#define pf          push_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define endl        '\n'
#define sz(x)	    (int)(x).size()
#define fo(i,l,u)   for(i=l;i<u;i++)
#define rfo(i,l,u)  for(i=l;i>=u;i--)
#define allfo(s)    for(auto it=(s).begin();it!=(s).end();it++)
#define _init(b)     memset(b,-1,sizeof(b))
#define _init0(b)     memset(b,0,sizeof(b))
#define MOD         1000000007
#define hell        998244353
#define output(x) cout << (x ? "YES" : "NO")<<endl;

int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
bool mod(double a,double b) {return a/b - floor(a/b);}

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
   int i,j,x,n;

	set<int,greater<int>> s;
	cin>>n;
   int ans=0;
   fo(i,0,n){
   	cin>>x;
   	s.insert(x);

   }
   allfo(s){
   	x=*it;
   	for(j=x+x;j<=1000000;j+=x){
   		auto iit=s.upper_bound(j);
   		if(iit!=s.end()){
   			ans=max(ans,*iit%x);
   		}
   	}
   }
   cout<<ans;

    return 0;
}


/*
No matter how long a night, dawn will always break.

Simular saying ... The darkest hour is just before the dawn.
 ***************************************   ************************8   **********************************
*/
#include <bits/stdc++.h>
using namespace std;

#define int         long long
#define double      long double
#define pb          push_back
#define pf          push_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define endl        '\n'
#define sz(x)	    (int)(x).size()
#define fo(i,l,u)   for(i=l;i<u;i++)
#define rfo(i,l,u)  for(i=l;i>=u;i--)
#define allfo(s)    for(auto it=(s).begin();it!=(s).end();it++)
#define _init(b)     memset(b,-1,sizeof(b))
#define _init0(b)     memset(b,0,sizeof(b))
#define MOD         1000000007
#define hell        998244353
#define output(x) cout << (x ? "YES" : "NO")<<endl;

int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
bool mod(double a,double b) {return a/b - floor(a/b);}

 int last[2000001];

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
   int i,j,x,n;

	set<int,greater<int>> s;
	cin>>n;
   int ans=0;
   fo(i,0,n){
   	cin>>x;
   	s.insert(x);

   }
   vi v;
   int mx=0;
   allfo(s){
   	x=*it;
   	last[x+1]=x;
   	v.pb(x);
   	mx=max(mx,x);
   }

   fo(i,1,2000001){
   	last[i]=max(last[i],last[i-1]);
   }
   fo(i,0,v.size()){
   	x=v[i];
   	for(j=x+x;j<2000001;j+=x){

   			ans=max(ans,last[j]%x);

   	}
   	// if(last[mx-1]>x)
   	// ans=max(ans,last[mx-1]%x);
   	// ans=max(ans,mx%x);
   }
   cout<<ans;

    return 0;
}


/*
No matter how long a night, dawn will always break.

Simular saying ... The darkest hour is just before the dawn.
 ***************************************   ************************8   **********************************
*/
#include <bits/stdc++.h>
using namespace std;

#define int         long long
#define double      long double
#define pb          push_back
#define pf          push_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define endl        '\n'
#define sz(x)	    (int)(x).size()
#define fo(i,l,u)   for(i=l;i<u;i++)
#define rfo(i,l,u)  for(i=l;i>=u;i--)
#define allfo(s)    for(auto it=(s).begin();it!=(s).end();it++)
#define _init(b)     memset(b,-1,sizeof(b))
#define _init0(b)     memset(b,0,sizeof(b))
#define MOD         1000000007
#define hell        998244353
#define output(x) cout << (x ? "YES" : "NO")<<endl;

int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
bool mod(double a,double b) {return a/b - floor(a/b);}

 int last[2000001];

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
   int i,j,x,n;

	set<int,greater<int>> s;
	cin>>n;
   int ans=0;
   fo(i,0,n){
   	cin>>x;
   	s.insert(x);

   }
   vi v;
   int mx=0;
   allfo(s){
   	x=*it;
   	last[x]=x;
   	v.pb(x);
   	mx=max(mx,x);
   }

   fo(i,1,2000001){
   	last[i]=max(last[i],last[i-1]);
   }
   fo(i,0,v.size()){
   	x=v[i];
   	for(j=x+x;j<2000001;j+=x){

   			ans=max(ans,last[j]%x);

   	}
   	// if(last[mx-1]>x)
   	// ans=max(ans,last[mx-1]%x);
   	// ans=max(ans,mx%x);
   }
   cout<<ans;

    return 0;
}


/*
No matter how long a night, dawn will always break.

Simular saying ... The darkest hour is just before the dawn.
 ***************************************   ************************8   **********************************
*/
#include <bits/stdc++.h>
using namespace std;

#define int         long long
#define double      long double
#define pb          push_back
#define pf          push_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define endl        '\n'
#define sz(x)	    (int)(x).size()
#define fo(i,l,u)   for(i=l;i<u;i++)
#define rfo(i,l,u)  for(i=l;i>=u;i--)
#define allfo(s)    for(auto it=(s).begin();it!=(s).end();it++)
#define _init(b)     memset(b,-1,sizeof(b))
#define _init0(b)     memset(b,0,sizeof(b))
#define MOD         1000000007
#define hell        998244353
#define output(x) cout << (x ? "YES" : "NO")<<endl;

int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
bool mod(double a,double b) {return a/b - floor(a/b);}

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
   int i,j,x,n;

	set<int,greater<int>> s;
	cin>>n;
   int ans=0;
   fo(i,0,n){
   	cin>>x;
   	s.insert(x);

   }
   allfo(s){
   	x=*it;
   	for(j=x+x;j<=110000;j+=x){
   		auto iit=s.upper_bound(j);
   		if(iit!=s.end()){
   			ans=max(ans,*iit%x);
   		}
   	}
   }
   cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    const int MAXN = 1e6;
    int n;
    cin>>n;
    bool a[2*MAXN+1];
    int cnt[2*MAXN+1];
    memset(a,false,sizeof(a));
    for(int i=0;i<n;i++){
        int val;
        cin>>val;
        a[val] = true;
    }
    cnt[1] = 0;
    for(int i=2;i<=2*MAXN;i++){
        if(a[i-1]) cnt[i] = i-1;
        else cnt[i] = cnt[i-1];
    }
    int ans = 0;
    for(int i=1;i<=MAXN;i++){
        if(!a[i]) continue;
        for(int j=2*i;j<=2*MAXN;j+=i){
            ans = max(ans,cnt[j]%i);
        }
    }
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    const int MAXN = 1e5;
    int n;
    cin>>n;
    int a[n], mx = 0;
    int cnt[MAXN+1];
    memset(cnt,-1,sizeof(cnt));
    for(int i=0;i<n;i++){
        cin>>a[i];
        cnt[a[i]+1] = a[i];
        mx = max(a[i],mx);
    }
    for(int i=MAXN-1;i>=0;i--){
        if(cnt[i] == -1){
            cnt[i] = cnt[i+1];
        }
    }
    for(int i=mx+2;i<=MAXN;i++){
        cnt[i] = cnt[i-1];
    }
    int ans = 0;
    for(int i=0;i<n;i++){
        for(int j=2*a[i];j<=2*mx;j+=a[i]){
            int x = cnt[j];
            //cout<<x<<" "<<a[i]<<"\n";
            ans = max(ans,x%a[i]);
        }
    }
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    const int MAXN = 1e6;
    int n;
    cin>>n;
    bool a[MAXN+1];
    int cnt[2*MAXN+10], mx = 0;
    memset(a,false,sizeof(a));
    memset(cnt,-1,sizeof(cnt));
    for(int i=0;i<n;i++){
        int val;
        cin>>val;
        a[val] = true;
        cnt[val+1] = val;
        mx = max(val,mx);
    }
    for(int i=2*MAXN+8;i>=0;i--){
        if(cnt[i] == -1){
            cnt[i] = cnt[i+1];
        }
    }
    for(int i=mx+2;i<=2*MAXN+9;i++){
        cnt[i] = cnt[i-1];
    }
    int ans = 0;
    for(int i=1;i<=MAXN;i++){
        if(!a[i]) continue;
        for(int j=2*i;j<=2*MAXN;j+=i){
            int x = cnt[j];
            ans = max(ans,x%i);
        }
    }
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    const int MAXN = 1e6;
    int n;
    cin>>n;
    int a[n], mx = 0;
    int cnt[MAXN+1];
    memset(cnt,-1,sizeof(cnt));
    for(int i=0;i<n;i++){
        cin>>a[i];
        cnt[a[i]+1] = a[i];
        mx = max(a[i],mx);
    }
    for(int i=MAXN-1;i>=0;i--){
        if(cnt[i] == -1){
            cnt[i] = cnt[i+1];
        }
    }
    for(int i=mx+2;i<=MAXN;i++){
        cnt[i] = cnt[i-1];
    }
    int ans = 0;
    for(int i=0;i<n;i++){
        for(int j=2*a[i];j<=2*mx;j+=a[i]){
            int x = cnt[j];
            //cout<<x<<" "<<a[i]<<"\n";
            ans = max(ans,x%a[i]);
        }
    }
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    const int MAXN = 1e6;
    int n;
    cin>>n;
    bool a[MAXN+1];
    int cnt[2*MAXN+10], mx = 0;
    memset(a,false,sizeof(a));
    memset(cnt,-1,sizeof(cnt));
    for(int i=0;i<n;i++){
        int val;
        cin>>val;
        a[val] = true;
        cnt[val+1] = val;
        mx = max(val,mx);
    }
    for(int i=2*MAXN+8;i>=0;i--){
        if(cnt[i] == -1){
            cnt[i] = cnt[i+1];
        }
    }
    for(int i=mx+2;i<=2*MAXN+9;i++){
        cnt[i] = cnt[i-1];
    }
    int ans = 0;
    for(int i=1;i<=MAXN;i++){
        if(!a[i]) continue;
        for(int j=2*i;j<=2*MAXN;j+=i){
            int x = cnt[j];
            ans = max(ans,x%i);
            if(x == mx) break;
        }
    }
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    const int MAXN = 1e6;
    int n;
    cin>>n;
    int a[n], mx = 0;
    int cnt[2*MAXN+10];
    memset(cnt,-1,sizeof(cnt));
    for(int i=0;i<n;i++){
        cin>>a[i];
        cnt[a[i]+1] = a[i];
        mx = max(a[i],mx);
    }
    for(int i=2*MAXN+9;i>=0;i--){
        if(cnt[i] == -1){
            cnt[i] = cnt[i+1];
        }
    }
    for(int i=mx+2;i<=2*MAXN+9;i++){
        cnt[i] = cnt[i-1];
    }
    int ans = 0;
    for(int i=0;i<n;i++){
        for(int j=2*a[i];j<=2*mx;j+=a[i]){
            int x = cnt[j];
            //cout<<x<<" "<<a[i]<<"\n";
            ans = max(ans,x%a[i]);
        }
    }
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    const int MAXN = 1e6;
    int n;
    cin>>n;
    int a[n], mx = 0;
    int cnt[2*MAXN+10];
    memset(cnt,-1,sizeof(cnt));
    for(int i=0;i<n;i++){
        cin>>a[i];
        cnt[a[i]+1] = a[i];
        mx = max(a[i],mx);
    }
    for(int i=2*MAXN+9;i>=0;i--){
        if(cnt[i] == -1){
            cnt[i] = cnt[i+1];
        }
    }
    for(int i=mx+2;i<=2*MAXN+9;i++){
        cnt[i] = cnt[i-1];
    }
    int ans = 0;
    for(int i=0;i<n;i++){
        for(int j=2*a[i];j<=2*mx;j+=a[i]){
            int x = cnt[j];
            //cout<<x<<" "<<a[i]<<"\n";
            ans = max(ans,x%a[i]);
            if(cnt[j] == mx) break;
        }
    }
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    const int MAXN = 1e6;
    int n;
    cin>>n;
    int a[n], mx = 0;
    int cnt[MAXN+10];
    memset(cnt,-1,sizeof(cnt));
    for(int i=0;i<n;i++){
        cin>>a[i];
        cnt[a[i]+1] = a[i];
        mx = max(a[i],mx);
    }
    for(int i=MAXN+8;i>=0;i--){
        if(cnt[i] == -1){
            cnt[i] = cnt[i+1];
        }
    }
    for(int i=mx+2;i<=MAXN+9;i++){
        cnt[i] = cnt[i-1];
    }
    int ans = 0;
    for(int i=0;i<n;i++){
        for(int j=2*a[i];j<=2*mx;j+=a[i]){
            int x = cnt[j];
            //cout<<x<<" "<<a[i]<<"\n";
            ans = max(ans,x%a[i]);
        }
    }
    cout<<ans<<"\n";
    return 0;
}


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

int n;
int a[200005];
int cnt[1000001];
int lo[1000001];

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

	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		cnt[a[i]]++;
	}

	int temp = 1;
	int curr = 1;
	while(temp != n + 1) {
		while(cnt[curr] != 0) {
			a[temp] = curr;
			cnt[curr]--;
			temp++;
		}
		curr++;
	}

	curr = n;
	for(int i = 1000000; i >= 1; i--) {
		if(a[curr] >= i) {
			curr--;
		}

		lo[i] = a[curr];
	}

	int maxi = a[n - 1];
	int ans = 0;

	for(int i = 1; i < n; i++) {
		for(int j = 2 * a[i]; j <= maxi + a[i]; j += a[i]) {
			int now = lo[j];
			ans = max(ans, now % a[i]);
		}
	}

	cout << ans << endl;
}


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

int n;
int a[200005];

int binser(int left, int right, int target) {
	while(left < right) {
		int mid = (left + right + 1) / 2;

		if(a[mid] < target) left = mid;
		else right = mid - 1;
	}

	return left;
}

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

	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a, a + n);

	int maxi = a[n - 1] * 2;
	int ans = 0;

	for(int i = 0; i < n - 1; i++) {
		for(int j = 2 * a[i]; j <= maxi; j += a[i]) {
			int idx = binser(0, n - 1, j);
			ans = max(ans, a[idx] % a[i]);
		}
	}

	cout << ans << endl;
}


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

int n;
int a[200005];
int cnt[1000001];

int binser(int left, int right, int target) {
	while(left < right) {
		int mid = (left + right + 1) / 2;

		if(a[mid] < target) left = mid;
		else right = mid - 1;
	}

	return left;
}

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

	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> a[i];
		cnt[a[i]]++;
	}

	int temp = 0;
	int curr = 1;
	while(temp != n) {
		while(cnt[curr] != 0) {
			a[temp] = curr;
			cnt[curr]--;
			temp++;
		}
		curr++;
	}

	int maxi = a[n - 1] * 2;
	int ans = 0;

	for(int i = 0; i < n - 1; i++) {
		for(int j = 2 * a[i]; j <= maxi; j += a[i]) {
			int idx = binser(0, n - 1, j);
			ans = max(ans, a[idx] % a[i]);
		}
	}

	cout << ans << endl;
}


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

int n;
int a[200005];

int binser(int left, int right, int target) {
	while(left < right) {
		int mid = (left + right + 1) / 2;

		if(a[mid] <= target) left = mid;
		else right = mid - 1;
	}

	return left;
}

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

	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a, a + n);

	int maxi = a[n - 1] * 2;
	int ans = -1;

	for(int i = 0; i < n; i++) {
		for(int j = 2 * a[i]; j <= maxi; j += a[i]) {
			int idx = binser(0, n - 1, j);
			ans = max(ans, a[idx] % a[i]);
		}
	}

	cout << ans << endl;
}


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

int n;
int a[200005];
int cnt[1000001];

int binser(int left, int right, int target) {
	while(left < right) {
		int mid = (left + right + 1) / 2;

		if(a[mid] < target) left = mid;
		else right = mid - 1;
	}

	return left;
}

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

	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> a[i];
		cnt[a[i]]++;
	}

	int temp = 0;
	int curr = 1;
	while(temp != n) {
		while(cnt[curr] != 0) {
			a[temp] = curr;
			cnt[curr]--;
			temp++;
		}
		curr++;
	}

	int maxi = a[n - 1];
	int ans = 0;

	for(int i = 0; i < n - 1; i++) {
		for(int j = 2 * a[i]; j <= maxi + a[i]; j += a[i]) {
			int idx = binser(0, n - 1, j);
			ans = max(ans, a[idx] % a[i]);
		}
	}

	cout << ans << endl;
}


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

int n;
int a[200005];
int cnt[1000001];
int lo[1000001];

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

	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		cnt[a[i]]++;
	}

	int temp = 1;
	int curr = 1;
	while(temp != n + 1) {
		while(cnt[curr] != 0) {
			a[temp] = curr;
			cnt[curr]--;
			temp++;
		}
		curr++;
	}

	curr = n;
	for(int i = 2000000; i >= 1; i--) {
		if(a[curr] > i) {
			curr--;
		}

		lo[i] = a[curr];
	}

	int maxi = a[n];
	int ans = 0;

	for(int i = 1; i < n; i++) {
		for(int j = 2 * a[i]; j <= maxi + a[i]; j += a[i]) {
			int now = lo[j];
			ans = max(ans, now % a[i]);
		}
	}

	cout << ans << endl;
}


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

int n;
int a[200005];

int binser(int left, int right, int target) {
	while(left < right) {
		int mid = (left + right + 1) / 2;

		if(a[mid] < target) left = mid;
		else right = mid - 1;
	}

	return left;
}

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

	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a, a + n);

	int maxi = a[n - 1] * 2;
	int ans = -1;

	for(int i = 0; i < n - 1; i++) {
		for(int j = 2 * a[i]; j <= maxi; j += a[i]) {
			int idx = binser(0, n - 1, j);
			ans = max(ans, a[idx] % a[i]);
		}
	}

	cout << ans << endl;
}


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

int n;
int a[200005];
int cnt[1000001];
int lo[1000001];

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

	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		cnt[a[i]]++;
	}

	int temp = 1;
	int curr = 1;
	while(temp != n + 1) {
		while(cnt[curr] != 0) {
			a[temp] = curr;
			cnt[curr]--;
			temp++;
		}
		curr++;
	}

	curr = n;
	for(int i = 2000000; i >= 1; i--) {
		if(a[curr] > i) {
			curr--;
		}

		lo[i] = a[curr];
	}

	int maxi = a[n - 1];
	int ans = 0;

	for(int i = 1; i < n; i++) {
		for(int j = 2 * a[i]; j <= maxi + a[i]; j += a[i]) {
			int now = lo[j];
			ans = max(ans, now % a[i]);
		}
	}

	cout << ans << endl;
}


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

int n;
int a[200005];
int maxi;
int ans = 0;

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

	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> a[i];
	}

	sort(a, a + n);
	maxi = a[n - 1];

	for(int i = 0; i < n; i++) {
		if(i != 0 and a[i] == a[i - 1]) continue;

		int temp = a[i] * 2;
		int up = maxi / a[i];
		up = (up + 1) * a[i];

		while(temp <= up) {
			int idx = lower_bound(a, a + n, temp) - a;

			if(idx > 0) {
				ans = max(ans, a[idx - 1] % a[i]);
			}

			temp += a[i];
		}
	}

	cout << ans << endl;
}


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

int n;
int a[200005];
int cnt[1000001];
int lo[2000001];

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

	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		cnt[a[i]]++;
	}

	int temp = 1;
	int curr = 1;
	while(temp != n + 1) {
		while(cnt[curr] != 0) {
			a[temp] = curr;
			cnt[curr]--;
			temp++;
		}
		curr++;
	}

	curr = n;
	for(int i = 2000000; i >= 1; i--) {
		if(a[curr] > i) {
			curr--;
		}

		lo[i] = a[curr];
	}

	int maxi = a[n - 1];
	int ans = 0;

	for(int i = 1; i < n; i++) {
		for(int j = 2 * a[i]; j <= maxi + a[i]; j += a[i]) {
			int now = lo[j];
			ans = max(ans, now % a[i]);
		}
	}

	cout << ans << endl;
}


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

int n;
int a[200005];
int cnt[1000001];
int lo[2000001];

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

	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		cnt[a[i]]++;
	}

	int temp = 1;
	int curr = 1;
	while(temp != n + 1) {
		while(cnt[curr] != 0) {
			a[temp] = curr;
			cnt[curr]--;
			temp++;
		}
		curr++;
	}

	curr = n;
	for(int i = 2000000; i >= 1; i--) {
		if(a[curr] > i) {
			curr--;
		}

		lo[i] = a[curr];
	}

	int maxi = a[n];
	int ans = 0;

	for(int i = 1; i < n; i++) {
		for(int j = 2 * a[i]; j <= maxi + a[i]; j += a[i]) {
			int now = lo[j];
			ans = max(ans, now % a[i]);
		}
	}

	cout << ans << endl;
}


//Apparat : drobucs, aleonov, vit_72
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <iomanip>
#include <algorithm>
#include <string>
#include <queue>
#include <numeric>

//#pragma warning(disable : 4996)
//#pragma GCC optimize("O3")

using namespace std;

#define nptr nullptr
#define all(x) (x).begin(), (x).end()
#define drobucs ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define ff first
#define ss second
#define pb push_back
#define sz(x) (int)(x).size()

typedef  long long ll;

const int maxn = (int)2e5 + 13;
const ll INFL = 9223372036854775807;
const ll INF = 2147483647;
const ll MOD = (int)1e9 + 7, MOD2 = 998244353;//1e9 + 21, 1e9 + 33, 1e9 + 87, 1e9 + 93
const ll base = 101, base2 = 37;
const long double pi = 3.1415926535897932;

int a[maxn];

int main() {
	drobucs;

	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i)cin >> a[i];
	sort(a, a + n + 1);
	int ans = 0;
	for (int i = 1; i <= n - 1; ++i) {
		int x = 2 * a[i];
		while (x <= 2 * a[n]) {
			int l = 0, r = n + 1;
			while (r - l > 1) {
				int m = (l + r) / 2;
				if (a[m] >= x)r = m;
				else l = m;
			}
			ans = max(a[l] % a[i], ans);
			x += a[i];
		}
	}
	cout << ans << "\n";
	return 0;
}


//Apparat : drobucs, aleonov, vit_72
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <iomanip>
#include <algorithm>
#include <string>
#include <queue>
#include <numeric>

//#pragma warning(disable : 4996)
//#pragma GCC optimize("O3")

using namespace std;

#define nptr nullptr
#define all(x) (x).begin(), (x).end()
#define drobucs ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define ff first
#define ss second
#define pb push_back
#define sz(x) (int)(x).size()

typedef  long long ll;

const int maxn = (int)1e6 + 13;
const ll INFL = 9223372036854775807;
const ll INF = 2147483647;
const ll MOD = (int)1e9 + 7, MOD2 = 998244353;//1e9 + 21, 1e9 + 33, 1e9 + 87, 1e9 + 93
const ll base = 101, base2 = 37;
const long double pi = 3.1415926535897932;

int a[maxn];
int f[maxn];
int main() {
	drobucs;

	int n, it = 0, ans = 0;
	cin >> n;
	set<int> st;
	for (int i = 1; i <= n; ++i)cin >> a[i], st.insert(a[i]);
	for (auto t : st)a[++it] = t;
	n = it;
	for (int i = 1; i <= 2 * a[n]; ++i) {
		int r = n + 1;
		while (r - f[i] > 1) {
			int m = (f[i] + r) / 2;
			if (a[m] >= i) r = m;
			else f[i] = m;
		}
	}
	for (int i = 1; i <= n - 1; ++i) {
		int x = 2 * a[i];
		while (x <= 2 * a[n]) {
			ans = max(a[f[x]] % a[i], ans);
			x += a[i];
		}
	}
	cout << ans << "\n";
	return 0;
}


//Apparat : drobucs, aleonov, vit_72
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <iomanip>
#include <algorithm>
#include <string>
#include <queue>
#include <numeric>

//#pragma warning(disable : 4996)
//#pragma GCC optimize("O3")

using namespace std;

#define nptr nullptr
#define all(x) (x).begin(), (x).end()
#define drobucs ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define ff first
#define ss second
#define pb push_back
#define sz(x) (int)(x).size()

typedef  long long ll;

const int maxn = (int)2e5 + 13;
const ll INFL = 9223372036854775807;
const ll INF = 2147483647;
const ll MOD = (int)1e9 + 7, MOD2 = 998244353;//1e9 + 21, 1e9 + 33, 1e9 + 87, 1e9 + 93
const ll base = 101, base2 = 37;
const long double pi = 3.1415926535897932;

int a[maxn];

int main() {
	drobucs;

	int n;
	cin >> n;
	set<int> st;
	for (int i = 1; i <= n; ++i)cin >> a[i], st.insert(a[i]);
	int it = 0;
	for (auto t : st)a[++it] = t;
	int ans = 0;
	n = it;
	for (int i = 1; i <= n - 1; ++i) {
		int x = 2 * a[i];
		while (x <= 2 * a[n]) {
			int l = 0, r = n + 1;
			while (r - l > 1) {
				int m = (l + r) / 2;
				if (a[m] >= x)r = m;
				else l = m;
			}
			ans = max(a[l] % a[i], ans);
			x += a[i];
		}
	}
	cout << ans << "\n";
	return 0;
}


//Apparat : drobucs, aleonov, vit_72
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <iomanip>
#include <algorithm>
#include <string>
#include <queue>
#include <numeric>

//#pragma warning(disable : 4996)
//#pragma GCC optimize("O3")

using namespace std;

#define nptr nullptr
#define all(x) (x).begin(), (x).end()
#define drobucs ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define ff first
#define ss second
#define pb push_back
#define sz(x) (int)(x).size()

typedef  long long ll;

const int maxn = (int)2e5 + 13;
const ll INFL = 9223372036854775807;
const ll INF = 2147483647;
const ll MOD = (int)1e9 + 7, MOD2 = 998244353;//1e9 + 21, 1e9 + 33, 1e9 + 87, 1e9 + 93
const ll base = 101, base2 = 37;
const long double pi = 3.1415926535897932;

int a[maxn];

int main() {
	drobucs;

	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i)cin >> a[i];
	sort(a + 1, a + n);
	int ans = 0;
	for (int i = 1; i <= n - 1; ++i) {
		int x = 2 * a[i];
		while (x <= 2 * a[n]) {
			int l = 0, r = n + 1;
			while (r - l > 1) {
				int m = (l + r) / 2;
				if (a[m] >= x)r = m;
				else l = m;
			}
			ans = max(a[l] % a[i], ans);
			x += a[i];
		}
	}
	cout << ans << "\n";
	return 0;
}


//Apparat : drobucs, aleonov, vit_72
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <iomanip>
#include <algorithm>
#include <string>
#include <queue>
#include <numeric>

//#pragma warning(disable : 4996)
//#pragma GCC optimize("O3")

using namespace std;

#define nptr nullptr
#define all(x) (x).begin(), (x).end()
#define drobucs ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define ff first
#define ss second
#define pb push_back
#define sz(x) (int)(x).size()

typedef  long long ll;

const int maxn = (int)3e6 + 13;
const ll INFL = 9223372036854775807;
const ll INF = 2147483647;
const ll MOD = (int)1e9 + 7, MOD2 = 998244353;//1e9 + 21, 1e9 + 33, 1e9 + 87, 1e9 + 93
const ll base = 101, base2 = 37;
const long double pi = 3.1415926535897932;

int a[maxn];
int f[maxn];
int main() {
	drobucs;

	int n, it = 0, ans = 0;
	cin >> n;
	set<int> st;
	for (int i = 1; i <= n; ++i)cin >> a[i], st.insert(a[i]);
	for (auto t : st)a[++it] = t;
	n = it;
	for (int i = 1; i <= 2 * a[n]; ++i) {
		int r = n + 1;
		while (r - f[i] > 1) {
			int m = (f[i] + r) / 2;
			if (a[m] >= i) r = m;
			else f[i] = m;
		}
	}
	for (int i = 1; i <= n - 1; ++i) {
		int x = 2 * a[i];
		while (x <= 2 * a[n]) {
			ans = max(a[f[x]] % a[i], ans);
			x += a[i];
		}
	}
	cout << ans << "\n";
	return 0;
}


#include<functional>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<string>
#include<cstdio>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define inf 1000000000000000000
#define MIN 1e-9
#define mod 1000000007
#define PI 3.1415927
const int MAX = 1e7 + 10;
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int t, n, m, l, a[2000010];
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &m);
		t = max(t, m);
		a[m] = m;
	}
	for (int i = 2; i < 2 * t; i++) {
		if (a[i] != i) {
			a[i] = a[i - 1];
		}
	}
	int ans = 0;
	for (int i = 2; i <2 * t; i++) {
		if (a[i] == i) {
			for (int j = 2 * i - 1; j <2 * t; j += i) {
				if (a[j] > a[i]) {
					ans = max(ans, a[j] % a[i]);
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<vector>
#include<iostream>
using namespace std;
struct node
{
    int id;
    int w;
}a[1005];
int b[1005];
vector<node> ve;
int vis[1005];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i].w);
        a[i].id=i;
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&b[i]);
        if(vis[b[i]]==0)
        {
             vis[b[i]]=1;
             ve.push_back(a[b[i]]);
        }
    }
    long long sum=0;
    for(int i=1;i<=m;i++)
    {
        node tmp;
        long long pos=0;
        for(int j=0;j<ve.size();j++)
        {
            if(b[i]==ve[j].id)
            {
                //cout<<pos<<endl;
                tmp=ve[j];
                sum+=pos;
                ve.erase(ve.begin()+j);
            }
            else
            {
                pos+=ve[j].w;
            }
        } ve.insert(ve.begin(),tmp);
    }
    printf("%lld\n",sum);
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

using namespace std;

/*
对于任何一个数a[j] 要找满足a[i]/a[j]=k 且a[i]%a[j]最大的数
那么只需要找到比(k+1)*a[j]小且最接近的数即lower_bound()前一位的数字
*/
const int MAX = 3 * 1e5;

int a[MAX],n,ans;

int main(){
//    freopen("in.txt", "r", stdin);
    scanf("%d",&n);
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
    sort(a, a+n);
    for(int i=0; i<n-1; i++){
        if(i == 0 || a[i] != a[i-1]){
            int j = a[i]+a[i],tmp;
            while(j <= a[n-1]){
                tmp = lower_bound(a, a+n, j) - a;
                if(tmp > 0) ans = max(ans, a[tmp - 1] % a[i]);
                j += a[i];
            }
            ans = max(ans, a[n-1] % a[i]);
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
const int M = 2e6+5;
int arr[M];

int main() {
	int n;
	cin >> n;
	vector<int>a(n);
	for (int i = 0; i < n; i++)
		cin >> a[i];
	sort(a.begin(), a.end());
	int ans = 0;
	a.resize(unique(a.begin(), a.end()) - a.begin());
	int Ma = 2*a[a.size() - 1];
	arr[0] = -1;
	int i = 0, prev = -1;
	for (int j = 1; j < M - 1; j++) {
		arr[j] = prev;

		if (i != a.size() && j == a[i]) {
			prev = a[i];
			i++;
		}
	}

	for (int i = 0; i < a.size()-1; i++) {
		int x;
		for (int k = 2; k <= (Ma / a[i]); k++) {
			x = k * a[i];
			if (arr[x] != -1) {
				ans = max(ans, (arr[x] % a[i]));
			}
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
const int M = 1e6;
int arr[M];

int main() {
	int n;
	cin >> n;
	vector<int>a(n);
	for (int i = 0; i < n; i++)
		cin >> a[i];
	sort(a.begin(), a.end());
	int ans = 0;
	a.resize(unique(a.begin(), a.end()) - a.begin());
	int Ma = 2*a[a.size() - 1];
	arr[0] = -1;
	int i = 0, prev = -1;
	for (int j = 1; j < M - 1; j++) {
		arr[j] = prev;

		if (i != a.size() && j == a[i]) {
			prev = a[i];
			i++;
		}
	}

	for (int i = 0; i < a.size()-1; i++) {
		int x;
		for (int k = 2; k <= (Ma / a[i]); k++) {
			x = k * a[i];
			if (arr[x] != -1) {
				ans = max(ans, (arr[x] % a[i]));
			}
		}
	}
	cout << ans << endl;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <string>
using namespace std;

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    scanf("%d", &n);

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

    static int prev[2000020];
    for (int i = 1, j = 0; i < 2000020; i++) {
        while (j < n - 1 && a[j + 1] < i) {
            j++;
        }
        prev[i] = a[j];
    }

    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (i && a[i] == a[i - 1]) {
            continue;
        }
        for (int f = 2; (f - 1) * a[i] <= 1000010; f++) {
            ans = max(ans, prev[f * a[i]] % a[i]);
        }
    }

    printf("%d", ans);
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <ctime>
using namespace std;

#define ll long long
#define MP make_pair

#define inf 1e9
#define eps 1e-8

#define maxn 210000
#define maxv 1010000
int a[maxn];
int b[maxv<<1];
int main(){
	int n;
	while(~scanf("%d",&n)){
		for(int i=0;i<n;++i) scanf("%d",a+i);
		sort(a,a+n);
		n = unique(a,a+n)-a;
		memset(b,-1,sizeof(b));
		for(int i=0;i<n;++i) b[a[i]] = a[i];
		for(int i=1;i<maxv;++i)
			if(b[i-1]!=-1 && b[i]==-1) b[i] = b[i-1];
		int ans=0;
		for(int i=0;i<n;++i)
			for(int j=a[i]<<1;j<a[n-1]<<1;j+=a[i])
				ans = max(ans, b[j-1]%a[i]);
		printf("%d\n",ans);
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <ctime>
using namespace std;

#define ll long long
#define MP make_pair

#define inf 1e9
#define eps 1e-8

#define maxn 110000
#define maxm 210000

int a[210000];
int main(){
    int n;
    srand(time(0));
    while(~scanf("%d",&n)){
        for(int i=0;i<n;++i) scanf("%d",a+i);
        //for(int i=0;i<n;++i) a[i]=i+1;
        sort(a,a+n);
        n = unique(a,a+n) - a;
        int ans = 0;
        for(int i=1;i<n;++i) ans = max(ans, a[i]%a[i-1]);
        for(int i=0;i<100000;++i){
            int ai = a[rand()%n];
            int aj = a[rand()%n];
            if(ai>=aj) ans = max(ans, ai%aj);
            else ans = max(ans, aj%ai);
        }
        for(int i=0;i<100000;++i){
            int ai = a[rand()%(n-ans)+ans];
            int aj = a[rand()%(n-ans)+ans];
            if(ai>=aj) ans = max(ans, ai%aj);
            else ans = max(ans, aj%ai);
        }
        for(int i=0;i<1000000;++i){
            int idx = lower_bound(a,a+n,ans*2-1)-a;
            int ai = a[rand()%(n-idx)+idx];
            int aj = a[rand()%(n-ans)+ans];
            if(ai>=aj) ans = max(ans, ai%aj);
            else ans = max(ans, aj%ai);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <ctime>
using namespace std;

#define ll long long
#define MP make_pair

#define inf 1e9
#define eps 1e-8

#define maxn 110000
#define maxm 210000

int a[210000];
int main(){
    int n;
    srand(time(0));
    while(~scanf("%d",&n)){
        for(int i=0;i<n;++i) scanf("%d",a+i);
        //for(int i=0;i<n;++i) a[i]=i+1;
        sort(a,a+n);
        n = unique(a,a+n) - a;
        int ans = 0;
        for(int i=1;i<n;++i) ans = max(ans, a[i]%a[i-1]);
        for(int i=0;i<100000;++i){
            int ai = a[rand()%n];
            int aj = a[rand()%n];
            if(ai>=aj) ans = max(ans, ai%aj);
            else ans = max(ans, aj%ai);
        }
        for(int i=0;i<100000;++i){
            int idx = lower_bound(a,a+n,ans)-a;
            if(idx==n) break;
            int ai = a[rand()%(n-idx)+idx];
            int aj = a[rand()%(n-idx)+idx];
            if(ai>=aj) ans = max(ans, ai%aj);
            else ans = max(ans, aj%ai);
        }
        for(int i=0;i<2000000;++i){
            int idx = lower_bound(a,a+n,ans)-a;
            int idx2 = lower_bound(a,a+n,ans*2-1)-a;
            if(idx==n || idx2==n) break;
            int ai = a[rand()%(n-idx)+idx];
            int aj = a[rand()%(n-idx2)+idx2];
            if(ai>=aj) ans = max(ans, ai%aj);
            else ans = max(ans, aj%ai);
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <ctime>
using namespace std;

#define ll long long
#define MP make_pair

#define inf 1e9
#define eps 1e-8

#define maxn 110000
#define maxm 210000

int a[210000];
int main(){
	int n;
	srand(time(0));
	while(~scanf("%d",&n)){
		for(int i=0;i<n;++i) scanf("%d",a+i);
		//for(int i=0;i<n;++i) a[i]=i+1;
		sort(a,a+n);
		n = unique(a,a+n) - a;
		int ans = 0;
		for(int i=1;i<n;++i) ans = max(ans, a[i]%a[i-1]);
		for(int i=0;i<100000;++i){
			int ai = a[rand()%n];
			int aj = a[rand()%n];
			if(ai>=aj) ans = max(ans, ai%aj);
			else ans = max(ans, aj%ai);
		}
		for(int i=0;i<100000;++i){
			int idx = lower_bound(a,a+n,ans)-a;
			if(idx==n) break;
			int ai = a[rand()%(n-idx)+idx];
			int aj = a[rand()%(n-idx)+idx];
			if(ai>=aj) ans = max(ans, ai%aj);
			else ans = max(ans, aj%ai);
		}
		for(int i=0;i<1000000;++i){
			int idx = lower_bound(a,a+n,ans)-a;
			int idx2 = lower_bound(a,a+n,ans*2-1)-a;
			if(idx==n || idx2==n) break;
			int ai = a[rand()%(n-idx)+idx];
			int aj = a[rand()%(n-idx2)+idx2];
			if(ai>=aj) ans = max(ans, ai%aj);
			else ans = max(ans, aj%ai);
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <ctime>
using namespace std;

#define ll long long
#define MP make_pair

#define inf 1e9
#define eps 1e-8

#define maxn 210000
#define maxv 1010000
int a[maxn];
int b[maxv<<1];
int main(){
    int n;
    while(~scanf("%d",&n)){
        for(int i=0;i<n;++i) scanf("%d",a+i);
        sort(a,a+n);
        n = unique(a,a+n)-a;
        memset(b,-1,sizeof(b));
        for(int i=0;i<n;++i) b[a[i]] = a[i];
        for(int i=1;i<(maxv<<1);++i)
            if(b[i-1]!=-1 && b[i]==-1) b[i] = b[i-1];
        int ans=0;
        for(int i=0;i<n;++i)
            for(int j=a[i]<<1;j<=(a[n-1]<<1);j+=a[i])
                ans = max(ans, b[j-1]%a[i]);
        printf("%d\n",ans);
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <ctime>
using namespace std;

#define ll long long
#define MP make_pair

#define inf 1e9
#define eps 1e-8

#define maxn 110000
#define maxm 210000

int a[210000];
int main(){
	int n;
	srand(time(0));
	while(~scanf("%d",&n)){
		//for(int i=0;i<n;++i) scanf("%d",a+i);
		for(int i=0;i<n;++i) a[i]=i+1;
		sort(a,a+n);
		n = unique(a,a+n) - a;
		int ans = 0;
		for(int i=1;i<n;++i) ans = max(ans, a[i]%a[i-1]);
		for(int i=0;i<100000;++i){
			int ai = a[rand()%n];
			int aj = a[rand()%n];
			if(ai>=aj) ans = max(ans, ai%aj);
			else ans = max(ans, aj%ai);
		}
		for(int i=0;i<100000;++i){
			int ai = a[rand()%(n-ans)+ans];
			int aj = a[rand()%(n-ans)+ans];
			if(ai>=aj) ans = max(ans, ai%aj);
			else ans = max(ans, aj%ai);
		}
		for(int i=0;i<1000000;++i){
			int idx = lower_bound(a,a+n,ans*2-1)-a;
			int ai = a[rand()%(n-idx)+idx];
			int aj = a[rand()%(n-ans)+ans];
			if(ai>=aj) ans = max(ans, ai%aj);
			else ans = max(ans, aj%ai);
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <ctime>
using namespace std;

#define ll long long
#define MP make_pair

#define inf 1e9
#define eps 1e-8

#define maxn 110000
#define maxm 210000

int a[210000];
int main(){
	int n;
	srand(time(0));
	while(~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;
		for(int i=1;i<n;++i) ans = max(ans, a[i]%a[i-1]);
		for(int i=0;i<1000000;++i){
			int ai = a[rand()%n];
			int aj = a[rand()%n];
			if(ai>=aj) ans = max(ans, ai%aj);
			else ans = max(ans, aj%ai);
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
using namespace std;

#define ll long long
#define MP make_pair

#define inf 1e9
#define eps 1e-8

#define maxn 110000
#define maxm 210000

int a[210000];
int main(){
	int n;
	srand(time(0));
	while(~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;
		for(int i=1;i<n;++i) ans = max(ans, a[i]%a[i-1]);
		for(int i=0;i<1000000;++i){
			int ai = a[rand()%n];
			int aj = a[rand()%n];
			if(ai>=aj) ans = max(ans, ai%aj);
			else ans = max(ans, aj%ai);
		}
		printf("%d\n",ans);
	}
	return 0;
}


// IN THE NAME OF ALLAH

#include<iostream>
#include<algorithm>

using namespace std;

typedef long long  ll;

const ll maxn=200105,maxm=2000006;
ll n,a[maxn],x,ans;

int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    sort(a+1,a+1+n);
    n=unique(a+1,a+1+n)-a-1;
    for(int i=1;i<n;i++){
        for(int j=2;j<=(maxm/a[i]);j++){
            x=lower_bound(a+1,a+1+n,a[i]*j)-a-1;
            ans=max(ans,a[x]%a[i]);
            if(x==n) break;
        }
    }
    cout<<ans<<endl;
//    system("pause");
    return 0;
}


// IN THE NAME OF ALLAH

#include<iostream>
#include<algorithm>

using namespace std;

typedef long long  ll;

const ll maxn=200105,maxm=1000006;
ll n,a[maxn],x,ans;

int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    sort(a+1,a+1+n);
    n=unique(a+1,a+1+n)-a-1;
    for(int i=1;i<n;i++){
        for(int j=2;j<=(maxm/a[i]);j++){
            x=lower_bound(a+1,a+1+n,a[i]*j)-a-1;
            ans=max(ans,a[x]%a[i]);
            if(x==n) break;
        }
    }
    cout<<ans<<endl;
//    system("pause");
    return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <ctime>

using namespace std;

bool usd[1000009];

int start;

bool TLE()
{
	return (clock() - start < CLOCKS_PER_SEC * 0.9);
}

int main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	start = clock();
	sort(a.rbegin(), a.rend());
	set<int> st;
	st.insert(a[0]);
	int ans = 0;
	for (int i = 1; i < n; ++i)
	{
		if (usd[a[i]])
			continue;
		for (int k = 2; k * a[i] - a[i] < 1000000; ++k)
		{
			set<int>::iterator it;
			it = st.lower_bound(k * a[i]);
			if (it == st.begin())
				continue;
			--it;
			ans = max(ans, (*it) % a[i]);
		}
		st.insert(a[i]);
		usd[a[i]] = true;
		if (!TLE())
			break;
	}
	cout << ans << endl;
}


#include <iostream>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

bool usd[1000009];

int main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	sort(a.begin(), a.end());
	set<int> st;
	int ans = 0;
	for (int i = 0; i < (int)a.size(); ++i)
	{
		if (i == 0 || a[i] != a[i - 1])
		{
			for (int k = 1; k * a[i] <= 1000000; ++k)
			{
				if (!usd[k * a[i]])
				{
					usd[k * a[i]] = true;
					st.insert(k * a[i]);
				}
			}
		}
		set<int>::iterator it;
		it = st.lower_bound(a[i] / 2 + 1);
		if (it != st.end() && *it < a[i])
		{
			ans = max(ans, a[i] - *it);
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	sort(a.rbegin(), a.rend());
	set<int> st;
	st.insert(a[0]);
	int ans = 0;
	for (int i = 1; i < n; ++i)
	{
		for (int k = 2; k * a[i] + a[i] < 1000000; ++k)
		{
			set<int>::iterator it;
			it = st.lower_bound(k * a[i]);
			if (it == st.begin())
				continue;
			--it;
			ans = max(ans, (*it) % a[i]);
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	sort(a.rbegin(), a.rend());
	set<int> st;
	st.insert(a[0]);
	int ans = 0;
	for (int i = 1; i < n; ++i)
	{
		for (int k = 2; k * a[i] - a[i] < 1000000; ++k)
		{
			set<int>::iterator it;
			it = st.lower_bound(k * a[i]);
			if (it == st.begin())
				continue;
			--it;
			ans = max(ans, (*it) % a[i]);
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

bool usd[1000009];

int main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	sort(a.rbegin(), a.rend());
	set<int> st;
	st.insert(a[0]);
	int ans = 0;
	for (int i = 1; i < n; ++i)
	{
		if (usd[a[i]])
			continue;
		for (int k = 2; k * a[i] - a[i] < 1000000; ++k)
		{
			set<int>::iterator it;
			it = st.lower_bound(k * a[i]);
			if (it == st.begin())
				continue;
			--it;
			ans = max(ans, (*it) % a[i]);
		}
		st.insert(a[i]);
		usd[a[i]] = true;
	}
	cout << ans << endl;
}


/***********************************************\
 |Author: YMC
 |Created Time: 2014/11/6 20:33:29
 |File Name: b.cpp
 |Description:
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
int da[200005];
bool vis[1000005];
int pre[1000005];
int main() {
    //freopen("input.txt","r",stdin);
    int n;
    scanf("%d",&n);
    rep(i,n) {
        scanf("%d",&da[i]);
    }
    memset(vis,false,sizeof(vis));
    sort(da,da+n);
    int nn = unique(da,da+n) - da;
    rep(i,nn) vis[da[i]] = true;
    int pr = 0;
    for(int i=1;i<=1000005;++i) {
        if(vis[i]) {
            pr = i;
            pre[i] = pr;
        } else {
            pre[i] = pr;
        }
    }
    int maxx = 0;
    int t = 0;
    int mm = pre[1000005] * 2 + 1;
    for(int i=0;i<nn;++i) {
        for(int j=2*da[i];j <= 1000005 && j <= mm;j += da[i]) {


/***********************************************\
 |Author: YMC
 |Created Time: 2014/11/6 20:33:29
 |File Name: b.cpp
 |Description:
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
int da[200005];
bool vis[1000005];
int pre[1000005];
int main() {
    //freopen("input.txt","r",stdin);
    int n;
    scanf("%d",&n);
    rep(i,n) {
        scanf("%d",&da[i]);
    }
    memset(vis,false,sizeof(vis));
    rep(i,n) vis[da[i]] = true;
    int pr = 0;
    for(int i=1;i<=1000005;++i) {
        if(vis[i]) {
            pr = i;
            pre[i] = pr;
        } else {
            pre[i] = pr;
        }
    }
    int maxx = 0;
    int t = 0;
    for(int i=0;i<n;++i) {
        for(int j=2*da[i];j <= 1000005;j += da[i]) {
            t = pre[j] % da[i];
            if(t > maxx) maxx = t;
        }
    }
    printf("%d\n",maxx);
    return 0;
}


/***********************************************\
 |Author: YMC
 |Created Time: 2014/11/6 20:33:29
 |File Name: b.cpp
 |Description:
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
int da[200005];
bool cmp(int a,int b) {
    return a > b;
}
int main() {
	//freopen("input.txt","r",stdin);
    int n;
    scanf("%d",&n);
    rep(i,n) {
        scanf("%d",&da[i]);
    }
    sort(da,da+n,cmp);
    int nn = unique(da,da+n) - da;
    int mma = 0;
    for(int i=0;i<nn && da[i] > mma;++i) {
        int ma = 0;
        for(int j=i+1;j < n && da[j] > ma; ++j) {
            int t = da[i] % da[j];
            if(t > ma) {
                ma = t;
            }
        }
        if(ma > mma) mma = ma;
    }
    printf("%d\n",mma);
	return 0;
}


/***********************************************\
 |Author: YMC
 |Created Time: 2014/11/6 20:33:29
 |File Name: b.cpp
 |Description:
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
int da[200005];
int main() {
    //freopen("input.txt","r",stdin);
    int n;
    scanf("%d",&n);
    rep(i,n) {
        scanf("%d",&da[i]);
    }
    sort(da,da+n);
    int nn = unique(da,da+n) - da;
    int maxx = 0;
    //cout<<da[nn-1]<<endl;
    int ma = 2 * da[nn - 1] + 1;
    for(int i=0;i<n;++i) {
        for(int j=2*da[i];j <= ma;j += da[i]) {
            int* p = lower_bound(da,da+nn,j);
            if(p == NULL) continue;
            p --;
            //cout<<(*p)<<endl;
            int t = (*p) % da[i];
            if(t > maxx) maxx = t;
        }
    }
    printf("%d\n",maxx);
    return 0;
}


/***********************************************\
 |Author: YMC
 |Created Time: 2014/11/6 20:33:29
 |File Name: b.cpp
 |Description:
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
int da[200005];
bool vis[1000005];
int pre[1000005];
int main() {
    //freopen("input.txt","r",stdin);
    int n;
    scanf("%d",&n);
    rep(i,n) {
        scanf("%d",&da[i]);
    }
    memset(vis,false,sizeof(vis));
    sort(da,da+n);
    int nn = unique(da,da+n) - da;
    rep(i,nn) vis[da[i]] = true;
    int pr = 0;
    for(int i=1;i<=1000005;++i) {
        if(vis[i]) {
            pr = i;
            pre[i] = pr;
        } else {
            pre[i] = pr;
        }
    }
    int maxx = 0;
    int t = 0;
    int mm = pre[1000005] * 2 + 1;
    for(int i=0;i<nn;++i) {
        for(int j=2*da[i];j <= 1000005 && j <= mm;j += da[i]) {
            t = pre[j] % da[i];
            if(t > maxx) maxx = t;
        }
        maxx = max(maxx,pre[1000005] % da[i]);
    }
    printf("%d\n",maxx);
    return 0;
}


/***********************************************\
 |Author: YMC
 |Created Time: 2014/11/6 20:33:29
 |File Name: b.cpp
 |Description:
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
int da[200005];
bool vis[1000005];
int pre[1000005];
int main() {
    //freopen("input.txt","r",stdin);
    int n;
    scanf("%d",&n);
    rep(i,n) {
        scanf("%d",&da[i]);
    }
    memset(vis,false,sizeof(vis));
    rep(i,n) vis[da[i]] = true;
    int pr = 0;
    for(int i=1;i<=1000005;++i) {
        if(vis[i]) {
            pr = i;
            pre[i] = pr;
        } else {
            pre[i] = pr;
        }
    }
    int maxx = 0;
    int t = 0;
    for(int i=0;i<n;++i) {
        for(int j=2*da[i];j <= 1000005;j += da[i]) {
            t = pre[j] % da[i];
            if(t > maxx) maxx = t;
        }
        maxx = max(maxx,pre[1000005] % da[i]);
    }
    printf("%d\n",maxx);
    return 0;
}


/***********************************************\
 |Author: YMC
 |Created Time: 2014/11/6 20:33:29
 |File Name: b.cpp
 |Description:
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
int da[200005];
bool vis[1000005];
int pre[1000005];
int main() {
    //freopen("input.txt","r",stdin);
    int n;
    scanf("%d",&n);
    rep(i,n) {
        scanf("%d",&da[i]);
    }
    memset(vis,false,sizeof(vis));
    sort(da,da+n);
    int nn = unique(da,da+n) - da;
    rep(i,nn) vis[da[i]] = true;
    int pr = 0;
    for(int i=1;i<=1000005;++i) {
        if(vis[i]) {
            pre[i] = pr;
            pr = i;
        } else {
            pre[i] = pr;
        }
    }
    int maxx = 0;
    int t = 0;
    int mm = pre[1000005] * 2 + 1;
    for(int i=0;i<nn;++i) {
        for(int j=2*da[i];j <= 1000005 && j <= mm;j += da[i]) {
            t = pre[j] % da[i];
            if(t > maxx) maxx = t;
        }
        maxx = max(maxx,pre[1000005] % da[i]);
    }
    printf("%d\n",maxx);
    return 0;
}


#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; double w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
	int n; cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) cin >> a[i];
	sort(a.begin(), a.end());
	a.erase(unique(a.begin(), a.end()), a.end());
	n = a.size();
	vector<int> b(2000001);
	for (int i = 0; i < n; i++) b[a[i]] = a[i];
	for (int j = 1; j <= 2000000; j++)
		b[j] = max(b[j], b[j - 1]);
	int ans = 0;
	for (int i = 0; i < n; i++)
		for (int j = a[i] - 1; j <= 2000000; j += a[i])
			if (b[j] >= a[i])
				ans = max(ans, b[j] % a[i]);
	cout << ans << endl;
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<stdio.h>
#define ll long long
#define maxn 200001
using namespace std;
template <class T>
bool read(T &x)
{
    char c;
    bool op=0;
    while(c=getchar(),c<'0'||c>'9')
	if(c=='-')
	{
		op=1;
	}
	else if(c==EOF)
	{
		return 0;
	}
    x=c-'0';
    while(c=getchar(),c>='0'&&c<='9')
	x=x*10+c-'0';
    if(op)x=-x;
    return 1;
}
template <class T>
void write(T x)
{
    if(x>=10)
	{
		write(x/10);
	}
    putchar('0'+x%10);
}
ll n;
ll arr[maxn];
ll ans;
set<ll> s;
int loc;
ll llmax(ll a,ll b)
{
	if(a>=b)
	{
		return a;
	}
	else
	{
		return b;
	}
}
ll llmin(ll a,ll b)
{
	if(a<=b)
	{
		return a;
	}
	else
	{
		return b;
	}
}
int main()
{
	read(n);
	for(int i=0;i<n;i++)
	{
		read(arr[i]);
	}
	sort(arr,arr+n);
	for(int i=0;i<n;i++)
	{
		if(s.find(arr[i])!=s.end())
		{
			continue;
		}
		else
		{
			s.insert(arr[i]);
			if(ans>=arr[i])
			{
				continue;
			}
			else
			{
				for(int j=2*arr[i];j<=arr[n-1];j+=arr[i])
				{
					loc=lower_bound(arr,arr+n,j)-arr;
					loc--;
					ans=llmax(ans,arr[loc]%arr[i]);
				}
				if(2*arr[i]>arr[n-1])
				{
					ans=llmax(ans,arr[n-1]%arr[i]);
				}
			}
		}
	}
	write(ans);
	cout<<endl;
	return 0;
}


#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int qq = 200000+10;
int num[qq];
int main(){
	int n;scanf("%d",&n);
	for(int i=0; i<n; ++i)	scanf("%d",num+i);
	sort(num, num+n);
	int ans = 0;
	for(int i=0; i<n; ++i){
		if(num[i]==num[i-1])	continue;
		if(ans>=num[i])	break;
		int tmp = 2*num[i];
		do{
			int id = lower_bound(num, num+n, tmp)-num;
			id = id-1;
			ans = max(ans, num[id]%num[i]);
			tmp+=num[i];
		}while(tmp<=num[n-1]);
		ans = max(ans, num[n-1]%num[i]);	//一定得加这一句，不然这个情况可能遍历不到、
	}
	//printf("%d\n", *(lower_bound(num, num+n, 9)-1));
	printf("%d\n", ans);
	return 0;
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<stdio.h>
#define ll long long
#define maxn 200001
using namespace std;
template <class T>
bool read(T &x)
{
    char c;
    bool op=0;
    while(c=getchar(),c<'0'||c>'9')
	if(c=='-')
	{
		op=1;
	}
	else if(c==EOF)
	{
		return 0;
	}
    x=c-'0';
    while(c=getchar(),c>='0'&&c<='9')
	x=x*10+c-'0';
    if(op)x=-x;
    return 1;
}
template <class T>
void write(T x)
{
    if(x>=10)
	{
		write(x/10);
	}
    putchar('0'+x%10);
}
ll n;
ll arr[maxn];
ll ans;
set<ll> s;
int loc;
ll llmax(ll a,ll b)
{
	if(a>=b)
	{
		return a;
	}
	else
	{
		return b;
	}
}
ll llmin(ll a,ll b)
{
	if(a<=b)
	{
		return a;
	}
	else
	{
		return b;
	}
}
int main()
{
	read(n);
	for(int i=0;i<n;i++)
	{
		read(arr[i]);
	}
	sort(arr,arr+n);
	for(int i=0;i<n;i++)
	{
		if(s.find(arr[i])!=s.end())
		{
			continue;
		}
		else
		{
			s.insert(arr[i]);
			if(ans>=arr[i])
			{
				continue;
			}
			else
			{
				for(int j=2*arr[i];j<=arr[n-1];j+=arr[i])
				{
					loc=lower_bound(arr,arr+n,j)-arr;
					//loc--;
					ans=llmax(ans,arr[loc]%arr[i]);
				}
				if(2*arr[i]>arr[n-1])
				{
					ans=llmax(ans,arr[n-1]%arr[i]);
				}
			}
		}
	}
	write(ans);
	cout<<endl;
	return 0;
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<stdio.h>
#define ll long long
#define maxn 200001
using namespace std;
template <class T>
bool read(T &x)
{
    char c;
    bool op=0;
    while(c=getchar(),c<'0'||c>'9')
	if(c=='-')
	{
		op=1;
	}
	else if(c==EOF)
	{
		return 0;
	}
    x=c-'0';
    while(c=getchar(),c>='0'&&c<='9')
	x=x*10+c-'0';
    if(op)x=-x;
    return 1;
}
template <class T>
void write(T x)
{
    if(x>=10)
	{
		write(x/10);
	}
    putchar('0'+x%10);
}
ll n;
ll arr[maxn];
ll ans;
set<ll> s;
int loc;
ll llmax(ll a,ll b)
{
	if(a>=b)
	{
		return a;
	}
	else
	{
		return b;
	}
}
ll llmin(ll a,ll b)
{
	if(a<=b)
	{
		return a;
	}
	else
	{
		return b;
	}
}
int main()
{
	read(n);
	for(int i=0;i<n;i++)
	{
		read(arr[i]);
	}
	sort(arr,arr+n);
	for(int i=0;i<n;i++)
	{
		if(s.find(arr[i])!=s.end())
		{
			continue;
		}
		else
		{
			s.insert(arr[i]);
			if(ans>=arr[i])
			{
				continue;
			}
			else
			{
				for(int j=2*arr[i];j<=arr[n-1];j+=arr[i])
				{
					loc=lower_bound(arr,arr+n,j)-arr;
					loc--;
					ans=llmax(ans,arr[loc]%arr[i]);
				}
				ans=llmax(ans,arr[n-1]%arr[i]);
			}
		}
	}
	write(ans);
	cout<<endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <algorithm>
#include <queue>
using namespace std;
#define maxn 200005
int a[maxn];
int main()
{
    int n;
    int i , j ;
    while( scanf("%d", &n ) != EOF)
    {
        memset( a,0 , sizeof( a ));
        for( i  = 1; i<=n; i++)
        {
            scanf("%d", &a[i]);
        }
        sort( a+1, a+n+1);
        int t;
        int ans = 0;
        for( i =  1; i<=n; i++)
        {
            if( a[i] == a[i-1])
                continue;
            for( j = 2*a[i]; j<=a[n]; j = j+a[i])
            {
               t = lower_bound( a+1, a+n+1, j )-a;
               ans = max( ans, a[t-1]%a[i]);
            }
            ans = max( ans, a[n]%a[i]);///（ 是不是也有点不懂 让我来为你解释哦 ）防止这个数的某倍数
            ///不在数组内，而没办法进入第二层循环，出现最后一个数模他
            ///会结果很大的情况而被忽略 比如 2 3 4 5 6 7 （ 4 的2倍没有 但不能忽略7%4 这个大的！）
        }
        printf("%d\n", ans);
    }
}


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<stdio.h>
#define ll long long
#define maxn 200001
using namespace std;
template <class T>
bool read(T &x)
{
    char c;
    bool op=0;
    while(c=getchar(),c<'0'||c>'9')
	if(c=='-')
	{
		op=1;
	}
	else if(c==EOF)
	{
		return 0;
	}
    x=c-'0';
    while(c=getchar(),c>='0'&&c<='9')
	x=x*10+c-'0';
    if(op)x=-x;
    return 1;
}
template <class T>
void write(T x)
{
    if(x>=10)
	{
		write(x/10);
	}
    putchar('0'+x%10);
}
ll n;
ll arr[maxn];
ll ans;
set<ll> s;
int loc;
ll llmax(ll a,ll b)
{
	if(a>=b)
	{
		return a;
	}
	else
	{
		return b;
	}
}
ll llmin(ll a,ll b)
{
	if(a<=b)
	{
		return a;
	}
	else
	{
		return b;
	}
}
int main()
{
	read(n);
	for(int i=0;i<n;i++)
	{
		read(arr[i]);
	}
	sort(arr,arr+n);
	for(int i=0;i<n;i++)
	{
		if(s.find(arr[i])!=s.end())
		{
			continue;
		}
		else
		{
			s.insert(arr[i]);
//			if(ans>=arr[i])
//			{
//				continue;
//			}
//			else
			{
				for(int j=2*arr[i];j<=arr[n-1];j+=arr[i])
				{
					loc=lower_bound(arr,arr+n,j)-arr;
					loc--;
					ans=llmax(ans,arr[loc]%arr[i]);
				}
				if(2*arr[i]>arr[n-1])
				{
					ans=llmax(ans,arr[n-1]%arr[i]);
				}
			}
		}
	}
	write(ans);
	cout<<endl;
	return 0;
}


#define _ijps 0
#define _CRT_SECURE_NO_DEPRECATE
#pragma comment(linker, "/STACK:66777216")
#include <iostream>
#include <cmath>
#include <vector>
#include <time.h>
#include <map>
#include <set>
#include <deque>
#include <cstdio>
#include <cstdlib>
#include <bitset>
#include <algorithm>
#include <string>
using namespace std;

#define name ""
#define double long double
typedef unsigned long long ull;
typedef long long ll;
#define mk make_pair
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define fornn(i, q, n) for(ll i = (ll)q; i < (ll)n; i++)
#define times clock() * 1.0 / CLOCKS_PER_SEC
#define X first
#define Y second
struct __isoff{
    __isoff(){
        if (_ijps)
            freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);//, freopen("test.txt", "w", stderr);
        //else freopen(name".in", "r", stdin), freopen(name".out", "w", stdout);
        ios_base::sync_with_stdio(0);
        srand(time(0));
    }
    ~__isoff(){
        //if(_ijps) cerr<<times<<'\n';
    }
} __osafwf;

const ll inf = 1e18 + 7;
const ll infi = 1e9 + 7;
const double infd = 23e30 + 7;
const ll dd = 1e6 + 7;
const ll mod = 1e9 + 7;
const ull p1 = 131797;
const ull p2 = 435493969;
const double eps = 1e-9;
const double pi = acos(-1.0);

vector<int> P;
int Z[dd];

int main(){
    int n, m, i = 0, c = 0;
    cin>>n;
    P.resize(n);
    forn(i, n){
        scanf("%d", &P[i]);
        Z[P[i]] = P[i];
    }
    fornn(i, 1, dd)
        Z[i] = max(Z[i], Z[i - 1]);
    sort(P.begin(), P.end());
    P.resize(unique(P.begin(), P.end()) - P.begin());
    m = (sqrt(P.back() * 1.0) + 5) / 3;
    for(i = 0; i < n && P[i] < m; i++){
        int t = 0;
        fornn(j, i + 1, n){
            while(t <= P[j])
                t += P[i];
            c = max(c, P[j] - t + P[i]);
        }
    }
    for(; i < n; i++){
        c = max(P.back() % P[i], c);
        for(int j = 2; j * P[i] <= P.back(); j++)
            c = max(c, Z[j * P[i] - 1] - P[i] * (j - 1));
    }

    cout<<c;
}


#define _ijps 0
#define _CRT_SECURE_NO_DEPRECATE
#pragma comment(linker, "/STACK:66777216")
#include <iostream>
#include <cmath>
#include <vector>
#include <time.h>
#include <map>
#include <set>
#include <deque>
#include <cstdio>
#include <cstdlib>
#include <bitset>
#include <algorithm>
#include <string>
using namespace std;

#define name ""
#define double long double
typedef unsigned long long ull;
typedef long long ll;
#define mk make_pair
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define fornn(i, q, n) for(ll i = (ll)q; i < (ll)n; i++)
#define times clock() * 1.0 / CLOCKS_PER_SEC
#define X first
#define Y second
struct __isoff{
    __isoff(){
        if (_ijps)
            freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);//, freopen("test.txt", "w", stderr);
        //else freopen(name".in", "r", stdin), freopen(name".out", "w", stdout);
        ios_base::sync_with_stdio(0);
        srand(time(0));
    }
    ~__isoff(){
        //if(_ijps) cerr<<times<<'\n';
    }
} __osafwf;

const ll inf = 1e18 + 7;
const ll infi = 1e9 + 7;
const double infd = 23e30 + 7;
const ll dd = 1e6 + 7;
const ll mod = 1e9 + 7;
const ull p1 = 131797;
const ull p2 = 435493969;
const double eps = 1e-9;
const double pi = acos(-1.0);

vector<int> P;

int main(){
    int n, m, i, c = 0;
    cin>>n;
    P.resize(n);
    forn(i, n)
        cin>>P[i];
    sort(P.begin(), P.end());
    m = sqrt(P.back() * 1.0) + 5;
    for(i = 0; i < n && P[i] < m; i++)
        fornn(j, i, n)
            c = max(c, P[j] % P[i]);
    for(; i < n; i++)
        for(int j = 2; j * P[i] <= P.back(); j++)
            c = max(c, P[lower_bound(P.begin(), P.end(), j * P[i] - 1) - P.begin()] % P[i]);
    cout<<c;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	//if (sz[u]<k) return 0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
	return res;
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}/*

	insert(-INF); insert(INF);
	for (int i=1;i<=10;++i) insert(i);
	insert(5); insert(6);
	printf("%lld\n",query2(9));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u);
	if (w2[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[u]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			if (!res) ans=max(a[i].a+(n-1)*a[i].b,ans);
			else if (num<=n-2)
				ans=max(res+a[i].a+(long long)(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}
	/*
	insert(-INF); insert(INF);
	insert(5); insert(2);
	pair<int, long long> que=query(4);
	printf("%d %lld\n",que.first,que.second);
	//printf("%lld\n",query2(8));*/
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int maxM=100000+5;
const int INF=0x3f3f3f3f;

int ch[maxM][2],fa[maxM],sz[maxM],cnt[maxM];
long long w2[maxM],W[maxM],w[maxM];
struct Node
{
	long long a,b;
	bool operator < (const Node &p) const
	{
		return b==p.b ? a<p.a : b<p.b;
	}
}a[maxM];
int n,m,rt,tot;

void pushup(int x)
{
	sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
	W[x]=W[ch[x][0]]+W[ch[x][1]]+cnt[x]*w2[x];
}

int chk(int x) { return ch[fa[x]][1]==x; }

void rotate(int x)
{
	int y=fa[x],z=fa[y],k=chk(x);
	ch[y][k]=ch[x][k^1]; fa[ch[x][k^1]]=y;
	ch[z][chk(y)]=x; fa[x]=z;
	ch[x][k^1]=y; fa[y]=x;
	pushup(y); pushup(x);
}

void splay(int x,int goal=0)
{
	while (fa[x]!=goal)
	{
		int y=fa[x],z=fa[y];
		if (z!=goal)
		{
			if (chk(x)==chk(y)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	if (!goal) rt=x;
}

void insert(int val)
{
	int u=rt,p=0;
	while (u && w[u]!=val)
	{
		p=u; u=ch[u][val>w[u]];
	}
	if (w[u]==val)
	{
		++cnt[u]; splay(u);
	}
	else
	{
		u=++tot;
		if (p) { ch[p][val>w[p]]=u; fa[u]=p; }
		w[u]=val;
		if (val!=INF && val!=-INF)
		{
			sz[u]=cnt[u]=1; w2[u]=val;
		}
		splay(u);
	}
}

pair<int, long long> query(int val)
{
	int u=rt;
	while (ch[u][val>w[u]] && w[u]!=val) u=ch[u][val>w[u]];
	splay(u); //printf("%lld\n",w2[u]);
	if (w[u]>val) return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
	u=ch[u][1];
	while (ch[u][0]) u=ch[u][0];
	splay(u);
	return make_pair(cnt[u]+sz[ch[u][1]],cnt[u]*w2[u]+W[ch[u][1]]);
}

long long query2(int k)
{
	int u=rt; long long res=0;
	while (k)
	{
		if (k<=sz[ch[u][1]]) u=ch[u][1];
		else if (k>cnt[u]+sz[ch[u][1]])
		{
			k-=cnt[u]+sz[ch[u][1]];
			res+=cnt[u]*w2[u]+W[ch[u][1]];
			u=ch[u][0];
		}
		else
		{
			res+=(k-sz[ch[u][1]])*w2[u]+W[ch[u][1]];
			splay(u); return res;
		}
	}
}

int main()
{
	int T; scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;++i) scanf("%I64d%I64d",&a[i].a,&a[i].b);
		sort(a+1,a+m+1);
		tot=0; rt=0;
		for (int i=1;i<=m+2;++i)
			ch[i][0]=ch[i][1]=fa[i]=sz[i]=cnt[i]=w[i]=w2[i]=W[i]=0;
		insert(-INF); insert(INF);
		long long ans=0;
		for (int i=1;i<=m;++i)
		{
			pair<int, long long> que=query(a[i].b);
			int num=que.first; long long res=que.second;
			//printf("%d %d %lld\n",i,num,res);
			if (!num)
			{
				ans=max(a[i].a+a[i].b*(n-1),ans);
				printf("%lld\n",ans);
			}
			else if (num<=n-2)
				ans=max(res+a[i].a+(n-num-1)*a[i].b,ans);
			else ans=max(query2(n-2)+a[i].a+a[i].b,ans);
			insert(a[i].a);
		}
		printf("%I64d\n",ans);
	}
	/*
	insert(-INF); insert(INF);
	insert(5); insert(2);
	pair<int, long long> que=query(4);
	printf("%d %lld\n",que.first,que.second);
	//printf("%lld\n",query2(8));*/
	return 0;
}