#include <cstdio>
#include <algorithm>
using namespace std;
int vis[10010];
int c[10010], cn, ci[10010];
int l[1000010];
int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		cn = 0;
		int n, p = 0, q = 0;
		scanf("%d", &n);
		for(int i = 1; i <= n; ++i) scanf("%d", l + i);
		sort(l + 1, l + n + 1);
		for(int i = 1; i <= n; ++i) {
			if(!vis[l[i]]) vis[l[i]] = ++cn, ci[cn] = l[i], c[cn] = 1;
			else ++c[vis[l[i]]];
		}
		bool has4 = false;
		for(int i = 1; i <= cn; ++i) if(c[i] >= 4) has4 = true, p = q = ci[i];
		if(!has4) {
			for(int i = 1, last = 0; i <= cn; ++i) {
				if(c[i] >= 2) {
					if(!last) {
						last = ci[i];
						continue;
					};
					if(!p && !q) p = last, q = ci[i];
					else if(ci[i] * p < q * last) p = last, q = ci[i];
					last = ci[i];
				}
			}
		}
		printf("%d %d %d %d\n", p, p, q, q);
		for(int i = 1; i <= n; ++i) vis[l[i]] = 0;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int t;
int n , len = 0;
int a[1010100];
int cnt[20010];
int b[101010];
void work(){
	memset(cnt,0,sizeof(cnt));
	len = 0;
	scanf("%lld",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&a[i]),cnt[a[i]]++;
	for (int i=1;i<=10010;i++){
	    if (cnt[i]>=2) b[++len] = i;
	    if (cnt[i]>=4) b[++len] = i;
	}
	  double minn = 100000000000.000000;
	  int minn1 = 0;
	  sort(b+1,b+len+1);
	  for (int i=2;i<=len;i++){
	  	  if (b[i] == b[i-1]){
//	  	  	  printf("%d %d %d %d\n",b[i],b[i],b[i],b[i]);
              cout<<b[i]<<' '<<b[i]<<' '<<b[i]<<' '<<b[i]<<endl;
	  	  	  return;
	  	  }
	      if (b[i]/b[i-1]*1.0 < minn) minn = b[i]/b[i-1]*1.0,minn1 = i;
	  }
      printf("%lld %lld %lld %lld\n",b[minn1],b[minn1],b[minn1-1],b[minn1-1]);
	  return;
}

signed main(){
	scanf("%lld",&t);
	while (t--) work();
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef long long ll;
typedef pair<ll,ll> abcd;
typedef set<abcd>::iterator It;
const int N=200010;
set<abcd>S;
vector<abcd>g;
int k,n,m,l;
ll Ans;
ll x,lst;
int main(){
	scanf("%d%I64d",&n,&lst);
	S.insert(abcd(lst-1,0));
	for(int i=1;i<n;i++){
		scanf("%I64d",&x);
		if(x>=lst)continue;
		lst=x;
		g.clear();
		while(!S.empty()){
			It I=S.end();I--;
			if((*I).fi<x)break;
			g.push_back((*I));
			S.erase(I);
		}
		if(!(g.size()))continue;
		ll mx=0;
		for(int j=0;j<g.size();j++){
			abcd t=g[j];
			mx=max(mx,t.se+1ll*i*(t.fi-x-t.fi%x));
			S.insert(abcd(t.fi%x,t.se+1ll*i*(t.fi-t.fi%x)));
		}
		S.insert(abcd(x-1,mx));
	}
	for(It I=S.begin();I!=S.end();I++){
		abcd t=(*I);
		Ans=max(Ans,n*t.fi+t.se);
	}
	cout<<Ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
struct Node{
	LL k,j;
	bool operator < (const Node& a)const{
		return j < a.j;
	}
};
priority_queue<Node>Q;
const int maxn = 2e5 + 7;
LL a[maxn];
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	Q.push((Node){0,a[1] - 1});
	for(int i = 2; i <= n; i++){
		LL Mx = 0;
		bool flag = 0;
		while(1){
			Node u = Q.top();
			if(u.j < a[i])
				break;
			flag = 1;
			Mx = max(Mx,u.k + a[i] * (i - 1) * ((u.j + 1) / a[i] - 1));
			Q.pop();
			Q.push((Node){u.k + 1LL * (i - 1) * (u.j - u.j % a[i])});
		}
		if(flag)
			Q.push((Node){Mx,a[i] - 1});
	}
	LL ans = 0;
	while(Q.size()){
		Node u = Q.top();
		Q.pop();
		ans = max(ans,u.k + u.j * n);
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
struct Node{
	LL k,j;
	bool operator < (const Node& a)const{
		return j < a.j;
	}
};
priority_queue<Node>Q;
const int maxn = 2e5 + 7;
LL a[maxn];
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	Q.push((Node){0,a[1] - 1});
	for(int i = 2; i <= n; i++){
		LL Mx = 0;
		bool flag = 0;
		while(1){
			Node u = Q.top();
			if(u.j < a[i])
				break;
			flag = 1;
			Mx = max(Mx,u.k + a[i] * (i - 1) * ((u.j + 1) / a[i] - 1));
			Q.pop();
			Q.push((Node){u.k + 1LL * (i - 1) * (u.j - u.j % a[i]),u.j % a[i]});
		}
		if(flag)
			Q.push((Node){Mx,a[i] - 1});
	}
	LL ans = 0;
	while(Q.size()){
		Node u = Q.top();
		Q.pop();
		ans = max(ans,u.k + u.j * n);
	}
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define N 200005

using namespace std;
inline ll Get() {ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while('0'<=ch&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}

int n;
ll a[N];
map<ll,ll>f;
map<ll,ll>::iterator it;
int main() {
	n=Get();
	for(int i=1;i<=n;i++) a[i]=Get();
	f[a[1]-1]=0;
	for(int i=2;i<=n;i++) {
		while(1) {
			it=--f.end();
			ll now=it->first;
			if(now<a[i]) break;
			f[now%a[i]]=max(f[now%a[i]],f[now]+(i-1)*(now-now%a[i]));
			f[now-1]=max(f[now-1],f[now]+(i-1)*(now-now%a[i]-1-(now-1)));
			f.erase(it);
		}
	}
	ll ans=0;
	for(it=f.begin();it!=f.end();it++) ans=max(ans,n*it->first+it->second);
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define N 200005

using namespace std;
inline ll Get() {ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while('0'<=ch&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}

int n;
ll a[N];
map<ll,ll>f;
map<ll,ll>::iterator it;
int main() {
	n=Get();
	for(int i=1;i<=n;i++) a[i]=Get();
	f[a[1]-1]=0;
	for(int i=2;i<=n;i++) {
		while(1) {
			it=--f.end();
			ll now=it->first;
			if(now<a[i]) break;
			ll val=it->second;
			f[now%a[i]]=max(f[now%a[i]],val+(i-1)*(now-now%a[i]));
			f[a[i]-1]=max(f[a[i]-1],val+(i-1)*(now-now%a[i]-1-(a[i]-1)));
			f.erase(it);
		}
	}
	ll ans=0;
	for(it=f.begin();it!=f.end();it++) ans=max(ans,n*it->first+it->second);
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int RLEN=(1<<20)|5;
inline char gc(){
	static char ibuf[RLEN],*ib,*ob;
	(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ib==ob)?EOF:*ib++;
}
#define gc getchar
inline int read(){
	char ch=gc();
	int res=0,f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
	return f?res:-res;
}
#define pb push_back
#define re register
#define fi first
#define se second
#define pii pair<int,int>
#define cs const
#define bg begin
#define ll long long
inline ll readl(){
	char ch=gc();
	ll res=0;bool f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
	return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=200005;
int n;
ll a[N];
map<ll,ll> mp;
map<ll,ll>::iterator it;
int main(){
	#ifdef Stargazer
	freopen("lx.cpp","r",stdin);
	#endif
	n=read();
	for(int i=1;i<=n;i++)a[i]=readl();
	mp[a[1]-1]=0;
	for(int i=2;i<=n;i++){
		for(it=--mp.end();it->fi>=a[i];mp.erase(it),it=--mp.end()){
			chemx(mp[a[i]-1],(it->fi-it->fi%a[i]-a[i])*(i-1)+it->se);
			chemx(mp[it->fi%a[i]],(it->fi-it->fi%a[i])*(i-1)+it->se);
		}
	}
	ll res=0;
	for(it=mp.bg();it!=mp.end();++it)
	chemx(res,it->fi*n+it->se);
	cout<<res;
}


#include<bits/stdc++.h>
#define loop(n,i) for(register int i=1;i<=(n);++i)
#define zxc(x) cerr<<(#x)<<'='<<(x)<<'\n'
#define zxcv(x) cerr<<(#x)<<'='<<(x)<<','
#define zcx zxc
#define MAX 10009
using namespace std;
inline int icin(){
	char c=getchar();int s=0;bool sign=0;
	while(!isdigit(c)&&c^'-')c=getchar();
	if(c=='-')c=getchar(),sign=1;
	while(isdigit(c))s=(s<<1)+(s<<3)+c-'0',c=getchar();
	return sign?-s:s;
}
unordered_map<int,int> f[2];int I;
int a[MAX],n;
main(){
	n=icin();
	loop(n,i) a[i]=icin();
	f[I][a[1]-1]=0;
	for(register int i=1;i<n;++i){
		f[I^1].clear();
		for(auto it:f[I]){
			int j=it.first,b=it.second;
			if(j<a[i+1]) f[I^1][j]=f[I][j];
			else{
				f[I^1][a[i+1]-1]=max(f[I^1][a[i+1]-1],f[I][j]+i*(j-j%a[i+1]-a[i+1]));
				f[I^1][j%a[i+1]]=max(f[I^1][j%a[i+1]],f[I][j]+i*(j-j%a[i+1]));
			}
		}
		I^=1;
	}
	int ans=0;
	for(auto it:f[I]) ans=max(ans,n*it.first+it.second);
	cout<<ans<<'\n';
}


#include<bits/stdc++.h>
#define loop(n,i) for(register int i=1;i<=(n);++i)
#define zxc(x) cerr<<(#x)<<'='<<(x)<<'\n'
#define zxcv(x) cerr<<(#x)<<'='<<(x)<<','
#define zcx zxc
#define MAX 200009
#define int long long
using namespace std;
inline int icin(){
	char c=getchar();int s=0;bool sign=0;
	while(!isdigit(c)&&c^'-')c=getchar();
	if(c=='-')c=getchar(),sign=1;
	while(isdigit(c))s=(s<<1)+(s<<3)+c-'0',c=getchar();
	return sign?-s:s;
}
map<int,int> f;
int a[MAX],n;
main(){
	n=icin();
	loop(n,i) a[i]=icin();
	f[a[1]-1]=0;
	for(register int i=1;i<n;++i){
		while(f.begin()!=f.end()){
			int j=(--f.end())->first,b=(--f.end())->second;
			if(j<a[i+1]) break;
			f[a[i+1]-1]=max(f[a[i+1]-1],b+i*(j-j%a[i+1]-a[i+1]));
			f[j%a[i+1]]=max(f[j%a[i+1]],b+i*(j-j%a[i+1]));
			f.erase(--f.end());
		}
	}
	int ans=0;
	for(auto it:f) ans=max(ans,n*it.first+it.second);
	cout<<ans<<'\n';
}//


#include<bits/stdc++.h>
#define loop(n,i) for(register int i=1;i<=(n);++i)
#define zxc(x) cerr<<(#x)<<'='<<(x)<<'\n'
#define zxcv(x) cerr<<(#x)<<'='<<(x)<<','
#define zcx zxc
#define MAX 200009
#define int long long
using namespace std;
inline int icin(){
	char c=getchar();int s=0;bool sign=0;
	while(!isdigit(c)&&c^'-')c=getchar();
	if(c=='-')c=getchar(),sign=1;
	while(isdigit(c))s=(s<<1)+(s<<3)+c-'0',c=getchar();
	return sign?-s:s;
}
unordered_map<int,int> f[2];int I;
int a[MAX],n;
main(){
	n=icin();
	loop(n,i) a[i]=icin();
	f[I][a[1]-1]=0;
	for(register int i=1;i<n;++i){
		f[I^1].clear();
		for(auto it:f[I]){
			int j=it.first,b=it.second;
			if(j<a[i+1]) f[I^1][j]=f[I][j];
			else{
				f[I^1][a[i+1]-1]=max(f[I^1][a[i+1]-1],f[I][j]+i*(j-j%a[i+1]-a[i+1]));
				f[I^1][j%a[i+1]]=max(f[I^1][j%a[i+1]],f[I][j]+i*(j-j%a[i+1]));
			}
		}
		I^=1;
	}
	int ans=0;
	for(auto it:f[I]) ans=max(ans,n*it.first+it.second);
	cout<<ans<<'\n';
}


#include<bits/stdc++.h>
#define loop(n,i) for(register int i=1;i<=(n);++i)
#define zxc(x) cerr<<(#x)<<'='<<(x)<<'\n'
#define zxcv(x) cerr<<(#x)<<'='<<(x)<<','
#define zcx zxc
#define MAX 200009
#define int long long
using namespace std;
inline int icin(){
	char c=getchar();int s=0;bool sign=0;
	while(!isdigit(c)&&c^'-')c=getchar();
	if(c=='-')c=getchar(),sign=1;
	while(isdigit(c))s=(s<<1)+(s<<3)+c-'0',c=getchar();
	return sign?-s:s;
}
unordered_map<int,int> f[2];int I;
int a[MAX],n;
main(){
	n=icin();
	loop(n,i) a[i]=icin();
	f[I][a[1]-1]=0;
	for(register int i=1;i<n;++i){
		f[I^1].clear();
		for(auto it:f[I]){
			int j=it.first,b=it.second;
			if(j<a[i+1]) f[I^1][j]=f[I][j];
			else{
				f[I^1][a[i+1]-1]=max(f[I^1][a[i+1]-1],b+i*(j-j%a[i+1]-a[i+1]));
				f[I^1][j%a[i+1]]=max(f[I^1][j%a[i+1]],b+i*(j-j%a[i+1]));
			}
		}
		I^=1;
	}
	int ans=0;
	for(auto it:f[I]) ans=max(ans,n*it.first+it.second);
	cout<<ans<<'\n';
}


#include<bits/stdc++.h>
#define loop(n,i) for(register int i=1;i<=(n);++i)
#define zxc(x) cerr<<(#x)<<'='<<(x)<<'\n'
#define zxcv(x) cerr<<(#x)<<'='<<(x)<<','
#define zcx zxc
#define MAX 200009
#define int long long
using namespace std;
inline int icin(){
	char c=getchar();int s=0;bool sign=0;
	while(!isdigit(c)&&c^'-')c=getchar();
	if(c=='-')c=getchar(),sign=1;
	while(isdigit(c))s=(s<<1)+(s<<3)+c-'0',c=getchar();
	return sign?-s:s;
}
map<int,int> f;
int a[MAX],n;
main(){
	n=icin();
	loop(n,i) a[i]=icin();
	f[a[1]-1]=0;
	for(register int i=1;i<n;++i){
		while(f.begin()!=f.end()){
			int j=(--f.end())->first,b=(--f.end())->second;
			if(j<a[i+1]) break;
			f[a[i+1]-1]=max(f[a[i+1]-1],b+i*(j-j%a[i+1]-a[i+1]));
			f[j%a[i+1]]=max(f[j%a[i+1]],b+i*(j-j%a[i+1]));
			f.erase(--f.end());
		}
	}
	int ans=0;
	for(auto it:f) ans=max(ans,n*it.first+it.second);
	cout<<ans<<'\n';
}


#include<bits/stdc++.h>
#define loop(n,i) for(register int i=1;i<=(n);++i)
#define zxc(x) cerr<<(#x)<<'='<<(x)<<'\n'
#define zxcv(x) cerr<<(#x)<<'='<<(x)<<','
#define zcx zxc
#define MAX 10009
#define int long long
using namespace std;
inline int icin(){
	char c=getchar();int s=0;bool sign=0;
	while(!isdigit(c)&&c^'-')c=getchar();
	if(c=='-')c=getchar(),sign=1;
	while(isdigit(c))s=(s<<1)+(s<<3)+c-'0',c=getchar();
	return sign?-s:s;
}
unordered_map<int,int> f[2];int I;
int a[MAX],n;
main(){
	n=icin();
	loop(n,i) a[i]=icin();
	f[I][a[1]-1]=0;
	for(register int i=1;i<n;++i){
		f[I^1].clear();
		for(auto it:f[I]){
			int j=it.first,b=it.second;
			if(j<a[i+1]) f[I^1][j]=f[I][j];
			else{
				f[I^1][a[i+1]-1]=max(f[I^1][a[i+1]-1],f[I][j]+i*(j-j%a[i+1]-a[i+1]));
				f[I^1][j%a[i+1]]=max(f[I^1][j%a[i+1]],f[I][j]+i*(j-j%a[i+1]));
			}
		}
		I^=1;
	}
	int ans=0;
	for(auto it:f[I]) ans=max(ans,n*it.first+it.second);
	cout<<ans<<'\n';
}


#include<bits/stdc++.h>
#define loop(n,i) for(register int i=1;i<=(n);++i)
#define zxc(x) cerr<<(#x)<<'='<<(x)<<'\n'
#define zxcv(x) cerr<<(#x)<<'='<<(x)<<','
#define zcx zxc
#define MAX 200009
#define int long long
using namespace std;
inline int icin(){
	char c=getchar();int s=0;bool sign=0;
	while(!isdigit(c)&&c^'-')c=getchar();
	if(c=='-')c=getchar(),sign=1;
	while(isdigit(c))s=(s<<1)+(s<<3)+c-'0',c=getchar();
	return sign?-s:s;
}
map<int,int> f[2];int I;
int a[MAX],n;
main(){
	n=icin();
	loop(n,i) a[i]=icin();
	f[I][a[1]-1]=0;
	for(register int i=1;i<n;++i){
		f[I^1].clear();
		for(auto it:f[I]){
			int j=it.first,b=it.second;
			if(j<a[i+1]) f[I^1][j]=f[I][j];
			else{
				f[I^1][a[i+1]-1]=max(f[I^1][a[i+1]-1],b+i*(j-j%a[i+1]-a[i+1]));
				f[I^1][j%a[i+1]]=max(f[I^1][j%a[i+1]],b+i*(j-j%a[i+1]));
			}
		}
		I^=1;
	}
	int ans=0;
	for(auto it:f[I]) ans=max(ans,n*it.first+it.second);
	cout<<ans<<'\n';
}


#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=200010;
typedef long long ll;
ll n,ans,v;
map<ll,ll> f;
map<ll,ll>::iterator it;
inline ll rd()
{
    ll ret=0,f=1;   char gc=getchar();
    while(gc<'0'||gc>'9') {if(gc=='-')    f=-f;   gc=getchar();}
    while(gc>='0'&&gc<='9')   ret=ret*10+(gc^'0'),gc=getchar();
    return ret*f;
}
int main()
{
    n=rd();
    ll i,a,b;
    f[rd()-1]=0;
    for(i=2;i<=n;i++)
    {
        v=rd();
        while(f.begin()!=f.end())
        {
            it=f.end(),it--,a=(*it).first,b=(*it).second;
            if(a<v)  break;
            f[v-1]=max(f[v-1],b+(i-1)*(a-a%v-v));
            f[a%v]=max(f[a%v],b+(i-1)*(a-a%v));
            f.erase(it);
        }
    }
    for(it=f.begin();it!=f.end();it++)  ans=max(ans,n*((*it).first)+(*it).second);
    printf("%I64d",ans);
    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#define ddq map<ll, ll>::iterator
#define ll long long
using namespace std;
const int maxn = 500010, inf = 1e9 + 233;
int n;
ll a[maxn];
map<ll, ll>f[maxn];

template<typename T>
inline void read(T &k)
{
	int f = 1; k = 0; char c = getchar();
	while (c < '0' || c > '9') c == '-' && (f = -1), c = getchar();
	while (c <= '9' && c >= '0') k = k * 10 + c - '0', c = getchar();
	k *= f;
}

int main()
{
	read(n);
	for (int i = 1; i <= n; i++) read(a[i]);

	f[1][a[1] - 1] = 0;
	for (int i = 1; i <= n; i++)
	{
		for (ddq j = f[i].begin(); j != f[i].end(); j++)
		{
			ll w = j -> first;
			if (a[i] > w) f[i + 1][w] = max(f[i + 1][w], j -> second);
			else
			{
				f[i + 1][w % a[i]] = max(f[i + 1][w % a[i]], j -> second + 1ll * (i - 1) * (w - w % a[i]));
				f[i + 1][a[i] - 1] = max(f[i + 1][a[i] - 1], j -> second + 1ll * (i - 1) * ((w - (a[i] - 1)) / a[i] * a[i]));
			}
		}
		f[i].clear();
	}

	ll ans = 0;
	for (ddq j = f[n + 1].begin(); j != f[n + 1].end(); j++)
		ans = max(ans, j -> second + 1ll * n * j -> first);
	printf("%I64d\n", ans);
}


#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#define ddq map<ll, ll>::iterator
#define ll long long
using namespace std;
const int maxn = 500010, inf = 1e9 + 233;
int n;
ll a[maxn];
map<ll, ll>f[maxn];

template<typename T>
inline void read(T &k)
{
	int f = 1; k = 0; char c = getchar();
	while (c < '0' || c > '9') c == '-' && (f = -1), c = getchar();
	while (c <= '9' && c >= '0') k = k * 10 + c - '0', c = getchar();
	k *= f;
}

int main()
{
	read(n);
	for (int i = 1; i <= n; i++) read(a[i]);

	f[1][a[1] - 1] = 0;
	for (int i = 1; i <= n; i++)
	{
		for (ddq j = f[i].begin(); j != f[i].end(); j++)
		{
			ll w = j -> first;
			if (a[i] > w) f[i + 1][w] = max(f[i + 1][w], j -> second);
			else
			{
				f[i + 1][w % a[i]] = max(f[i + 1][w % a[i]], j -> second + 1ll * (i - 1) * (w - w % a[i]));
				f[i + 1][a[i] - 1] = max(f[i + 1][a[i] - 1], j -> second + 1ll * (i - 1) * ((w - (a[i] - 1)) / a[i] * a[i]));
			}
		}
	}

	ll ans = 0;
	for (ddq j = f[n + 1].begin(); j != f[n + 1].end(); j++)
		ans = max(ans, j -> second + 1ll * n * j -> first);
	printf("%I64d\n", ans);
}


#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#define ddq map<ll, ll>::reverse_iterator
#define ll long long
using namespace std;
const int maxn = 500010, inf = 1e9 + 233;
int n;
ll a[maxn];
map<ll, ll>f;

template<typename T>
inline void read(T &k)
{
	int f = 1; k = 0; char c = getchar();
	while (c < '0' || c > '9') c == '-' && (f = -1), c = getchar();
	while (c <= '9' && c >= '0') k = k * 10 + c - '0', c = getchar();
	k *= f;
}

int main()
{
	read(n);
	for (int i = 1; i <= n; i++) read(a[i]);

	f[a[1] - 1] = 0;
	for (int i = 1; i <= n; i++)
	{
		for (ddq j = f.rbegin(); j != f.rend(); j++)
		{
			ll w = j -> first;
			if (a[i] > w) break;
			else
			{
				f[w % a[i]] = max(f[w % a[i]], j -> second + 1ll * (i - 1) * (w - w % a[i]));
				f[a[i] - 1] = max(f[a[i] - 1], j -> second + 1ll * (i - 1) * ((w - (a[i] - 1)) / a[i] * a[i]));
			}
		}

		f.erase(f.lower_bound(a[i]), f.end());
	}

	ll ans = 0;
	for (ddq j = f.rbegin(); j != f.rend(); j++)
		ans = max(ans, j -> second + 1ll * n * j -> first);
	printf("%lld\n", ans);
}


#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#define ddq map<ll, ll>::iterator
#define ll long long
using namespace std;
const int maxn = 500010, inf = 1e9 + 233;
int n;
ll a[maxn];
map<ll, ll>f[maxn];

template<typename T>
inline void read(T &k)
{
	int f = 1; k = 0; char c = getchar();
	while (c < '0' || c > '9') c == '-' && (f = -1), c = getchar();
	while (c <= '9' && c >= '0') k = k * 10 + c - '0', c = getchar();
	k *= f;
}

int main()
{
	read(n);
	for (int i = 1; i <= n; i++) read(a[i]);

	f[1][a[1] - 1] = 0;
	for (int i = 1; i <= n; i++)
	{
		for (ddq j = f[i].begin(); j != f[i].end(); j++)
		{
			int w = j -> first;
			if (a[i] > w) f[i + 1][w] = max(f[i + 1][w], j -> second);
			else
			{
				f[i + 1][w % a[i]] = max(f[i + 1][w % a[i]], j -> second + 1ll * (i - 1) * (w - w % a[i]));
				f[i + 1][a[i] - 1] = max(f[i + 1][a[i] - 1], j -> second + 1ll * (i - 1) * ((w - (a[i] - 1)) / a[i] * a[i]));
			}
		}
	}

	ll ans = 0;
	for (ddq j = f[n + 1].begin(); j != f[n + 1].end(); j++)
		ans = max(ans, j -> second + 1ll * n * j -> first);
	printf("%I64d\n", ans);
}


#include<bits/stdc++.h>
#define pil pair<int,long long>
#define mp make_pair
#define X1 first
#define Y1 second
using namespace std;

int n;
long long a[200005];
map<pil,long long> g;
bool cmp(long long x,long long y){
	return x>y;
}
long long f(int i,long long x){
	if(g.count(mp(i,x)))return g[mp(i,x)];
	if(i==n-1)return (x-1)*n;
	long long &ans=g[mp(i,x)];i++;
	if(x<=a[i]){
		int u=upper_bound(a+i,a+n,x,cmp)-a;
		return ans=f(u-1,x);
	}
	long long tmp=f(i,a[i])+(x-x%a[i]-a[i])*i;
	if(x%a[i]){
		long long s=f(i,x%a[i])+(x-x%a[i])*i;
		tmp=max(tmp,s);
	}
	return ans=tmp;
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<n;i++)a[i]=min(a[i],a[i-1]);
	printf("%lld\n",f(0,a[0]));
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>

using namespace std;
typedef long long LL;

LL read()
{
	LL x = 0, f = 1; char c = getchar();
	while(!isdigit(c)) { if(c == '-') f = 0; c = getchar(); }
	while(isdigit(c)) { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); }
	return f ? x : -x;
}

map<LL, LL> f;
map<LL, LL>::iterator it;

int main()
{
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
#endif

	int n = read();
	f[read() - 1] = 0;
	for(int i = 2; i <= n; ++i)
	{
		LL v = read();
		for(it = --f.end(); ; f.erase(it--))
		{
			LL a = (*it).first, b = (*it).second;
			if(a < v) break;
			f[v - 1] = max(f[v - 1], b + (i - 1) * (a - a % v - v));
			f[a % v] = max(f[a % v], b + (i - 1) * (a - a % v));
		}
	}
	LL ans = 0;
	for(it = f.begin(); it != f.end(); ++it) ans = max(ans, n * (*it).first + (*it).second);
	cout << ans << endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>

using namespace std;
typedef long long LL;

int read()
{
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)) { if(c == '-') f = 0; c = getchar(); }
	while(isdigit(c)) { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); }
	return f ? x : -x;
}

map<LL, LL> f;
map<LL, LL>::iterator it;

int main()
{
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
#endif

	int n = read();
	f[read() - 1] = 0;
	for(int i = 2; i <= n; ++i)
	{
		int v = read();
		for(it = --f.end(); ; f.erase(it--))
		{
			LL a = (*it).first, b = (*it).second;
			if(a < v) break;
			f[v - 1] = max(f[v - 1], b + (i - 1) * (a - a % v - v));
			f[a % v] = max(f[a % v], b + (i - 1) * (a - a % v));
		}
	}
	LL ans = 0;
	for(it = f.begin(); it != f.end(); ++it) ans = max(ans, n * (*it).first + (*it).second);
	cout << ans << endl;

	return 0;
}


#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <list>
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

const int MAXN=200000;

typedef struct G {
	ll len;
	map<ll,ll> hor,ver;

	void init(ll _len) {
		len=_len; hor.clear(); ver.clear(); hor[0]=0; ver[0]=0;
		//printf("init:"); for(auto it=hor.begin();it!=hor.end();++it) printf(" (%lld,%lld)",it->first,it->second); puts("");
	}
	void resize(ll _len) {
		len=_len; auto it=hor.lower_bound(len); while(it!=hor.end()) { ver.erase(it->second); auto prv=it++; hor.erase(prv); }
		//printf("resize(%lld):",_len); for(auto it=hor.begin();it!=hor.end();++it) printf(" (%lld,%lld)",it->first,it->second); puts("");
	}
	void update(ll x,ll y) {
		//printf("updating(%lld,%lld)\n",x,y);
		ll sx,py,ny,nx; auto it=ver.lower_bound(y); py=it==ver.end()?LLONG_MAX:it->first; if(it==ver.begin()) return; --it; sx=it->second; if(sx>x) return;
		while(it!=ver.begin()&&it->second<=x) { ny=it->first; hor.erase(it->second); auto prv=it--; ver.erase(prv); }
		if(it==ver.begin()&&it->second<=x) { ny=it->first; hor.erase(it->second); ver.erase(it); nx=len; } else nx=it->second;
		if(y!=py) hor[sx]=y,ver[y]=sx;
		if(x+1!=nx) hor[x+1]=ny,ver[ny]=x+1;
		//printf("update(%lld,%lld):",x,y); for(auto it=hor.begin();it!=hor.end();++it) printf(" (%lld,%lld)",it->first,it->second); puts("");
	}
	vector<pair<ll,ll> > getfrom(ll x) {
		vector<pair<ll,ll> > ret;
		auto it=hor.lower_bound(x); if(it==hor.end()||it->first>x) --it;
		ret.PB(MP(x,it->second)); ++it; while(it!=hor.end()) { ret.PB(MP(it->first,it->second)); ++it; }
		ret.PB(MP(len,0LL));
		//printf("getfrom(%lld):",x); REPSZ(i,ret) printf(" (%lld,%lld)",ret[i].first,ret[i].second); puts("");
		return ret;
	}
	ll calc(int mlt) {
		ll ret=LLONG_MIN;
		for(auto it=hor.begin();it!=hor.end();++it) {
			ll y=it->second; auto nxt=it; ++nxt; ll x=nxt==hor.end()?len-1:nxt->first-1;
			//printf("x=%lld y=%lld\n",x,y);
			ll cur=y+x*mlt; if(cur>ret) ret=cur;
		}
		return ret;
	}
	void print(int a) {
		printf("hor%d:",a); for(auto it=hor.begin();it!=hor.end();++it) printf(" (%lld,%lld)",it->first,it->second); puts("");
		printf("ver%d:",a); for(auto it=ver.begin();it!=ver.end();++it) printf(" (%lld,%lld)",it->first,it->second); puts("");
	}

	/*vector<ll> val;
	void init(ll _len) { len=_len; val=vector<ll>(len,0); }
	void resize(ll _len) { len=_len; while(SZ(val)>len) val.pop_back(); }
	void update(ll x,ll y) { REPE(xx,x) val[xx]=max(val[xx],y); }
	vector<pair<ll,ll> > getfrom(ll x) {
		vector<pair<ll,ll> > ret;
		ret.PB(MP(x,val[x]));
		FORSZ(xx,x+1,val) if(val[xx]!=val[xx-1]) ret.PB(MP(xx,val[xx]));
		ret.PB(MP(len,0));
		return ret;
	}
	ll calc(int mlt) { ll ret=LLONG_MIN; REPSZ(i,val) ret=max(ret,val[i]+(ll)i*mlt); return ret; }
	void print(int a) { printf("%d:",a); REPSZ(i,val) printf(" %lld",val[i]); puts(""); }
	*/

} G;

int n;
ll a[MAXN];

G g;

void run() {
	scanf("%d",&n); REP(i,n) scanf("%lld",&a[i]);
	g.init(a[0]);
	FOR(i,1,n) if(a[i]<g.len) {
		vector<pair<ll,ll> > p=g.getfrom(a[i]);
		g.resize(a[i]);
		FORSZ(j,1,p) {
			ll px=p[j-1].first,x=p[j].first-1,y=p[j-1].second;
			g.update(x%a[i],y+(x/a[i])*i*a[i]);
			if(px/a[i]!=x/a[i]) g.update(a[i]-1,y+((x/a[i])-1)*i*a[i]);
		}
		//g.print(i);
	}
	ll ret=g.calc(n);
	printf("%lld\n",ret);

	/*vector<ll> best(a[0],0); REPSZ(j,best) best[j]+=j;
	FOR(i,1,n) {
		if(a[i]>=SZ(best)) {
			REPSZ(j,best) best[j]+=j;
		} else {
			vector<ll> nbest(a[i],0); REPSZ(j,best) { int nj=j%a[i]; nbest[nj]=max(nbest[nj],best[j]+nj); } best=nbest;
		}
		printf("%d:",i); ll prv=LLONG_MIN; REPSZ(j,best) { ll cur=best[j]-(i+1)*j; if(cur!=prv) printf(" (%lld,%lld)",(ll)j,cur); prv=cur; } puts("");
	}*/
}

int main() {
	run();
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<map>
#define LL long long
using namespace std;
typedef map<LL,LL>::iterator it;
LL n,x,y,sl,ans,a[200010];
map<LL,LL> dp;
char gc()
{
    return getchar();
    static char buf[1<<16],*S,*T;
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(S==T)return EOF;}
    return *S++;
}
LL rd()
{
    sl=0;
    char ch=gc();
    while(ch<'0'||'9'<ch) ch=gc();
    while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=gc();
    return sl;
}
int main()
{
    n=rd();
    for(int i=1;i<=n;++i) a[i]=rd();
    dp[a[1]-1]=0;
    for(int i=1;i<n;++i)
        for(it j=dp.lower_bound(a[i+1]);j!=dp.end();dp.erase(j++))
        {
            x=j->first;y=j->second;
            dp[x%a[i+1]]=max(dp[x%a[i+1]],y+i*(x-x%a[i+1]));
            dp[a[i+1]-1]=max(dp[a[i+1]-1],y+i*((x+1)/a[i+1]*a[i+1]-a[i+1]));
        }
    for(it i=dp.begin();i!=dp.end();++i)
    {
        x=i->first;y=i->second;
        ans=max(ans,x*n+y);
    }
    printf("%lld\n",ans);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <map>

#define X first
#define Y second
#define mp std::make_pair
#define MAX_N 200010
typedef long long ll;

int n;
ll a[MAX_N];

std::map <ll, ll> f[2];

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
		scanf("%I64d", &a[i]);
	f[1].insert(mp(a[1] - 1, 0));
	for(int i = 2, t = 0; i <= n; i++, t ^= 1){
		f[t].clear();
		for(auto j: f[t^1])
			if(j.X < a[i])
				f[t][j.X] = std::max(f[t][j.X], j.Y);
			else{
				f[t][j.X%a[i]] = std::max(f[t][j.X%a[i]], 1ll * (i-1) * (j.X-j.X%a[i]));
				f[t][a[i] - 1] = std::max(f[t][a[i] - 1], 1ll * (i-1) * ((j.X + 1) / a[i] * a[i] - a[i]));
			}
	}
	ll ans = 0;
	for(auto i: f[n&1])
		ans = std::max(ans, 1ll * n * i.X + i.Y);
	printf("%I64d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <map>

#define X first
#define Y second
#define mp std::make_pair
#define MAX_N 200010
typedef long long ll;

int n;
ll a[MAX_N];

std::map <ll, ll> f;
std::map <ll, ll> :: iterator it;

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
		scanf("%I64d", &a[i]);
	f.insert(mp(a[1] - 1, 0));
	for(int i = 2; i <= n; i++){
		for(it = f.lower_bound(a[i]); it != f.end(); f.erase(it++)){
			f[it->X%a[i]] = std::max(f[it->X%a[i]], it->Y + 1ll*(i-1) * (it->X-it->X%a[i]));
			f[a[i] - 1] = std::max(f[a[i] - 1], it->Y + 1ll*(i-1) * ((it->X + 1) / a[i] * a[i] - a[i]));
		}
	}
	ll ans = 0;
	for(auto i: f)
		ans = std::max(ans, 1ll * n * i.X + i.Y);
	printf("%I64d\n", ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <map>

#define X first
#define Y second
#define mp std::make_pair
#define MAX_N 200010
typedef long long ll;

int n;
ll a[MAX_N];

std::map <ll, ll> f[2];

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
		scanf("%I64d", &a[i]);
	f[1].insert(mp(a[1] - 1, 0));
	for(int i = 2, t = 0; i <= n; i++, t ^= 1){
		f[t].clear();
		for(auto j: f[t^1])
			if(j.X < a[i])
				f[t][j.X] = std::max(f[t][j.X], j.Y);
			else{
				f[t][j.X%a[i]] = std::max(f[t][j.X%a[i]], j.Y + 1ll*(i-1) * (j.X-j.X%a[i]));
				f[t][a[i] - 1] = std::max(f[t][a[i] - 1], j.Y + 1ll*(i-1) * ((j.X + 1) / a[i] * a[i] - a[i]));
			}
	}
	ll ans = 0;
	for(auto i: f[n&1])
		ans = std::max(ans, 1ll * n * i.X + i.Y);
	printf("%I64d\n", ans);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
int n;
ll a[200005];
map<ll,ll> mp;
int main(){
	scanf("%d",&n);
	For(i,1,n) scanf("%lld",&a[i]);
	mp.insert(pll(a[1],0));
	For(i,1,n-1){
		auto j=mp.upper_bound(a[i+1]);
		for (;j!=mp.end();){
			ll &f0=mp[a[i+1]];
			f0=max(f0,j->se+(j->fi/a[i+1]-1)*a[i+1]*i);
			if (j->fi%(a[i+1])){
				ll &f1=mp[j->fi%a[i+1]];
				f1=max(f1,j->se+j->fi/a[i+1]*a[i+1]*i);
			}
			mp.erase(j++);
		}
	}
	ll ans=0;
	for (auto i:mp)
		ans=max(ans,n*i.fi+i.se-n);
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define sqr(x) ((x)*(x))
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define ld long double
#define vi vector<int>
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
#define pii pair<ll,ll>
#define pb push_back
#define mp make_pair
#define debuge cerr<<"isok"<<endl
#define debug(x) cerr<<#x<<"="<<x<<endl
#define dprintf(...) fprintf(stderr,__VA_ARGS__)
#define SS second
#define FF first
#define ls (k<<1)
#define rs (k<<1|1)
#define clr(a,x) memset(a,x,sizeof(a))
#define cpy(a,x) memcpy(a,x,sizeof(a))
#define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
#define SZ(x) ((int)x.size())
using namespace std;
template<class T> inline void gmin(T &x,const T &y){if(x>y) x=y;}
template<class T> inline void gmax(T &x,const T &y){if(x<y) x=y;}
const int BufferSize=1<<16;
char buffer[BufferSize],*Bufferhead,*Buffertail;
bool Terminal;
inline char Getchar(){
	if(Bufferhead==Buffertail){
		int l=fread(buffer,1,BufferSize,stdin);
		if(!l){Terminal=1;return 0;}
		Buffertail=(Bufferhead=buffer)+l;
	}
	return *Bufferhead++;
}
template<class T>inline bool read(T &x){
	x=0;char c=Getchar(),rev=0;
	while(c<'0'||c>'9'){rev|=c=='-';c=Getchar();if(Terminal)return 0;}
	while(c>='0'&&c<='9') x=x*10+c-'0',c=Getchar();
	if(c=='.'){
		c=Getchar();double t=0.1;
		while(c>='0'&&c<='9') x=x+(c-'0')*t,c=Getchar(),t=t/10;
	}
	x=rev?-x:x;
	return 1;
}
template<class T1,class T2> inline bool read(T1 &x,T2 &y){return read(x)&read(y);}
template<class T1,class T2,class T3> inline bool read(T1 &x,T2 &y,T3 &z){return read(x)&read(y)&read(z);}
template<class T1,class T2,class T3,class T4> inline bool read(T1 &x,T2 &y,T3 &z,T4 &w){return read(x)&read(y)&read(z)&read(w);}
inline bool reads(char *x){
	char c=Getchar();
	while(c<33||c>126){c=Getchar();if(Terminal)return 0;}
	while(c>=33&&c<=126) (*x++)=c,c=Getchar();
	*x=0;return 1;
}
template<class T>inline void print(T x,const char c='\n'){
	if(!x){putchar('0');putchar(c);return;}
	if(x<0) putchar('-'),x=-x;
	int m=0,a[20];
	while(x) a[m++]=x%10,x/=10;
	while(m--) putchar(a[m]+'0');
	putchar(c);
}
//--------------------------------head---------------------------------------------

const ll INF=1ll<<60;
const int inf=0x3f3f3f3f;
const int N=200005,M=100005,mod=1e9+7;
template<class T,class S> inline void ch(T &x,const S y){x=(x+y)%mod;}
inline int exp(int x,int y,const int mod=::mod){
	int ans=1;
	while(y){
		if(y&1) ans=(ll)ans*x%mod;
		x=(ll)x*x%mod;y>>=1;
	}return ans;
}

int n;
ll a;
set<pii> s;

int main(){
#ifdef rqgao2014
	freopen("input.txt","r",stdin);
#endif
	read(n,a);
	s.insert(mp(a-1,0));
	for(int i=2;i<=n;i++){
		read(a);
//		ll ans=0;
//		for(auto j:s) gmax(ans,j.FF*(i-1)+j.SS);
//		print(ans);
		vector<pii> del(0);
		set<pii>::iterator it=s.lower_bound(mp(a,0)),it2=it;
		ll mx=-INF;
		while(it!=s.end()){
			ll r=(*it).FF,k=(*it).SS;
			if(r%a!=a-1) s.insert(mp(r%a,k+(r/a)*a*(i-1)));
			gmax(mx,k+max(0ll,(r+1)/a-1)*a*(i-1));
			del.pb(*it);it++;
		}
		if(mx>=0) s.insert(mp(a-1,mx));
		for(auto i:del) s.erase(i);
	}
	ll ans=0;
	for(auto i:s) gmax(ans,i.FF*n+i.SS);
	print(ans);
	return 0;
}


#include<cstdio>
#include<map>
using namespace std;
const int N=200010;
int n;
long long Mx,ans,a[N];
map<long long,long long> mp;
int main() {
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
	mp[a[1]-1]=0; Mx=a[1];
	for (int i=1;i<n;i++) {
		for (map<long long,long long>::iterator j=mp.begin();j!=mp.end();++j) {
			long long x=j->first, y=j->second;
			if (x>=Mx) break;
			//printf("f[%d][%lld]=%lld\n",i,x,y);
			if (x>=a[i+1]) {
				mp[x%a[i+1]]=max(mp[x%a[i+1]],y+1ll*i*(x-x%a[i+1]));
				mp[a[i+1]-1]=max(mp[a[i+1]-1],y+1ll*i*((x-a[i+1]+1)/a[i+1]*a[i+1]));
			}
		}
		Mx=min(Mx,a[i+1]);
	}
	for (map<long long,long long>::iterator i=mp.begin();i!=mp.end();++i) {
		long long x=i->first, y=i->second;
		if (x>=Mx) break;
		//printf("f[%d][%lld]=%lld\n",n,x,y);
		ans=max(ans,y+x*n);
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<tr1/unordered_map>
using namespace std;
const int N=200010;
int n;
long long ans,a[N];
tr1::unordered_map<long long,long long> mp[N];
int main() {
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
	mp[1][a[1]-1]=0;
	for (int i=1;i<n;i++) {
		for (tr1::unordered_map<long long,long long>::iterator j=mp[i].begin();j!=mp[i].end();++j) {
			long long x=j->first, y=j->second;
			//printf("f[%d][%lld]=%lld\n",i,x,y);
			if (x<a[i+1]) mp[i+1][x]=max(mp[i+1][x],y);
			else {
				mp[i+1][x%a[i+1]]=max(mp[i+1][x%a[i+1]],y+1ll*i*(x-x%a[i+1]));
				mp[i+1][a[i+1]-1]=max(mp[i+1][a[i+1]-1],y+1ll*i*((x-a[i+1]+1)/a[i+1]*a[i+1]));
			}
		}
	}
	for (tr1::unordered_map<long long,long long>::iterator i=mp[n].begin();i!=mp[n].end();++i) {
		long long x=i->first, y=i->second;
		//printf("f[%d][%lld]=%lld\n",n,x,y);
		ans=max(ans,y+x*n);
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<map>
using namespace std;
const int N=200010;
int n;
long long Mx,ans,a[N];
map<long long,long long> mp;
int main() {
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
	mp[a[1]-1]=0; Mx=a[1];
	for (int i=1;i<n;i++) {
		for (map<long long,long long>::iterator j=mp.begin();j!=mp.end();++j) {
			long long x=j->first, y=j->second;
			if (x>=Mx) break;
			//printf("f[%d][%lld]=%lld\n",i,x,y);
			if (x<a[i+1]) continue;
			else {
				mp[x%a[i+1]]=max(mp[x%a[i+1]],y+1ll*i*(x-x%a[i+1]));
				mp[a[i+1]-1]=max(mp[a[i+1]-1],y+1ll*i*((x-a[i+1]+1)/a[i+1]*a[i+1]));
			}
			Mx=min(Mx,a[i+1]);
		}
	}
	for (map<long long,long long>::iterator i=mp.begin();i!=mp.end();++i) {
		long long x=i->first, y=i->second;
		if (x>=Mx) break;
		//printf("f[%d][%lld]=%lld\n",n,x,y);
		ans=max(ans,y+x*n);
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<tr1/unordered_map>
using namespace std;
const int N=200010;
int n,a[N];
long long ans;
tr1::unordered_map<long long,long long> mp[N];
int main() {
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	mp[1][a[1]-1]=0;
	for (int i=1;i<n;i++) {
		for (tr1::unordered_map<long long,long long>::iterator j=mp[i].begin();j!=mp[i].end();++j) {
			long long x=j->first, y=j->second;
			//printf("f[%d][%lld]=%lld\n",i,x,y);
			if (x<a[i+1]) mp[i+1][x]=max(mp[i+1][x],y);
			else {
				mp[i+1][x%a[i+1]]=max(mp[i+1][x%a[i+1]],y+1ll*i*(x-x%a[i+1]));
				mp[i+1][a[i+1]-1]=max(mp[i+1][a[i+1]-1],y+1ll*i*((x-a[i+1]+1)/a[i+1]*a[i+1]));
			}
		}
	}
	for (tr1::unordered_map<long long,long long>::iterator i=mp[n].begin();i!=mp[n].end();++i) {
		long long x=i->first, y=i->second;
		//printf("f[%d][%lld]=%lld\n",n,x,y);
		ans=max(ans,y+x*n);
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<map>
using namespace std;
const int N=200010;
int n;
long long Mx,ans,a[N];
map<long long,long long> mp;
int main() {
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
	mp[a[1]-1]=0; Mx=a[1];
	for (int i=1;i<n;i++) {
		for (map<long long,long long>::iterator j=mp.lower_bound(a[i+1]);j!=mp.end();++j) {
			long long x=j->first, y=j->second;
			if (x>=Mx) break;
			//printf("f[%d][%lld]=%lld\n",i,x,y);
			if (x>=a[i+1]) {
				mp[x%a[i+1]]=max(mp[x%a[i+1]],y+1ll*i*(x-x%a[i+1]));
				mp[a[i+1]-1]=max(mp[a[i+1]-1],y+1ll*i*((x-a[i+1]+1)/a[i+1]*a[i+1]));
			}
		}
		Mx=min(Mx,a[i+1]);
	}
	for (map<long long,long long>::iterator i=mp.begin();i!=mp.end();++i) {
		long long x=i->first, y=i->second;
		if (x>=Mx) break;
		//printf("f[%d][%lld]=%lld\n",n,x,y);
		ans=max(ans,y+x*n);
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<tr1/unordered_map>
using namespace std;
const int N=200010;
int n;
long long ans,a[N];
tr1::unordered_map<long long,long long> mp[2];
int main() {
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
	mp[1][a[1]-1]=0;
	for (int i=1;i<n;i++) {
		for (tr1::unordered_map<long long,long long>::iterator j=mp[i&1].begin();j!=mp[i&1].end();++j) {
			long long x=j->first, y=j->second;
			//printf("f[%d][%lld]=%lld\n",i,x,y);
			if (x<a[i+1]) mp[(i&1)^1][x]=max(mp[(i&1)^1][x],y);
			else {
				mp[(i&1)^1][x%a[i+1]]=max(mp[(i&1)^1][x%a[i+1]],y+1ll*i*(x-x%a[i+1]));
				mp[(i&1)^1][a[i+1]-1]=max(mp[(i&1)^1][a[i+1]-1],y+1ll*i*((x-a[i+1]+1)/a[i+1]*a[i+1]));
			}
		}
		mp[i&1].clear();
	}
	for (tr1::unordered_map<long long,long long>::iterator i=mp[n&1].begin();i!=mp[n&1].end();++i) {
		long long x=i->first, y=i->second;
		//printf("f[%d][%lld]=%lld\n",n,x,y);
		ans=max(ans,y+x*n);
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<map>
using namespace std;
const int N=200010;
int n;
long long Mx,ans,a[N];
map<long long,long long> mp;
int main() {
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
	mp[a[1]-1]=0; Mx=1e14;
	for (int i=1;i<n;i++) {
		for (map<long long,long long>::iterator j=mp.begin();j!=mp.end();++j) {
			long long x=j->first, y=j->second;
			//printf("f[%d][%lld]=%lld\n",i,x,y);
			if (x>=Mx) break;
			if (x<a[i+1]) break;
			else {
				mp[x%a[i+1]]=max(mp[x%a[i+1]],y+1ll*i*(x-x%a[i+1]));
				mp[a[i+1]-1]=max(mp[a[i+1]-1],y+1ll*i*((x-a[i+1]+1)/a[i+1]*a[i+1]));
			}
			Mx=min(Mx,a[i+1]);
		}
	}
	for (map<long long,long long>::iterator i=mp.begin();i!=mp.end();++i) {
		long long x=i->first, y=i->second;
		if (x>=Mx) break;
		//printf("f[%d][%lld]=%lld\n",n,x,y);
		ans=max(ans,y+x*n);
	}
	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef map<ll,ll>::iterator mt;
void upd(ll & x,ll y){x=max(x,y);}
ll g[200005];
map<ll,ll> dp[2];
int main()
{
	//freopen("cf.in","r",stdin);
	ios::sync_with_stdio(0);
	int n; cin>>n;
	for(int i=1;i<=n;i++)
		cin>>g[i];

	int now=1; dp[now][g[1]-1]=0;
	for(int i=1;i<n;i++) {
		now^=1;
		dp[now].clear();
		mt it=dp[now^1].begin();
		for(;it!=dp[now^1].end() && it->first<g[i+1];it++)
			dp[now].insert(*it);

		for(;it!=dp[now^1].end();it++) {
			ll r=it->first; ll k=it->second;
			if(r<g[i+1])
				upd(dp[now][r],k);
			else {
				upd(dp[now][r%g[i+1]],k+(ll)(r-r%g[i+1])*i);
				upd(dp[now][g[i+1]-1],k+(ll)(r-r%g[i+1]-g[i+1])*i);
			}
		}
	}
	ll ans=0;
	for(mt it=dp[now].begin();it!=dp[now].end();it++)
		ans=max(ans,(ll)it->first*n+it->second);
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef map<ll,ll>::iterator mt;
void upd(ll & x,ll y){x=max(x,y);}
ll g[200005];
map<ll,ll> dp[2];
int main()
{
	ios::sync_with_stdio(0);
	int n; cin>>n;
	for(int i=1;i<=n;i++)
		cin>>g[i];

	int now=1; dp[now^1][g[1]-1]=0;
	for(int i=1;i<n;i++) {
		dp[now].clear();

		for(mt nxt,it=dp[now^1].upper_bound(g[i+1]-1);it!=dp[now^1].end();it=nxt) {
			ll r=it->first; ll k=it->second;
			if(r<g[i+1])
				upd(dp[now][r],k);
			else {
				upd(dp[now][r%g[i+1]],k+(ll)(r-r%g[i+1])*i);
				upd(dp[now][g[i+1]-1],k+(ll)(r-r%g[i+1]-g[i+1])*i);
			}
			it++; nxt=it; it--;
			dp[now^1].erase(it);
		}
		for(mt it=dp[now].begin();it!=dp[now].end();it++)
			upd(dp[now^1][it->first],it->second);
	}
	ll ans=0;
	for(mt it=dp[now^1].begin();it!=dp[now^1].end();it++)
		ans=max(ans,(ll)it->first*n+it->second);
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef map<ll,ll>::iterator mt;
void upd(ll & x,ll y){x=max(x,y);}
ll g[200005];
map<ll,ll> dp[2];
int main()
{
	ios::sync_with_stdio(0);
	int n; cin>>n;
	for(int i=1;i<=n;i++)
		cin>>g[i];

	int now=1; dp[now][g[1]-1]=0;
	for(int i=1;i<n;i++) {
		now^=1; dp[now]=dp[now^1];
		for(mt it=dp[now].upper_bound(g[i+1]);it!=dp[now].end();it++)
			dp[now].erase(it);
		for(mt it=dp[now^1].lower_bound(g[i+1]);it!=dp[now^1].end();it++) {
			ll r=it->first; ll k=it->second;
			if(r<g[i+1])
				upd(dp[now][r],k);
			else {
				upd(dp[now][r%g[i+1]],k+(ll)(r-r%g[i+1])*i);
				upd(dp[now][g[i+1]-1],k+(ll)(r-r%g[i+1]-g[i+1])*i);
			}
		}
	}
	ll ans=0;
	for(mt it=dp[now].begin();it!=dp[now].end();it++)
		ans=max(ans,(ll)it->first*n+it->second);
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef map<ll,ll>::iterator mt;
void upd(ll & x,ll y){x=max(x,y);}
ll g[200005];
map<ll,ll> dp[2];
int main()
{
	//freopen("cf.in","r",stdin);
	ios::sync_with_stdio(0);
	int n; cin>>n;
	for(int i=1;i<=n;i++)
		cin>>g[i];

	int now=1; dp[now][g[1]-1]=0;
	for(int i=1;i<n;i++) {
		now^=1;
		//dp[now]=dp[now^1];
		dp[now].clear();
		for(mt it=dp[now].upper_bound(g[i+1]);it!=dp[now].end();it++)
			dp[now].erase(it);
		for(mt it=dp[now^1].begin();it!=dp[now^1].end();it++) {
			ll r=it->first; ll k=it->second;
			if(r<g[i+1])
				upd(dp[now][r],k);
			else {
				upd(dp[now][r%g[i+1]],k+(ll)(r-r%g[i+1])*i);
				upd(dp[now][g[i+1]-1],k+(ll)(r-r%g[i+1]-g[i+1])*i);
			}
		}
	}
	ll ans=0;
	for(mt it=dp[now].begin();it!=dp[now].end();it++)
		ans=max(ans,(ll)it->first*n+it->second);
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unordered_map<ll,ll>::iterator mt;
void upd(ll & x,ll y){x=max(x,y);}
ll g[200005];
unordered_map<ll,ll> dp[2];
int main()
{
	ios::sync_with_stdio(0);
	int n; cin>>n;
	for(int i=1;i<=n;i++)
		cin>>g[i];

	int now=1; dp[now][g[1]-1]=0;
	for(int i=1;i<n;i++) {
		now^=1; dp[now].clear();
		for(mt it=dp[now^1].begin();it!=dp[now^1].end();it++) {
			ll r=it->first; ll k=it->second;
			if(r<g[i+1])
				upd(dp[now][r],k);
			else {
				upd(dp[now][r%g[i+1]],k+(ll)(r-r%g[i+1])*i);
				upd(dp[now][g[i+1]-1],k+(ll)(r-r%g[i+1]-g[i+1])*i);
			}
		}
	}
	ll ans=0;
	for(mt it=dp[now].begin();it!=dp[now].end();it++)
		ans=max(ans,(ll)it->first*n+it->second);
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef map<ll,ll>::iterator mt;
void upd(ll & x,ll y){x=max(x,y);}
ll g[200005];
map<ll,ll> dp[2];
int main()
{
	ios::sync_with_stdio(0);
	int n; cin>>n;
	for(int i=1;i<=n;i++)
		cin>>g[i];

	int now=1; dp[now][g[1]-1]=0;
	for(int i=1;i<n;i++) {
		cerr<<i<<' '<<dp[now].size()<<endl;
		now^=1; dp[now]=dp[now^1];
		for(mt it=dp[now].upper_bound(g[i+1]);it!=dp[now].end();it++)
			dp[now].erase(it);
		for(mt it=dp[now^1].lower_bound(g[i+1]);it!=dp[now^1].end();it++) {
			ll r=it->first; ll k=it->second;
			if(r<g[i+1])
				upd(dp[now][r],k);
			else {
				upd(dp[now][r%g[i+1]],k+(ll)(r-r%g[i+1])*i);
				upd(dp[now][g[i+1]-1],k+(ll)(r-r%g[i+1]-g[i+1])*i);
			}
		}
	}
	ll ans=0;
	for(mt it=dp[now].begin();it!=dp[now].end();it++)
		ans=max(ans,(ll)it->first*n+it->second);
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef map<ll,ll>::iterator mt;
void upd(ll & x,ll y){x=max(x,y);}
ll g[200005];
map<ll,ll> dp[2];
int main()
{
	ios::sync_with_stdio(0);
	int n; cin>>n;
	for(int i=1;i<=n;i++)
		cin>>g[i];

	int now=1; dp[now^1][g[1]-1]=0;
	for(int i=1;i<n;i++) {
		dp[now].clear();

		for(mt nxt,it=dp[now^1].upper_bound(g[i+1]-1);it!=dp[now^1].end();it=nxt) {
			ll r=it->first; ll k=it->second;
			if(r<g[i+1])
				upd(dp[now][r],k);
			else {
				upd(dp[now][r%g[i+1]],k+(ll)(r-r%g[i+1])*i);
				upd(dp[now][g[i+1]-1],k+(ll)(r-r%g[i+1]-g[i+1])*i);
			}
			it++; nxt=it; it--;
			dp[now^1].erase(it);
		}
		for(mt it=dp[now].begin();it!=dp[now].end();it++)
			dp[now^1].insert(*it);
	}
	ll ans=0;
	for(mt it=dp[now].begin();it!=dp[now].end();it++)
		ans=max(ans,(ll)it->first*n+it->second);
	cout<<ans<<endl;
	return 0;
}


#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

typedef long long ll;

ll read() {
    char ch;
    for(ch=getchar();ch<'0'||ch>'9';ch=getchar());
    ll x=ch-'0';
    for(ch=getchar();ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
    return x;
}

const int N=2*1e5+5;

struct node{
    ll j,k;
    node(ll _j=0,ll _k=0) {j=_j;k=_k;}
    friend bool operator < (node x,node y) {return x.j>y.j||x.j==y.j&&x.k>y.k;}
};

set<node> f;

typedef set<node> :: iterator it;
ll a[N];

int main() {
    int n=read();
    fo(i,1,n) a[i]=read();
    f.insert(node(a[1]-1,0));
    fo(i,1,n-1) {
        ll m=a[i+1],Mx=0;bool pty=0;
        while (1) {
            it pos=f.begin();
            ll j=(*pos).j,k=(*pos).k;
            if (j<m) break;
            pty=1;
            Mx=max(Mx,k+(ll)i*((j+1)/m*m-m));
            f.insert(node(j%m,k+(ll)i*(j-j%m)));
            f.erase(pos);
        }
        if (pty) f.insert(node(m-1,Mx));
    }
    ll ans=0;
    for(it pos=f.begin();pos!=f.end();pos++) {
        ll j=(*pos).j,k=(*pos).k;
        ans=max(ans,k+(ll)j*n);
    }
    printf("%lld\n",ans);
}


#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
#pragma GCC target("popcnt")
#include <bits/stdc++.h>
#define maxn 200010
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define pii pair<int, int>
#define pil pair<int, ll>
#define pll pair<ll, ll>
#define IL inline
#define ss system
using namespace std;

int n;
ll a[maxn], ans = 0;
map<ll, ll> M;
template <class T> void read (T &x) {
  char ch = x = 0;
  bool fl = false;
  while (!isdigit(ch))
    fl |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x = fl ? -x : x;
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    read(a[i]);
  }
  M[1LL << 60] = 0;
  for (int i = 1; i <= n; i++) {
    for (auto it = M.lower_bound(a[i]); it != M.end(); it = M.erase(it)) {
      ll x = it -> fi, y = it -> se;
      M[x % a[i]] = max(M[x % a[i]], y + 1LL * (i - 1) * (x - x % a[i]));
      M[a[i] - 1] = max(M[a[i] - 1], y + 1LL * (i - 1) * ((x + 1) / a[i] * a[i] - a[i]));
    }
  }
  for (auto it = M.begin(); it != M.end(); ++it) {
    ans = max(ans, it -> se + 1LL * n * it -> fi);
  }
  printf("%lld\n", ans);
  return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=200000+10;
int n;ll a[maxn];map<ll,ll> f;

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
	f[a[1]-1]=0;
	map<ll,ll>::iterator it;
	for(int i=2;i<=n;i++)
		for(it=f.lower_bound(a[i]);it!=f.end();f.erase(it++))
		{
			ll v=it->first;
			f[v%a[i]]=max(f[v%a[i]],it->second+(i-1)*(v-v%a[i]));
			f[a[i]-1]=max(f[a[i]-1],it->second+(i-1)*(((v+1)/a[i])*a[i]-a[i]));
		}
	ll ans=0;
	for(it=f.begin();it!=f.end();it++)
		ans=max(ans,it->first*n+it->second);
	printf("%I64d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define N 222222
using namespace std;
int n,i,j;
LL x,S,f[N],a[N];
int fd(LL x){
	int l=1,r=n+1,md,an;
	if(x<=a[n])return n+1;
	for(;l<=r;)if(x>a[md=l+r>>1])r=md-1,an=md;else l=md+1;
	return an;
}
int main(){
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%I64d",&a[i]);
	for(i=2;i<=n;i++)a[i]=min(a[i],a[i-1]);a[n+1]=1;
	for(i=n;i;i--)for(x=a[i],S=0;x;x%=a[j]){
		j=fd(x);
		f[i]=max(f[i],f[j]+(x-x%a[j]-a[j])*(j-1)+S);
		S+=(x-x%a[j])*(j-1);
	}
	printf("%I64d",f[1]);
}


#include<bits/stdc++.h>
#define LL long long
#define N 222222
using namespace std;
int n,i,j;
LL x,S,f[N],a[N];
int fd(LL x){
	int l=1,r=n+1,md,an;
	if(x<=a[n])return n+1;
	for(;l<=r;)if(x>a[md=l+r>>1])r=md-1,an=md;else l=md+1;
	return an;
}
int main(){
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]);
	for(i=2;i<=n;i++)a[i]=min(a[i],a[i-1]);a[n+1]=1;
	for(i=n;i;i--){
		for(x=a[i],S=0;x;x%=a[j]){
			j=fd(x);
			f[i]=max(f[i],f[j]+(x-x%a[j]-a[j])*(j-1)+S);
			S+=(x-x%a[j])*(j-1);
		}
	}
	printf("%lld",f[1]);
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e5+10;
int n;
long long f[MAXN],a[MAXN];
int main()
{
scanf("%d",&n);
for(int i=n;i;i--)
scanf("%lld",&a[i]);
for(int i=n-1;i;i--)
a[i]=min(a[i],a[i+1]);
a[0]=1;
for(int i=1;i<=n;i++)
{
for(long long j=a[i],k,l=0;j;j%=a[k])
{
k=lower_bound(a+1,a+n+1,j)-a-1;
f[i]=max(f[i],f[k]+(n-k)*(j-j%a[k]-a[k])+l);
l+=(n-k)*(j-j%a[k]);
}
}
printf("%lld\n",f[n]);
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
map<long long,long long>f[200010];
typedef map<long long,long long>::iterator iter;
long long a[200010];
int main()
{
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%I64d",&a[i]);
	f[1][a[1]-1]=0;
	for(int i=1;i<n;i++)
		for(iter it=f[i].begin();it!=f[i].end();it++)
		{
			long long j=it->first;
			long long val=it->second;
			if(j<a[i+1])
				f[i+1][j]=max(f[i+1][j],val);
			else{
				f[i+1][a[i+1]-1]=max(f[i+1][a[i+1]-1],val+(j-a[i+1]+1)/a[i+1]*a[i+1]*i);
				f[i+1][j%a[i+1]]=max(f[i+1][j%a[i+1]],val+i*(j-j%a[i+1]));
			}
		}
	long long ans=0;
	for(iter it=f[n].begin();it!=f[n].end();it++)
	{
		long long j=it->first;
		long long val=it->second;
		ans=max(ans,val+n*j);
	}
	printf("%I64d\n",ans);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
map<long long,long long>f[2];
typedef map<long long,long long>::iterator iter;
long long a[200010];
int main()
{
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%I64d",&a[i]);
	f[1][a[1]-1]=0;
	for(int i=1;i<n;i++)
	{
		int now=i&1;
		int nxt=now^1;
		f[nxt].clear();
		for(iter it=f[now].begin();it!=f[now].end();it++)
		{
			long long j=it->first;
			long long val=it->second;
			if(j<a[i+1])
				f[nxt][j]=max(f[nxt][j],val);
			else{
				f[nxt][a[i+1]-1]=max(f[nxt][a[i+1]-1],val+(j-a[i+1]+1)/a[i+1]*a[i+1]*i);
				f[nxt][j%a[i+1]]=max(f[nxt][j%a[i+1]],val+i*(j-j%a[i+1]));
			}
		}
	}
	long long ans=0;
	for(iter it=f[n&1].begin();it!=f[n&1].end();it++)
	{
		long long j=it->first;
		long long val=it->second;
		ans=max(ans,val+n*j);
	}
	printf("%I64d\n",ans);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
map<long long,long long>f;
typedef map<long long,long long>::iterator iter;
long long a[200010];
int main()
{
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%I64d",&a[i]);
	f[a[1]-1]=0;
	for(int i=1;i<n;i++)
	{
		iter it=f.lower_bound(a[i+1]);
		while(it!=f.end())
		{
			long long j=it->first;
			long long val=it->second;
			long long& to1=f[a[i+1]-1];
			to1=max(to1,val+(j-a[i+1]+1)/a[i+1]*a[i+1]*i);
			long long& to2=f[j%a[i+1]];
			to2=max(to2,val+i*(j-j%a[i+1]));
			f.erase(it++);
		}
	}
	long long ans=0;
	for(iter it=f.begin();it!=f.end();it++)
	{
		long long j=it->first;
		long long val=it->second;
		ans=max(ans,val+n*j);
	}
	printf("%I64d\n",ans);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
map<long long,long long>f[2];
typedef map<long long,long long>::iterator iter;
long long a[200010];
int main()
{
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%I64d",&a[i]);
	f[1][a[1]-1]=0;
	for(int i=1;i<n;i++)
	{
		int now=i&1;
		int nxt=now^1;
		f[nxt].clear();
		for(iter it=f[now].begin();it!=f[now].end();it++)
		{
			long long j=it->first;
			long long val=it->second;
			if(j<a[i+1])
			{
				long long& to=f[nxt][j];
				to=max(to,val);
			}
			else{
				long long& to1=f[nxt][a[i+1]-1];
				to1=max(to1,val+(j-a[i+1]+1)/a[i+1]*a[i+1]*i);
				long long& to2=f[nxt][j%a[i+1]];
				to2=max(to2,val+i*(j-j%a[i+1]));
			}
		}
	}
	long long ans=0;
	for(iter it=f[n&1].begin();it!=f[n&1].end();it++)
	{
		long long j=it->first;
		long long val=it->second;
		ans=max(ans,val+n*j);
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Maxn=200005;
ll v[Maxn],ans;
map<ll,ll>f;
int main(){
	int n;cin>>n;
	for(int i=1;i<=n;i++)cin>>v[i];
	f[v[1]-1]=0;
	for(int i=2;i<=n;i++){
		while(prev(f.end())->first>=v[i]){
			ll a,b;tie(a,b)=*prev(f.end());
			f[v[i]-1]=max(f[v[i]-1],b+(i-1)*(a-a%v[i]-v[i]));
			f[a%v[i]]=max(f[a%v[i]],b+(i-1)*(a-a%v[i]));
			f.erase(prev(f.end()));
		}
	}
	for(auto i:f)ans=max(ans,n*i.first+i.second);
	cout<<ans<<"\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define Rep(i,a,b) for(register int i=(a);i<=int(b);++i)
#define Dep(i,a,b) for(register int i=(a);i>=int(b);--i)
#define rep(i,a,b) for(register int i=(a);i<int(b);++i)
#define mem(x,v) memset(x,v,sizeof(x))
#define gc getchar
#define pc putchar
inline ll rd(){
    ll x=0,f=1;char c=gc();
    for(;!isdigit(c);c=gc())if(c=='-')f=-1;
    for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);
    return x*f;
}
void write(ll x){if(x<0)x=-x,pc('-');if(x>=10)write(x/10);putchar(x%10+'0');}
void writeln(ll x){write(x);puts("");}
#define fi first
#define se second
ll n,m;
map<ll,ll> mp;
int main(){
	n = rd(),m = rd();
	mp[m-1] = 0;
	rep(i,1,n){
		m = rd();
		while(true){
			auto it = mp.lower_bound(m);
			if(it != mp.end()){
				mp[it -> fi % m] = max(mp[it -> fi % m],it -> se + it -> fi / m * m * i);
				mp[m-1] = max(mp[m-1],it -> se + (it -> fi - m + 1) / m * m * i);
				mp . erase(it++);
			} else break;
		}
	}
	ll ans = 0;
	for (auto w:mp) ans=max(ans,w.fi*n+w.se);
	writeln(ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define Rep(i,a,b) for(register int i=(a);i<=int(b);++i)
#define Dep(i,a,b) for(register int i=(a);i>=int(b);--i)
#define rep(i,a,b) for(register int i=(a);i<int(b);++i)
#define mem(x,v) memset(x,v,sizeof(x))
#define gc getchar
#define pc putchar
inline ll rd(){
    ll x=0,f=1;char c=gc();
    for(;!isdigit(c);c=gc())if(c=='-')f=-1;
    for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);
    return x*f;
}
void write(ll x){if(x<0)x=-x,pc('-');if(x>=10)write(x/10);putchar(x%10+'0');}
void writeln(ll x){write(x);puts("");}
#define fi first
#define se second
int n,m;
map<ll,ll> mp;
int main(){
	n = rd(),m = rd();
	mp[m-1] = 0;
	rep(i,1,n){
		m = rd();
		while(true){
			auto it = mp.lower_bound(m);
			if(it != mp.end()){
				mp[it -> fi % m] = max(mp[it -> fi % m],it -> se + it -> fi / m * m * i);
				mp[m-1] = max(mp[m-1],it -> se + (it -> fi - m + 1) / m * m * i);
				mp . erase(it++);
			} else break;
		}
	}
	ll ans = 0;
	for (auto w:mp) ans=max(ans,w.fi*n+w.se);
	writeln(ans);
	return 0;
}


#include <iostream>
#include <map>
using namespace std;
int N;
long long A[200005];
map <long long, long long> M;
pair <long long,long long> V[200005];
void Read()
{
    cin >> N;
    for(int i = 1; i <= N; i++)
        cin >> A[i];
}

void Solve()
{
    M[A[1] - 1] = 0;
    for(int i = 1; i < N; i++)
    {
        int cnt = 0;
        long long Max = -1;
        while(!M.empty())
        {
            long long a = prev(M.end()) -> first, b = prev(M.end()) -> second;
            if(a < A[i + 1])
                break;
            long long mod = a % A[i + 1];
            long long newb = a - mod;
            V[++cnt] = make_pair(mod, b + i * newb);
            newb -= A[i + 1];
            Max = max(Max, b + i * newb);
            M.erase(prev(M.end()));
        }
        for(int i = 1; i <= cnt; i++)
        {
            if(M.find(V[i].first) != M.end())
                M[V[i].first] = max(M[V[i].first], V[i].second);
            else
                M[V[i].first] = V[i].second;
        }
        if(cnt > 0)
        {
            if(M.find(A[i + 1] - 1) != M.end())
                M[A[i + 1] - 1] = max(M[A[i + 1] - 1], Max);
            else
                M[A[i + 1] - 1] = Max;
        }
    }
    long long ans = 0;
    for(auto it = M.begin(); it != M.end(); it = next(it))
    {
        long long a = it -> first, b = it -> second;
        ans = max(ans, a * N + b);
    }
    cout << ans << "\n";
}
int main()
{
    Read();
    Solve();
    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>

#define pb push_back
#define mp make_pair

using namespace std;

typedef unsigned int u32;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod pi=acos(-1);
const int oo=1<<30;
const LL OO=1e18;

const int N=2e5+100;

LL gi() {
	LL w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("E.in","r",stdin);
	freopen("E.out","w",stdout);
#endif
	int n=gi(),i;LL x,y=gi(),ans=0;map<LL,LL>s;s[y-1]=0;
	for (i=1;i<n;i++)
		if ((x=gi())<y)
			for (auto t=s.lower_bound(y=x);t!=s.end();s.erase(t++)) {
				s[x-1]=max(s[x-1],t->second+(t->first-x+1)/x*x*i);
				s[t->first%x]=max(s[t->first%x],t->second+t->first/x*x*i);
			}
	for (auto t:s)
		ans=max(ans,t.first*n+t.second);
	cout<<ans<<endl;
	return 0;
}


#include<map>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=2e5+10;
inline void Read(LL &x){
	x=0;register char cc='\0';int fff=1;
	for(;cc<'0'||cc>'9';cc=getchar())if(cc=='-')fff=-1;
	for(;cc>='0'&&cc<='9';cc=getchar())x=(x<<1)+(x<<3)+(cc&15);
	x*=fff;
}
map<LL,LL> mp;
LL n,tot,fans,a[N],nxtj[N<<1],nxtf[N<<1];
int main()
{
	Read(n);
	for(int i=1;i<=n;++i)Read(a[i]);
	mp[a[1]-1]=0;
	for(int i=2;i<=n;++i){
		LL nowm=a[i];
		tot=0;
		map<LL,LL>::iterator it=mp.lower_bound(nowm);
		for(map<LL,LL>::iterator j=it;j!=mp.end();++j){
			nxtj[++tot]=(j->first)%nowm;
			nxtf[tot]=(j->second)+(i-1)*((j->first)-(j->first)%nowm);
			nxtj[++tot]=nowm-1;
			nxtf[tot]=(j->second)+(i-1)*(((j->first)+1)/nowm*nowm/*(j->first)*/-nowm);
		}
		mp.erase(it,mp.end());
		for(register int j=1;j<=tot;++j)
			mp[nxtj[j]]=max(mp[nxtj[j]],nxtf[j]);
	}
	for(map<LL,LL>::iterator it=mp.begin();it!=mp.end();++it)
		fans=max(fans,it->second+(it->first)*n);
	cout<<fans<<endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <functional>
#define ll long long
using namespace std;
namespace ywy {
    inline ll get() {
        ll n = 0;
        char c;
        while ((c = getchar()) || 23333) {
            if (c >= '0' && c <= '9')
                break;
            if (c == '-')
                goto s;
        }
        n = c - '0';
        while ((c = getchar()) || 23333) {
            if (c >= '0' && c <= '9')
                n = n * 10 + c - '0';
            else
                return (n);
        }
    s:
        while ((c = getchar()) || 23333) {
            if (c >= '0' && c <= '9')
                n = n * 10 - c + '0';
            else
                return (n);
        }
    }
    ll ints[222222];
    map<ll, ll, greater<ll> > mp;
    typedef map<ll, ll, greater<ll> >::iterator it;
    ll tmp[1000001];
    void ywymain() {
        int n = get();
        for (register int i = 1; i <= n; i++) ints[i] = get();
        mp[ints[1] - 1] = 0;
        for (register int i = 1; i < n; i++) {
            int ptr = 0;
            ll mx = 0;
            for (it j = mp.begin(); j != mp.end(); j++) {
                if ((j->first) < ints[i + 1])
                    break;
                mp[ints[i + 1] - 1] =
                    max(mp[ints[i + 1] - 1],
                        (j->second) + (ll)i * (ints[i + 1] * (((j->first) + 1) / ints[i + 1]) - ints[i + 1]));
                mp[(j->first) % ints[i + 1]] = max(mp[(j->first) % ints[i + 1]],
                                                   (j->second) + (ll)i * ((j->first) - (j->first) % ints[i + 1]));
                tmp[ptr] = j->first;
                ptr++;
            }
            while (ptr) ptr--, mp.erase(tmp[ptr]);
        }
        ll ans = 0;
        ll mx = 0;
        for (it i = mp.begin(); i != mp.end(); i++) {
            ans = max(ans, n * (i->first) + (i->second));
        }
        cout << ans << endl;
    }
}
int main() {
    ywy::ywymain();
    return (0);
}


#include <algorithm>
#include <cstdio>
#include <map>

using namespace std;

#define LL long long
typedef map<LL,LL>::iterator it;

LL n, x, y, sl, ans, a[200010];
map <LL,LL> dp;
#define BUF_SIZE 1000000
inline char gc() {
	static char buf[BUF_SIZE], *p1 = buf, *pend = buf;
	if (p1 == pend) {
		p1 = buf;
		pend = buf + fread(buf, 1, BUF_SIZE, stdin);
		if (p1 == pend) {
			return -1;
		}
	}
	return *p1++;
}
#undef BUF_SIZE
LL rd()
{
	sl = 0;
	char ch = gc();
	for (; ch < '0' || ch > '9'; ch = gc());
	for (; ch >= '0' && ch <= '9'; ch = gc()) {
		sl = (sl << 1) + (sl << 3) + (ch ^ '0');
	}
	return sl;
}
int main()
{
	n = rd();
	for (int i = 1 ; i <= n; ++i) {
		a[i] = rd();
	}
	dp[a[1] - 1] = 0;
	for (int i = 1; i < n; ++i) {
		for (it j = dp.lower_bound(a[i + 1]); j != dp.end(); dp.erase(j++)) {
			x = j->first;
			y = j->second;
			dp[x % a[i + 1]] = max(dp[x % a[i + 1]], y + i * (x - x % a[i + 1]));
			dp[a[i + 1] - 1] = max(dp[a[i + 1] - 1], y + i * ((x + 1) / a[i + 1] * a[i + 1] - a[i + 1]));
		}
	}
	for(it i = dp.begin(); i != dp.end(); ++i) {
		x = i->first;
		y = i->second;
		ans = max(ans, x * n + y);
	}
	printf("%lld\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
typedef long long LL;
std::map<LL,LL>f;
int main(){
	int n;scanf("%d",&n);
	for(int i=1;i<=n;++i){
		LL x;scanf("%lld",&x);
		if(i==1)f[x-1]=0;
		else while("Jumbo is tql"){
			auto it=--f.end();
			LL v=it->first,b=it->second;
			if(v<x)break;
			f[x-1]=std::max(f[x-1],b+(i-1)*(v-v%x-x));
			f[v%x]=std::max(f[v%x],b+(i-1)*(v-v%x));
			f.erase(it);
		}
	}
	LL ans=0;
	for(auto x:f)
		ans=std::max(ans,n*x.first+x.second);
	printf("%lld\n",ans);
	return 0;
}


/*
Copied from Asttinx64
*/
#include<bits/stdc++.h>
typedef long long ll;
const int maxn=202020;
ll val[maxn];
namespace bf{
	int coef[maxn];
	const int maxv=1010101;
	int buc[maxv];
	void main(int n){
		int cnt=0;
		val[0]=LLONG_MAX;
		for(int i=1;i<=n;i++){
			if(val[i]<val[cnt])
				val[++cnt]=val[i];
			++coef[cnt];
		}
		val[cnt+1]=1;
		for(int i=1;i<=cnt;i++)
			coef[i]+=coef[i-1];
		for(int i=1;i<=cnt;i++)
			buc[val[i]]=i;
		if(!buc[0])
			buc[0]=cnt+1;
		for(int i=0;i<=val[1];i++)
			if(!buc[i])
				buc[i]=buc[i-1];
		ll ans=0;
		for(int i=1;i<val[1];i++){
			ll tmp=0;
			int ptr=1,curr=i;
			while(ptr<=cnt){
				int skip=buc[curr];
				tmp+=curr*1ll*(coef[skip-1]-coef[ptr-1]);
				curr%=val[ptr=skip];
			}
			ans=std::max(tmp,ans);
		}
		std::cout<<ans;
	}
}
namespace cheat{
	int coef[maxn];
	void main(int n){
		int cnt=0;
		val[0]=LLONG_MAX;
		for(int i=1;i<=n;i++){
			if(val[i]<val[cnt])
				val[++cnt]=val[i];
			++coef[cnt];
		}
		val[cnt+1]=1;
		for(int i=1;i<=cnt;i++)
			coef[i]+=coef[i-1];
		if(val[1]==1){
			std::cout<<"0";
			return;
		}
		ll ans=0;
		std::mt19937_64 rd((long long)new char);
		int times=3000000;
		if(cnt>=300)
			times=2e11/cnt;
		for(int round=0;round<=times;round++){
			ll curr;
			curr=rd()%(val[1]-1)+1;
			int ptr=1;
			ll tmp=0;
			ll min=LLONG_MAX;
			while(ptr<=cnt){
				int l=ptr,r=cnt+1;
				while(l<r){
					int mid=(l+r)>>1;
					if(val[mid]>curr)
						l=mid+1;
					else
						r=mid;
				}
				int skip=l;
				min=std::min(val[skip-1]-curr-1,min);
				tmp+=curr*1ll*(coef[skip-1]-coef[ptr-1]);
				if(!(curr%=val[ptr=skip])){
					min=std::min(val[cnt]-1,min);
					break;
				}
			}
			ans=std::max(tmp+min*1ll*n,ans);
		}
		std::cout<<ans;
	}
}
int main(){
	std::ios_base::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);
	int n;
	std::cin>>n;
	ll max=0;
	for(int i=1;i<=n;i++){
		std::cin>>val[i];
		max=std::max(val[i],max);
	}
	if(max<=1000000)
		bf::main(n);
	else
		cheat::main(n);
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
typedef long long LL;
std::map<LL,LL>f;
int main(){
	int n;scanf("%d",&n);
	for(int i=1;i<=n;++i){
		LL x;scanf("%lld",&x);
		if(i==1)f[x-1]=0;
		else while("Jumbo is tql!!!"){
			auto it=--f.end();
			LL v=it->first,b=it->second;
			if(v<x)break;
			f[x-1]=std::max(f[x-1],b+(i-1)*(v-v%x-x));
			f[v%x]=std::max(f[v%x],b+(i-1)*(v-v%x));
			f.erase(it);
		}
	}
	LL ans=0;
	for(auto x:f)
		ans=std::max(ans,n*x.first+x.second);
	printf("%lld\n",ans);
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<map>
using namespace std;

long long findIndex(long long L, long long R, long long val, vector<long long> v) {
	long long res = R, mid;
	while (L <= R) {
		mid = (L + R) / 2;
		if (v[mid] <= val) {
			R = mid - 1;
			res = min(res, mid);
		} else {
			L = mid + 1;
		}
	}
	return res;
}

bool work() {
	vector<map<long long, long long>> f;
	vector<long long> v;
	vector<long long> vIndex;
	long long N;
	if (scanf("%I64d", &N) != 1)
		return false;
	f.resize(N + 10);
	v.push_back(1e15);
	vIndex.push_back(0);
	for (int i = 1; i <= N; i ++) {
		long long tmp;
		scanf("%I64d", &tmp);
		if (tmp < v[v.size() - 1]) {
			v.push_back(tmp);
			vIndex.push_back(i);
		}
	}
	if (v[v.size() - 1] != 1) {
		v.push_back(1);
		vIndex.push_back(N + 1);
	}
	N = v.size() - 1;
	v[N + 1] = 1;
	f[1][v[1] - 1] = 0;
	long long res = 0;
	for (int i = 1; i < N; i ++) {
		for (map<long long, long long>::iterator j = f[i].begin(); j != f[i].end(); j ++) {
			long long range = j->first, val = j->second;
			if (range == 0)
				res = max(res, val);
			long long k = findIndex(i + 1, N, range, v);
			long long tmp = (range - v[k] - (range % v[k])) * (vIndex[k] - 1) + val;
			f[k][v[k] - 1] = max(f[k][v[k] - 1], tmp);
			tmp = (range - (range % v[k])) * (vIndex[k] - 1) + val;
			f[k][range % v[k]] = max(f[k][range % v[k]], tmp);
		}
	}
	for (int i = N; i <= N; i ++) {
		for (map<long long, long long>::iterator j = f[i].begin(); j != f[i].end(); j ++) {
			long long range = j->first, val = j->second;
			res = max(res, val);
		}
	}
	printf("%I64d\n", res);
	return true;
}

int main() {
//	freopen("889E.in", "r", stdin);
	while (work());
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<map>
using namespace std;

long long findIndex(long long L, long long R, long long val, vector<long long> v) {
	long long res = R, mid;
	while (L <= R) {
		mid = (L + R) / 2;
		if (v[mid] <= val) {
			R = mid - 1;
			res = min(res, mid);
		} else {
			L = mid + 1;
		}
	}
	return res;
}

bool work() {
	vector<long long> f;
	vector<long long> v;
	vector<long long> vIndex;
	long long N;
	if (scanf("%I64d", &N) != 1)
		return false;
	f.resize(N + 10);
	v.push_back(1e15);
	vIndex.push_back(0);
	for (int i = 1; i <= N; i ++) {
		long long tmp;
		scanf("%I64d", &tmp);
		if (tmp < v[v.size() - 1]) {
			v.push_back(tmp);
			vIndex.push_back(i);
		}
	}
	if (v[v.size() - 1] != 1) {
		v.push_back(1);
		vIndex.push_back(N + 1);
	}
	N = v.size() - 1;
	for (int i = 0; i <= N; i ++)
		f[i] = 0;
	f[1] = 0;
	long long res = 0;
	for (int i = 1; i < N; i ++) {
		long long j = i, range = v[i] - 1, val = f[i];
		while (range != 0) {
			long long k = findIndex(j + 1, N, range, v);
			long long tmp = (range - v[k] - (range % v[k])) * (vIndex[k] - 1) + val;
			f[k] = max(f[k], tmp);
			tmp = (range - (range % v[k])) * (vIndex[k] - 1) + val;
			j = k;
			range = range % v[k];
			val = tmp;
		}
		res = max(res, val);
	}
	for (int i = N; i <= N; i ++) {
		res = max(res, f[N]);
	}
	printf("%I64d\n", res);
	return true;
}

int main() {
//	freopen("889E.in", "r", stdin);
	while (work());
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<map>
using namespace std;

long long findIndex(long long L, long long R, long long val, vector<long long> &v) {
	long long res = R, mid;
	while (L <= R) {
		mid = (L + R) / 2;
		if (v[mid] <= val) {
			R = mid - 1;
			res = min(res, mid);
		} else {
			L = mid + 1;
		}
	}
	return res;
}

bool work() {
	vector<long long> f;
	vector<long long> v;
	vector<long long> vIndex;
	long long N;
	if (scanf("%I64d", &N) != 1)
		return false;
	f.resize(N + 10);
	v.push_back(1e15);
	vIndex.push_back(0);
	for (int i = 1; i <= N; i ++) {
		long long tmp;
		scanf("%I64d", &tmp);
		if (tmp < v[v.size() - 1]) {
			v.push_back(tmp);
			vIndex.push_back(i);
		}
	}
	if (v[v.size() - 1] != 1) {
		v.push_back(1);
		vIndex.push_back(N + 1);
	}
	N = v.size() - 1;
	for (int i = 0; i <= N; i ++)
		f[i] = 0;
	f[1] = 0;
	long long res = 0;
	for (int i = 1; i < N; i ++) {
		long long j = i, range = v[i] - 1, val = f[i];
		while (range != 0) {
			long long k = findIndex(j + 1, N, range, v);
			long long tmp = (range - v[k] - (range % v[k])) * (vIndex[k] - 1) + val;
			f[k] = max(f[k], tmp);
			tmp = (range - (range % v[k])) * (vIndex[k] - 1) + val;
			j = k;
			range = range % v[k];
			val = tmp;
		}
		res = max(res, val);
	}
	for (int i = N; i <= N; i ++) {
		res = max(res, f[N]);
	}
	printf("%I64d\n", res);
	return true;
}

int main() {
//	freopen("889E.in", "r", stdin);
	while (work());
	return 0;
}


#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^'0')
#define pb push_back
#define solid const auto &
#define enter cout<<endl
#define pii pair<int,int>
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
    char ch;x=0;bool fl=false;while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
    for(x=numb;isdigit(ch=getchar());x=x*10+numb);(fl==true)&&(x=-x);}
template<class T>il void output(T x){if(x/10)output(x/10);putchar(x%10+'0');}
template<class T>il void ot(T x){if(x<0) putchar('-'),x=-x;output(x);putchar(' ');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');}
namespace Modulo{
const int mod=998244353;
int ad(int x,int y){return (x+y)>=mod?x+y-mod:x+y;}
void inc(int &x,int y){x=ad(x,y);}
int mul(int x,int y){return (ll)x*y%mod;}
void inc2(int &x,int y){x=mul(x,y);}
int qm(int x,int y=mod-2){int ret=1;while(y){if(y&1) ret=mul(x,ret);x=mul(x,x);y>>=1;}return ret;}
}
//using namespace Modulo;
namespace Miracle{
const int N=200000+5;
int n;
ll a[N];
map<ll,ll,greater<ll> >mp;
ll tmp[5*N];
int main(){
    rd(n);
    for(reg i=1;i<=n;++i) rd(a[i]);
    mp[a[1]-1]=0;
    for(reg i=1;i<n;++i){
        int ptr=0;
        for(solid x:mp){
            if(x.fi<a[i+1]) break;
            mp[a[i+1]-1]=max(mp[a[i+1]-1],mp[x.fi]+(ll)i*a[i+1]*((x.fi-a[i+1]+1)/a[i+1]));
            mp[x.fi%a[i+1]]=max(mp[x.fi%a[i+1]],mp[x.fi]+(ll)i*(x.fi-x.fi%a[i+1]));
            tmp[++ptr]=x.fi;
        }
        while(ptr) mp.erase(tmp[ptr--]);
    }
    ll ans=0;
    for(solid x:mp){
        ans=max(ans,(ll)x.fi*n+x.se);
    }
    ot(ans);
    return 0;
}

}
signed main(){
    Miracle::main();
    return 0;
}

/*
   Author: *Miracle*
*/


#include<bits/stdc++.h>
#define N 200009
using namespace std;
typedef long long ll;
const int mod=1e9+7;
map<ll,ll>f;
map<ll,ll>::iterator it;
int n;
inline ll rd(){
    ll x=0;char c=getchar();bool f=0;
    while(!isdigit(c)){if(c=='-')f=1;c=getchar();}
    while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    return f?-x:x;
}
int main(){
    n=rd();
    f[rd()-1]=0;
    for(int i=2;i<=n;++i){
        ll x=rd();
        while(!f.empty()){
            it=f.end();--it;
            ll a=it->first,b=it->second;
            if(a<x)break;
            f[x-1]=max(f[x-1],(i-1)*(a-a%x-x));
            f[a%x]=max(f[a%x],(i-1)*(a-a%x));
            f.erase(it);
        }
    }
    ll ans=0;
    for(it=f.begin();it!=f.end();++it)ans=max(ans,it->first*n+it->second);
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
#define N 200009
using namespace std;
typedef long long ll;
const int mod=1e9+7;
map<ll,ll>f;
map<ll,ll>::iterator it;
int n;
inline ll rd(){
    ll x=0;char c=getchar();bool f=0;
    while(!isdigit(c)){if(c=='-')f=1;c=getchar();}
    while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
    return f?-x:x;
}
int main(){
    n=rd();
    f[rd()-1]=0;
    for(int i=2;i<=n;++i){
        ll x=rd();
        while(!f.empty()){
            it=f.end();--it;
            ll a=it->first,b=it->second;
            if(a<x)break;
            f.erase(it);
            f[x-1]=max(f[x-1],b+1ll*(i-1)*(a-a%x-x));
            f[a%x]=max(f[a%x],b+1ll*(i-1)*(a-a%x));
        }
    }
    ll ans=0;
    for(it=f.begin();it!=f.end();++it)ans=max(ans,it->first*n+it->second);
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,ans,a[200005];
map<ll,ll> mp;
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
		scanf("%I64d",&a[i]);
	mp.insert(pair<ll,ll>(a[1],0));
	map<ll,ll>::iterator j;
	for (int i=1;i<n;i++)
		for (j=mp.upper_bound(a[i+1]);j!=mp.end();){
			long long &f0=mp[a[i+1]];
			f0=max(f0,j->second+(j->first/a[i+1]-1)*a[i+1]*i);
			if (j->first%a[i+1]){
				long long &f1=mp[j->first%a[i+1]];
				f1=max(f1,j->second+j->first/a[i+1]*a[i+1]*i);
			}
			mp.erase(j);
			j=mp.upper_bound(a[i+1]);
		}
	for (j=mp.begin();j!=mp.end();j++)
		ans=max(ans,(j->first-1)*n+j->second);
	printf("%I64d",ans);
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ~ ";
	dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int N; cin >> N;
	V<int> c(N+1);
	rep(i,N){
		int x; cin >> x; c[x]++;
	}
	int a = 0;
	rep(i,N+1) if(c[i] == 0) a++;
	cout << a << endl;
}


#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    map<int, int> mp;
    mp[0] = 1;
    int sol = 1;
    for (int i = 1; i <= n; ++i)
    {
        int x;
        cin >> x;
        auto it = mp.find(x);
        if (it != mp.end())
        {
            int y = it->second;
            mp.erase(it);
            mp[i] = y;
        }
        else
            mp[i] = ++sol;
    }
    cout << sol;
}


/*
  Warn - Don't change next line else you will get WA verdict. Online Judge is configured to give WA if next line is not present.
  "An ideal problem has no test data."
  Author - Aryan Choudhary (@aryanc403)
*/

// #pragma warning(disable:4996)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize ("Ofast")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize ("-ffloat-store")

#include<iostream>
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define fo(i,n)   for(i=0;i<(n);++i)
#define repA(i,j,n)   for(i=(j);i<=(n);++i)
#define repD(i,j,n)   for(i=(j);i>=(n);--i)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define endl "\n"
typedef long long int lli;
typedef long double mytype;
typedef pair<lli,lli> ii;
typedef vector<ii> vii;
typedef vector<lli> vi;

clock_t time_p=clock();
void aryanc403()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}

#ifdef ARYANC403
#define dbg(...) { cerr<<"[ "; __aryanc403__(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.X<<","<<x.Y<<")";
}

template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}

template <typename Arg1>
void __aryanc403__(const string name, Arg1&& arg1){
	cerr << name << " : " << arg1 << " ] " << endl;
}

template <typename Arg1, typename... Args>
void __aryanc403__(const string names, Arg1&& arg1, Args&&... args){
	const string name = names.substr(0,names.find(','));
	cerr<<name<<" : "<<arg1<<" | ";
	__aryanc403__(names.substr(1+(int)name.size()), args...);
}

#else
    #define dbg(args...)
#endif

const lli INF = 0xFFFFFFFFFFFFFFFL;

lli seed;
mt19937 rng(seed=chrono::steady_clock::now().time_since_epoch().count());
inline lli rnd(lli l=0,lli r=INF)
{return uniform_int_distribution<lli>(l,r)(rng);}

class CMP
{public:
bool operator()(ii a , ii b) //For min priority_queue .
{    return ! ( a.X < b.X || a.X==b.X && a.Y <= b.Y);   }};

void add( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt==m.end())         m.insert({x,cnt});
    else                    jt->Y+=cnt;
}

void del( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt->Y<=cnt)            m.erase(jt);
    else                      jt->Y-=cnt;
}

bool cmp(const ii &a,const ii &b)
{
    return a.X<b.X||(a.X==b.X&&a.Y<b.Y);
}

const lli mod = 1000000007L;

    lli T,n,i,j,k,in,cnt,l,r,u,v,x,y;
    lli m;
    string s;
    map<lli,lli> a;
    //priority_queue < ii , vector < ii > , CMP > pq;// min priority_queue .

int main(void) {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    // freopen("txt.in", "r", stdin);
    // freopen("txt.out", "w", stdout);
// cout<<std::fixed<<std::setprecision(35);
// cin>>T;while(T--)
{

    cin>>n;
    a.clear();

    fo(i,n)
    {
        cin>>in;
        a[in]++;
    }

    cnt=1;
    for(auto x:a)
        cnt+=x.Y-1;
    cout<<cnt<<endl;

}   aryanc403();
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}
vector<vector<int> > g;
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int>a(n);
    rep(i,n)cin >> a[i];
    g.resize(n+1);
    rep(i,n){
        g[a[i]].push_back(i+1);
    }
    int sm = 0;
    rep(i,n+1){
        if(g[i].size()==0){
            sm++;
        }
    }
    cout << sm << endl;
    return 0;
}


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

typedef long long ll;
const int maxn=1e6+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//#define int ll
//#define endl '\n'

int n;
multiset<int> mm;

int32_t main()
{
    IOS
    cin>>n;
    mm.insert(0);
    for (int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if (mm.find(x)==mm.end()) mm.insert(i);
        else
        {
            mm.erase(mm.find(x));
            mm.insert(i);
        }
    }
    cout<<sz(mm)<<endl;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define int long long
#define pb push_back
#define ins insert
#define rs resize
#define br break;
#define cont continue;
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define si set<ll>
#define sll set<long long>
#define pii pair<ll,ll>
#define pll pair<long long, long long>
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL);
#define vec vector
#define cend cout<<endl;
#define sc second
#define fr first
#define retz return 0;
#define ll_MAX (ll)(1e9*1e9)
#define ll_MIN (ll)(-1e9*1e9)
#define lb lower_bound
#define ub upper_bound
#define rep(i,a,b)  for(int i=a; i<=b; i++)
#define repd(i,a,b) for(int i=a; i>=b; i--)
#define ps(arr, n) for(int i=1; i<n; i++)arr[i]+=arr[i-1];
#define inp(arr, n)for(int i=0; i<n; i++)cin>>arr[i];
#define out(arr, n)for(int i=0; i<n; i++)cout<<arr[i];
#define inp1(arr,n,m)for(int i=0; i<n; i++)for(int j=0;j<m;j++)cin>>arr[i][j];
#define out1(arr,n,m)for(int i=0; i<n; i++)for(int j=0;j<m;j++)cin>>arr[i][j];
#define loop(q) for(auto it:q)
#define loop1(q) for(auto&it:q)
#define db1 cout << "*" << endl
#define db2 cout << "YES" << endl
using namespace std;
using namespace __gnu_pbds;
#define oset(X) tree<X,null_type,less<X>, rb_tree_tag, tree_order_statistics_node_update>
#define roset(X) tree<X,null_type,greater<X>, rb_tree_tag, tree_order_statistics_node_update>
#define okey order_of_key //lower_bound_set
#define ofind find_by_order
#define cyes cout << "Yes" << endl;
#define cno cout << "No" << endl;
#define mod (long long)1000000007
#define mod2 (long long)998244353
#define INT_MAX1 900000000
#define MAXN 2000000
#define INF INT_MAX
#define lcm(a,b) (a*(b/__gcd(a,b)))
#define gcd(a,b) __gcd(a,b)

void solve()
{
    int n;
    cin >> n;
    int arr[n];
    inp(arr, n);
    multiset<int> q;
    int ans = 0;
    rep(i,0,n-1)
    {
        if(q.find(arr[i])!=q.end())
        {
            q.erase(q.find(arr[i]));
            q.insert(i+1);
        }
        else
        {
            ans++;
            q.insert(i+1);
        }
    }

    cout << ans << endl;
}

signed main()
{

    speed;
    int t = 1;
    //cin >> t;
    while(t--)
        solve();
}


#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<string>
#include<iostream>
using namespace std;

int main() {
	int n; cin>>n;
	vector<int> t(n+1);
	t[0]=0;
	for (int i=1; i<=n; i++) cin>>t[i];

	set<int> vis;
	vis.insert(0);
	int ret=0;
	for (int i=n; i>=1; i--) {
		int cur=i;
		if(vis.find(cur)==vis.end()) {
			ret++;
			while(1) {
				if(!cur) break;
				vis.insert(cur);
				int nxt=t[cur];
				if(vis.find(nxt)!=vis.end()) break;
				cur=nxt;
			}
		}
	}
	cout << ret << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,ans,a[200010],b[200010];
int main()
{
	scanf("%d",&n);
	ans=1;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
	{
		if(!b[a[i]])
		{
			b[a[i]]=1;
		}
		else ans++;
	}
	printf("%d\n",ans);
}


#define DBG 1
// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>
//#include<ext/pb_ds/tree_policy.hpp>
//#include<ext/pb_ds/assoc_container.hpp>

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define debug(x) if (BDG) cerr << #x << " = " << x << endl;

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef pair<int, int> pii;

//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int MAXN = (int)2e5 + 5;

int leaf[MAXN];
int n;

void solve() {
    scanf("%d", &n);

    for (int i = 1; i <= n; ++i) {
        leaf[i] = 1;
    }

    for (int i = 1; i <= n; ++i) {
        int x;
        scanf("%d", &x);
        leaf[x] = 0;
    }

    printf("%d\n", accumulate(leaf + 1, leaf + n + 1, 0));
}

int main() {
	int tt = 1;

	while (tt--) {
		solve();
	}

	return 0;
}


#include<bits/stdc++.h>
#define N 200005
#define ll long long
#define pb push_back
#define x first
#define y second
#define all(v) v.begin(),v.end()
using namespace std;
int n,ar[N],vis[N];
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>ar[i];
    }
    int ans = 0;
    for(int i=n;i;i--){
        if(vis[i])continue;
        int tmp = i;
        ans++;
        while(tmp&&!vis[tmp]){
            vis[tmp] = 1;
            tmp = ar[tmp];
        }
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 201110;
int t[N];
bool pre[N];
int n, ans = 0;

int main() {
	//freopen("in.in", "r", stdin);
	cin >> n;
	for(int i = 1; i <= n; ++i) cin >> t[i];
	for(int i = n; i >= 1; --i) {
		if(!pre[i]) {
			ans++;
			int u = i;
			while(!pre[u]) {
				pre[u] = true;
				u = t[u];
			}
		}
	}

	cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

int n;
int t[N];
int cnt[N];

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

    cin >> n;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        cnt[x]++;
    }
    int res = 1;
    for (int i = 0; i < N; i++) {
        res += max(0, cnt[i] - 1);
    }
    cout << res << '\n';

    return 0;
}


#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<ll>;

int main() {
	ios::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	int n;
	map<int, int> cnt;
	cin >> n;
	int ans = 0;
	cnt[0]++;
	for(int t, i = 0; i < n; i++) {
		cin >> t;
		if(++cnt[t] != 1) {
			ans++;
		}
	}
	cout << ans;
}


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

using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    rep(i, v.size()) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

int main() {
    int n;
    cin >> n;
    V<int> a(n);
    rep(i, n) cin >> a[i];
    int ans = 1;
    set<int> st;
    st.insert(0);
    rep(i, n) {
        if (st.count(a[i])) {
            st.erase(a[i]);
            st.insert(i + 1);
        } else {
            ans++;
            st.insert(i + 1);
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 200010;
map<int,int>M;
int n,t[maxn],cur;

int main()
{
    scanf("%d",&n);
    for (int i = 1; i <= n; i++)
        scanf("%d",&t[i]);
    cur = 1;
    M[0] = 1;
    for (int i = 1; i <= n; i++)
    {
        if (!M.count(t[i]) || M[t[i]] <= 0)
        {
            M[i]++;
            cur++;
        }
        else
        {
            M[t[i]]--;
            M[i]++;
        }
    }
    printf("%d\n",cur);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

namespace utils{
constexpr bool use_stdio = false;
ostream* out = &cout;
FILE* stdio_out = stdout;
const bool __ = [](){ if constexpr(!use_stdio){ ios_base::sync_with_stdio(false); cin.tie(nullptr);
  cout << fixed; cout << setprecision(12); cerr << fixed; cerr << setprecision(12); } return true; }();
template<typename ...> using to_void = void;
template<typename T, typename = void> struct is_container : false_type {};
template<typename T> struct is_container<T, to_void<decltype(declval<T>().begin()), decltype(declval<T>().end())>> : true_type {};

inline void rd(int& x) { if constexpr(use_stdio) scanf("%d", &x); else cin >> x; }
inline void rd(long long& x) { if constexpr(use_stdio) scanf("%lld", &x); else cin >> x; }
inline void rd(double& x) { if constexpr(use_stdio) scanf("%lf", &x); else cin >> x; }
inline void rd(long double& x) { if constexpr(use_stdio){ double y; scanf("%lf", &y); x=y; } else cin >> x; }
inline void rd(char& x) { if constexpr(use_stdio) scanf(" %c", &x); else cin >> x; }
inline void rd(char* x) { if constexpr(use_stdio) scanf("%s", x); else cin >> x; }
inline void rd(string& x) { if constexpr(use_stdio) assert(false); else cin >> x; }
template<typename T, typename enable_if<!use_stdio && is_integral<T>::value>::type* = nullptr> inline void rd(T& x) { cin >> x; }
template<typename T> inline void rd(T* x);
template<typename T, typename enable_if<is_container<T>::value>::type* = nullptr> inline void rd(T& x);
template<typename... Ts> inline void rd(tuple<Ts...>& x);
template<typename T, typename U> inline void rd(pair<T, U>& x) { rd(x.first); rd(x.second); }
template<typename T, typename enable_if<is_container<T>::value>::type* = nullptr> inline void rd(T& x) { for(auto& v : x) rd(v); }
template<typename T, typename U, typename... Ts> inline void rd(T&& t, U&& u, Ts&&... ts) {
  rd(forward<T>(t)); rd(forward<U>(u), forward<Ts>(ts)...); }
template<typename... Ts> inline void rd(tuple<Ts...>& x) { static_assert(sizeof...(Ts) > 0);
  constexpr auto apply_rd = [](auto&&... xs) { rd(xs...); }; apply(apply_rd, x); }
template<typename T> inline void rd(T* x) { rd(*x); }

inline void prt(const int& x) { if constexpr(use_stdio) fprintf(stdio_out, "%d", x); else *out << x; }
inline void prt(const long long& x) { if constexpr(use_stdio) fprintf(stdio_out, "%lld", x); else *out << x; }
inline void prt(const double& x) { if constexpr(use_stdio) fprintf(stdio_out, "%.12f", x); else *out << x; }
inline void prt(const long double& x) { if constexpr(use_stdio) fprintf(stdio_out, "%.12f", (double)x); else *out << x; }
inline void prt(const char& x) { if constexpr(use_stdio) fprintf(stdio_out, "%c", x); else *out << x; }
inline void prt(const char* x) { if constexpr(use_stdio) fprintf(stdio_out, "%s", x); else *out << x; }
inline void prt(const string &x) { if constexpr(use_stdio) fprintf(stdio_out, "%s", x.c_str()); else *out << x; }
template<typename T, typename enable_if<!use_stdio && is_integral<T>::value>::type* = nullptr> inline void prt(const T& x) { *out << x; }
template<typename T, typename enable_if<is_container<T>::value>::type* = nullptr> inline void prt(const T& x);
template<typename... Ts> inline void prt(const tuple<Ts...>& x);
template<typename T, typename U> inline void prt(const pair<T, U>& x) { prt(x.first); prt(' '); prt(x.second); }
template<typename T, typename enable_if<is_container<T>::value>::type* = nullptr> inline void prt(const T& x) {
  if (x.begin() != x.end()) { prt(*x.begin()); for(auto it = x.begin(); ++it != x.end(); ) { prt(' '); prt(*it); } } }
template<typename T, typename U, typename... Ts> inline void prt(T&& t, U&& u, Ts&&... ts) {
  prt(forward<T>(t)); prt(' '); prt(forward<U>(u), forward<Ts>(ts)...); }
template<typename... Ts> inline void prt(const tuple<Ts...>& x ) { static_assert(sizeof...(Ts) > 0);
  constexpr auto apply_prt = [](auto&&... xs) { prt(xs...); }; apply(apply_prt, x); }
inline void wt() { prt('\n'); }
template<typename... Ts> inline void wt(Ts&&... ts) { prt(forward<Ts>(ts)...); wt(); }

#ifdef MY_DEBUG_FLAG
inline void enter_db() { if constexpr(use_stdio) stdio_out = stderr; else out = &cerr; }
inline void leave_db() { if constexpr(use_stdio) stdio_out = stdout; else out = &cout; }
inline void db() { enter_db(); prt('\n'); leave_db(); }
template<typename... Ts> inline void db(Ts&&... ts) { enter_db(); prt(forward<Ts>(ts)...); wt(); leave_db(); }
#define db(...) utils::db("Debug(", #__VA_ARGS__, ") == (", __VA_ARGS__, ")")
#else
#define db(...)
#endif // define(MY_DEBUG_FLAG)
constexpr int slen(char*s){ return (int) strlen(s); }
template<typename T, typename U, typename enable_if<is_container<T>::value>::type* = nullptr> inline T& resize(T& x, U n) {
  x.resize(n); return x; }
void flush() { if constexpr(use_stdio) fflush(stdout); else cout << std::flush; }
} using utils::rd, utils::prt, utils::wt, utils::slen, utils::resize, utils::flush;

#define pb emplace_back
#define x first
#define y second
#define sz(a) ((int)(a).size())
#define rep(i,a,b) for(int i=(a); i<(b); i++)
#define dec(i,n,a) for(int i=(n); i>=(a); i--)
#define clr(a,v) memset(a, v, sizeof(a))
#define all(a) (a).begin(),(a).end()

constexpr int inf = 0x3f3f3f3f;
constexpr ld EPS = 1e-9;
constexpr int MAXN = 1e5 + 10;
constexpr int LOGN = 20;
constexpr int mod = 1000000007;

int main() {
  int n;
  rd(n);
  vi v(n);
  rd(v);
  multimap<int, int> room;
  room.insert(pii(0,0));
  rep(i,0,n) {
    auto it = room.find(v[i]);
    if (it != room.end()) {
      int id = it->y;
      room.erase(it);
      room.insert(pii(i + 1, id));
    }
    else {
      room.insert(pii(i + 1, sz(room)));
    }
  }
  wt(sz(room));
}


/*
ID: essi
LANG: C++
TASK: .cpp
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define sep ios::sync_with_stdio(0),cin.tie(0)
const int MXi = 2e5 + 5;
vector <int> seen(MXi);
int n, t;

int main() {
    sep;
    cin >> n;
    int node = 0;
    for (int i = 1; i <= n; ++i) {
        cin >> t;
        if (seen[t]) {
            seen[t] = 0;
            seen[i] = 1;
        } else {
            ++node;
            seen[i] = 1;
        }
    }
    cout << node << "\n";
    return 0;
}