#include<bits/stdc++.h>
#define f(i,a,n) for(int i=a;i<n;i++)
#define S second
#define F first
#define Sc(n) scanf("%lld",&n)
#define scc(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define Scc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define sp(a) scanf("%d %d",&a.first,&a.second)
#define Sp(a) scanf("%lld %lld",&a.first,&a.second)
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(),a.end()
#define sc(n) scanf("%d",&n)
#define It iterator
#define SET(a,b) memset(a,b,sizeof(a))
#define DRT()  int t; cin>>t; while(t--)
// inbuilt functions
// __gcd,  __builtin_ffs,     (returns least significant 1-bit, __builtin_ffsll(1)=1)
// __builtin_clz,             (returns number of leading zeroes in
// __builtin_popcount,
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> vi;
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define trv(s,it) for(auto it:s)
//#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1)
{	    cerr << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args)
{	    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#else
#define trace(...)
#endif
int ar[100005];
priority_queue<PII> qu;
int tr[200005],ll=0;
void build(int b,int e,int n)
{	if(b==e)
	{	tr[n]=1;
		return;
	}
	build(b,(b+e)/2,2*n);
	build((b+e)/2+1,e,2*n+1);
	tr[n]=tr[2*n]+tr[2*n+1];
}
void update(int b,int e,int n, int i)
{	tr[n]--;
	if(b==e)
			return;
	if(i<=(b+e)/2)
		update(b,(b+e)/2,2*n,i);
	else
		update((b+e)/2+1,e,2*n+1,i);
}
int query(int b, int e, int n, int l, int r)
{	if(b>r || e<l) return 0;
	if(b>=l && e<=r)
		return tr[n];
	return query((b+e)/2+1,e,2*n+1,l,r)+query(b,(b+e)/2,2*n,l,r);
}

#define N 100005
int nt[2*N];//,ar[N];
void build(int n)
{	f(i,1,n+1)
		nt[i]=(i&(-i));
}

void upd(int n,int i)
{	nt[i]--;i+=(i&(-i));
	while(i<=n)
	{	nt[i]--;
		i+=(i&(-i));
	}
}
inline int query(int l,int r)
{	int on=0,tw=0;l--;
	while(l)
	{	on+=nt[l];
		l-=(l&(-l));
	}
	while(r)
	{	tw+=nt[r];
		r-=(r&(-r));
	}
	return tw-on;
}
map<int,int> mm;
vi vec[100005];
int main()
{	int n;
	cin>>n;
	f(i,1,n+1)
	{	sc(ar[i]);
		if(mm.find(ar[i])==mm.end())
			mm[ar[i]]=ll++;
		vec[mm[ar[i]]].pb(i);
	}
	int pr=1,l,tmp;
	LL an=0;
	build(n);
	while(!mm.empty())
	{	tmp=(*(mm.begin())).S;
		if(!vec[tmp].empty() && pr>vec[tmp].back())
		{	an+=query(pr,n);
			l=0;pr=1;
		}
		else
			l=lb(all(vec[tmp]),pr)-vec[tmp].begin();
		an+=query(pr,vec[tmp].back());
		pr=vec[tmp].back();
		f(i,0,vec[tmp].size())
		{
//		if(vec[tmp][i]>=pr)
//			trace(query(pr,vec[tmp][i]));
//		else
//			an+=query(0,n-1,1,pr,n-1)+query(0,n-1,1,0,vec[tmp].S);
		upd(n,vec[tmp][i]);
//		trace(vec[tmp][i],(*(mm.begin())).F);
		}
		if(l!=0)
		{	an+=query(pr,n) + query(0,vec[tmp][l-1]);
			pr=vec[tmp][l-1];
		}
		mm.erase(mm.begin());
	}
	cout<<an<<endl;
}


#include<bits/stdc++.h>
#define f(i,a,n) for(int i=a;i<n;i++)
#define S second
#define F first
#define Sc(n) scanf("%lld",&n)
#define scc(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define Scc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define sp(a) scanf("%d %d",&a.first,&a.second)
#define Sp(a) scanf("%lld %lld",&a.first,&a.second)
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(),a.end()
#define sc(n) scanf("%d",&n)
#define It iterator
#define SET(a,b) memset(a,b,sizeof(a))
#define DRT()  int t; cin>>t; while(t--)
// inbuilt functions
// __gcd,  __builtin_ffs,     (returns least significant 1-bit, __builtin_ffsll(1)=1)
// __builtin_clz,             (returns number of leading zeroes in
// __builtin_popcount,
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> vi;
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define trv(s,it) for(auto it:s)
//#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1)
{	    cerr << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args)
{	    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#else
#define trace(...)
#endif
int ar[100005];
priority_queue<PII> qu;
int tr[200005];
void build(int b,int e,int n)
{	if(b==e)
	{	tr[n]=1;
		return;
	}
	build(b,(b+e)/2,2*n);
	build((b+e)/2+1,e,2*n+1);
	tr[n]=tr[2*n]+tr[2*n+1];
}
void update(int b,int e,int n, int i)
{	tr[n]--;
	if(b==e)
			return;
	if(i<=(b+e)/2)
		update(b,(b+e)/2,2*n,i);
	else
		update((b+e)/2+1,e,2*n+1,i);
}
int query(int b, int e, int n, int l, int r)
{	if(b>r || e<l) return 0;
	if(b>=l && e<=r)
		return tr[n];
	return query((b+e)/2+1,e,2*n+1,l,r)+query(b,(b+e)/2,2*n,l,r);
}

#define N 100005
int nt[2*N];//,ar[N];
void build(int n)
{	f(i,1,n+1)
		nt[i]=(i&(-i));
}

void upd(int n,int i)
{	nt[i]--;i+=(i&(-i));
	while(i<=n)
	{	nt[i]--;
		i+=(i&(-i));
	}
}
inline int query(int l,int r)
{	int on=0,tw=0;l--;
	while(l)
	{	on+=nt[l];
		l-=(l&(-l));
	}
	while(r)
	{	tw+=nt[r];
		r-=(r&(-r));
	}
	return tw-on;
}
map<int,vi> mm;
int main()
{	int n;
	cin>>n;
	f(i,1,n+1)
	{	sc(ar[i]);
		mm[ar[i]].pb(i);
	}
	trv(mm,it)
		sort(all(it.S));
	auto tmp=mm[ar[0]];int pr=1,l;
	LL an=0;
	build(n);
	while(!mm.empty())
	{	tmp=(*(mm.begin())).S;
		if(!tmp.empty() && pr>tmp.back())
		{	an+=query(pr,n);
			l=0;pr=1;
		}
		else
			l=lb(all(tmp),pr)-tmp.begin();
		f(i,l,tmp.size()){
//		if(tmp[i]>=pr)
			an+=query(pr,tmp[i]);
			trace(query(pr,tmp[i]));
//		else
//			an+=query(0,n-1,1,pr,n-1)+query(0,n-1,1,0,tmp.S);
		pr=tmp[i];
		upd(n,tmp[i]);
		trace(tmp[i],(*(mm.begin())).F);
		}
		if(l==0)mm.erase(mm.begin());
	}
	cout<<an<<endl;
}


#include<bits/stdc++.h>
#define f(i,a,n) for(int i=a;i<n;i++)
#define S second
#define F first
#define Sc(n) scanf("%lld",&n)
#define scc(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define Scc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define sp(a) scanf("%d %d",&a.first,&a.second)
#define Sp(a) scanf("%lld %lld",&a.first,&a.second)
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(),a.end()
#define sc(n) scanf("%d",&n)
#define It iterator
#define SET(a,b) memset(a,b,sizeof(a))
#define DRT()  int t; cin>>t; while(t--)
// inbuilt functions
// __gcd,  __builtin_ffs,     (returns least significant 1-bit, __builtin_ffsll(1)=1)
// __builtin_clz,             (returns number of leading zeroes in
// __builtin_popcount,
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> vi;
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define trv(s,it) for(auto it:s)
//#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1)
{	    cerr << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args)
{	    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#else
#define trace(...)
#endif
int ar[100005];
priority_queue<PII> qu;
int tr[200005],ll=0;
void build(int b,int e,int n)
{	if(b==e)
	{	tr[n]=1;
		return;
	}
	build(b,(b+e)/2,2*n);
	build((b+e)/2+1,e,2*n+1);
	tr[n]=tr[2*n]+tr[2*n+1];
}
void update(int b,int e,int n, int i)
{	tr[n]--;
	if(b==e)
			return;
	if(i<=(b+e)/2)
		update(b,(b+e)/2,2*n,i);
	else
		update((b+e)/2+1,e,2*n+1,i);
}
int query(int b, int e, int n, int l, int r)
{	if(b>r || e<l) return 0;
	if(b>=l && e<=r)
		return tr[n];
	return query((b+e)/2+1,e,2*n+1,l,r)+query(b,(b+e)/2,2*n,l,r);
}

#define N 100005
int nt[2*N];//,ar[N];
void build(int n)
{	f(i,1,n+1)
		nt[i]=(i&(-i));
}

void upd(int n,int i)
{	nt[i]--;i+=(i&(-i));
	while(i<=n)
	{	nt[i]--;
		i+=(i&(-i));
	}
}
inline int query(int l,int r)
{	int on=0,tw=0;l--;
	while(l)
	{	on+=nt[l];
		l-=(l&(-l));
	}
	while(r)
	{	tw+=nt[r];
		r-=(r&(-r));
	}
	return tw-on;
}
map<int,int> mm;
vi vec[100005];
int main()
{	int n;
	cin>>n;
	f(i,1,n+1)
	{	sc(ar[i]);
		if(mm.find(ar[i])==mm.end())
			mm[ar[i]]=ll++;
		vec[mm[ar[i]]].pb(i);
	}
	int pr=1,l,tmp;
	LL an=0;
	build(n);
	while(!mm.empty())
	{	tmp=(*(mm.begin())).S;
		if(!vec[tmp].empty() && pr>vec[tmp].back())
		{	an+=query(pr,n);
			l=0;pr=1;
		}
		else
			l=lb(all(vec[tmp]),pr)-vec[tmp].begin();
		f(i,l,vec[tmp].size()){
//		if(vec[tmp][i]>=pr)
			an+=query(pr,vec[tmp][i]);
			trace(query(pr,vec[tmp][i]));
//		else
//			an+=query(0,n-1,1,pr,n-1)+query(0,n-1,1,0,vec[tmp].S);
		pr=vec[tmp][i];
		upd(n,vec[tmp][i]);
		trace(vec[tmp][i],(*(mm.begin())).F);
		}
		if(l==0)mm.erase(mm.begin());
	}
	cout<<an<<endl;
}


#include<bits/stdc++.h>
#define f(i,a,n) for(int i=a;i<n;i++)
#define S second
#define F first
#define Sc(n) scanf("%lld",&n)
#define scc(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define Scc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define sp(a) scanf("%d %d",&a.first,&a.second)
#define Sp(a) scanf("%lld %lld",&a.first,&a.second)
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(),a.end()
#define sc(n) scanf("%d",&n)
#define It iterator
#define SET(a,b) memset(a,b,sizeof(a))
#define DRT()  int t; cin>>t; while(t--)
// inbuilt functions
// __gcd,  __builtin_ffs,     (returns least significant 1-bit, __builtin_ffsll(1)=1)
// __builtin_clz,             (returns number of leading zeroes in
// __builtin_popcount,
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> vi;
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define trv(s,it) for(auto it:s)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1)
{	    cerr << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args)
{	    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#else
#define trace(...)
#endif
int ar[100005];
priority_queue<PII> qu;
int tr[200005],ll=0;
void build(int b,int e,int n)
{	if(b==e)
	{	tr[n]=1;
		return;
	}
	build(b,(b+e)/2,2*n);
	build((b+e)/2+1,e,2*n+1);
	tr[n]=tr[2*n]+tr[2*n+1];
}
void update(int b,int e,int n, int i)
{	tr[n]--;
	if(b==e)
			return;
	if(i<=(b+e)/2)
		update(b,(b+e)/2,2*n,i);
	else
		update((b+e)/2+1,e,2*n+1,i);
}
int query(int b, int e, int n, int l, int r)
{	if(b>r || e<l) return 0;
	if(b>=l && e<=r)
		return tr[n];
	return query((b+e)/2+1,e,2*n+1,l,r)+query(b,(b+e)/2,2*n,l,r);
}

#define N 100005
int nt[2*N];//,ar[N];
void build(int n)
{	f(i,1,n+1)
		nt[i]=(i&(-i));
}

void upd(int n,int i)
{	nt[i]--;i+=(i&(-i));
	while(i<=n)
	{	nt[i]--;
		i+=(i&(-i));
	}
}
inline int query(int l,int r)
{	int on=0,tw=0;l--;
	while(l)
	{	on+=nt[l];
		l-=(l&(-l));
	}
	while(r)
	{	tw+=nt[r];
		r-=(r&(-r));
	}
	return tw-on;
}
map<int,int> mm;
vi vec[100005];
int main()
{	int n;
	cin>>n;
//	f(i,1,n+1)vec[i].clear();
	f(i,1,n+1)
	{	sc(ar[i]);
		if(mm.find(ar[i])==mm.end())
			mm[ar[i]]=ll++;
		vec[mm[ar[i]]].pb(i);
	}
	int pr=1,l,tmp;
	LL an=0;
	build(n);
	while(!mm.empty())
	{	tmp=(*(mm.begin())).S;
		if(vec[tmp].empty())
		{	mm.erase(mm.begin()); continue; }
		if(pr>vec[tmp].back())
		{	an+=query(pr+1,n);
			l=0;pr=1;
		}
		else
			l=lb(all(vec[tmp]),pr)-vec[tmp].begin();
		trace(pr,vec[tmp].back());
		an+=query(pr,vec[tmp].back());
		pr=vec[tmp].back();
		if(l!=0)
		{	trace(pr,l,vec[tmp][l-1]);
			an+=query(pr+1,n) + query(1,vec[tmp][l-1]);
			pr=vec[tmp][l-1];
		}
		f(i,0,vec[tmp].size())
		{
//		if(vec[tmp][i]>=pr)
//			trace(query(pr,vec[tmp][i]));
//		else
//			an+=query(0,n-1,1,pr,n-1)+query(0,n-1,1,0,vec[tmp].S);
		upd(n,vec[tmp][i]);
//		trace(vec[tmp][i],(*(mm.begin())).F);
		}
		mm.erase(mm.begin());
	}
	cout<<an<<endl;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define mid ((l + r) >> 1)
#define lson (x << 1)
#define rson ((x << 1) | 1)
using namespace std;
typedef long long LL;

LL add[800005] = {0};
LL val[800005] = {0};

void apply(LL x,LL l,LL r,LL v){
	val[x] += v * (LL)(r - l + 1); add[x] += v;
}

void pushdown(LL x,LL l,LL r){
	if(!add[x]) return;
	apply(lson,l,mid,add[x]);
	apply(rson,mid + 1,r,add[x]);
	add[x] = 0;
}

void modify(LL x,LL l,LL r,LL L,LL R,LL v){
	if(L <= l && r <= R){ apply(x,l,r,v); return; }
	pushdown(x,l,r);
	if(L <= mid) modify(lson,l,mid,L,R,v);
	if(R > mid) modify(rson,mid + 1,r,L,R,v);
	val[x] = val[lson] + val[rson];
}

LL query(LL x,LL l,LL r,LL L,LL R){
	if(L <= l && r <= R) return val[x];
	LL ret = 0;
	pushdown(x,l,r);
	if(L <= mid) ret += query(lson,l,mid,L,R);
	if(R > mid) ret += query(rson,mid + 1,r,L,R);
	val[x] = val[lson] + val[rson];
	return ret;
}

LL n,p,ans = 0;
LL a[100005] = {0};
vector <LL> pos[100005];

int main(){
	cin >> n;
	modify(1,1,n,1,n,1);
	for(LL i = 1;i <= n;i ++){
		cin >> p;
		pos[p].push_back(i);
	}
	p = 1;
	for(LL i = 1;i <= 100000;i ++){
		LL to = 0;
		while(1){
			if(to == pos[i].size()) break;
			if(p > pos[i][to]){
				ans += query(1,1,n,p,n);
				p = 1;
			}
			ans += query(1,1,n,p,pos[i][to]);
			modify(1,1,n,pos[i][to],pos[i][to],-1);
			p = pos[i][to] + 1;
			to ++;
		}
	}
	cout << ans << endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define mid ((l + r) >> 1)
#define lson (x << 1)
#define rson ((x << 1) | 1)
using namespace std;
typedef long long LL;

LL add[800005] = {0};
LL val[800005] = {0};

void apply(LL x,LL l,LL r,LL v){
	val[x] += v * (LL)(r - l + 1); add[x] += v;
}

void pushdown(LL x,LL l,LL r){
	if(!add[x]) return;
	apply(lson,l,mid,add[x]);
	apply(rson,mid + 1,r,add[x]);
	add[x] = 0;
}

void modify(LL x,LL l,LL r,LL L,LL R,LL v){
	if(L <= l && r <= R){ apply(x,l,r,v); return; }
	pushdown(x,l,r);
	if(L <= mid) modify(lson,l,mid,L,R,v);
	if(R > mid) modify(rson,mid + 1,r,L,R,v);
	val[x] = val[lson] + val[rson];
}

LL query(LL x,LL l,LL r,LL L,LL R){
	if(L <= l && r <= R) return val[x];
	LL ret = 0;
	pushdown(x,l,r);
	if(L <= mid) ret += query(lson,l,mid,L,R);
	if(R > mid) ret += query(rson,mid + 1,r,L,R);
	val[x] = val[lson] + val[rson];
	return ret;
}

LL n,p,ans = 0;
LL a[100005] = {0};
vector <LL> pos[100005];

int main(){
	cin >> n;
	modify(1,1,n,1,n,1);
	for(LL i = 1;i <= n;i ++){
		cin >> p;
		pos[p].push_back(i);
	}
	p = 1;
	for(LL i = 1;i <= 100000;i ++){
		LL to = 0;
		while(1){
			if(to == pos[i].size()) break;
			if(p > pos[i][to]){
				ans += query(1,1,n,p,n);
				p = 1;
			}
			ans += query(1,1,n,p,pos[i][to]);
			modify(1,1,n,pos[i][to],pos[i][to],-1);
			p = pos[i][to] + 1;
			to ++;
		}
	}
	cout << ans << endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define mid ((l + r) >> 1)
#define lson (x << 1)
#define rson ((x << 1) | 1)
using namespace std;
typedef long long LL;

LL add[800005] = {0};
LL val[800005] = {0};

void apply(LL x,LL l,LL r,LL v){
	val[x] += v * (LL)(r - l + 1); add[x] += v;
}

void pushdown(LL x,LL l,LL r){
	if(!add[x]) return;
	apply(lson,l,mid,add[x]);
	apply(rson,mid + 1,r,add[x]);
	add[x] = 0;
}

void modify(LL x,LL l,LL r,LL L,LL R,LL v){
	if(L <= l && r <= R){ apply(x,l,r,v); return; }
	pushdown(x,l,r);
	if(L <= mid) modify(lson,l,mid,L,R,v);
	if(R > mid) modify(rson,mid + 1,r,L,R,v);
	val[x] = val[lson] + val[rson];
}

LL query(LL x,LL l,LL r,LL L,LL R){
	if(L > R) return 0;
	if(L <= l && r <= R) return val[x];
	LL ret = 0;
	pushdown(x,l,r);
	if(L <= mid) ret += query(lson,l,mid,L,R);
	if(R > mid) ret += query(rson,mid + 1,r,L,R);
	val[x] = val[lson] + val[rson];
	return ret;
}

LL n,p,ans = 0;
LL a[100005] = {0};
vector <LL> pos[100005];

int main(){
	cin >> n;
	modify(1,1,n,1,n,1);
	for(LL i = 1;i <= n;i ++){
		cin >> p;
		pos[p].push_back(i);
	}
	p = 1;
	for(LL i = 1;i <= 100000;i ++){
		if(!pos[i].size()) continue;
		LL to = pos[i].size() - 1;
		for(LL j = 0;j < pos[i].size();j ++){
			if(pos[i][j] < p) to = j;
			else break;
		}
		if(p > pos[i][to]){
			ans += query(1,1,n,p,n);
			p = 1;
		}
		ans += query(1,1,n,p,pos[i][to]);
		for(LL j = 0;j < pos[i].size();j ++){
			modify(1,1,n,pos[i][j],pos[i][j],-1);
		}
		p = pos[i][to] + 1;
	}
	cout << ans << endl;
	return 0;
}


// time is tough ...so don't stop ...keep learning ...keep growing :)

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef pair<int,int> ii;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef tree<pair<int,int>, null_type,less<pair<int, int> >,rb_tree_tag,tree_order_statistics_node_update > pbds;

#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
#define debug4(x,y,z,w) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<endl;

#define pb push_back
#define ft first
#define sd second
#define all(c)    (c).begin(),(c).end()
#define tr(c,i)   for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x)   ((c).find(x) != (c).end())
#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

ll poww(ll a,ll b){ll res=1;while(b){if(b&1){res*=a;}a=a*a;b>>=1;}return res;}
ll poww(ll a,ll b,ll mod){ll res=1;while(b){if(b&1){res*=a;res%=mod;}a=a*a;a%=mod;b>>=1;}return res;}

const int INF = 2e9 ;
const int dx[] = {-1,0,0,1};
const int dy[] = {0,-1,1,0};
const int lg = 21;
const ll mod = 1e9 + 7;
// #define int ll
const int N = 1e5 + 5;

int n, BIT[N];

void upd(int id, int x) {
    for(; id <= n; id += id&-id) {
        BIT[id] += x;
    }
}

int query(int id) {
    int ans = 0;
    for(; id > 0; id -= id&-id) {
        ans += BIT[id];
    }
    return ans;
}

signed main() {
    cin >> n;
    int a[n + 4];
    set<ii> s;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        s.insert({a[i], i});
        upd(i, 1);
    }
    //for(int i=1;i<=n; i++) cout << BIT[i] ;
    int last = 0;
    ll ans = 0;
    for(int i = 1; i <= n; i++) {
        int to_remove = (*s.begin()).sd;
        if(to_remove > last) {
            ans += 1ll*(query(to_remove) - query(last));

            last = to_remove;
        }
        else {
            ans += 1ll*(query(n) - query(last) + query(to_remove));
        }
        upd(to_remove, -1);
        s.erase(s.begin());
        last = to_remove;
    }
    cout << ans << '\n';
    return 0;
}


// time is tough ...so don't stop ...keep learning ...keep growing :)

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef pair<int,int> ii;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef tree<pair<int,int>, null_type,less<pair<int, int> >,rb_tree_tag,tree_order_statistics_node_update > pbds;

#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
#define debug4(x,y,z,w) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<endl;

#define pb push_back
#define ft first
#define sd second
#define all(c)    (c).begin(),(c).end()
#define tr(c,i)   for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x)   ((c).find(x) != (c).end())
#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

ll poww(ll a,ll b){ll res=1;while(b){if(b&1){res*=a;}a=a*a;b>>=1;}return res;}
ll poww(ll a,ll b,ll mod){ll res=1;while(b){if(b&1){res*=a;res%=mod;}a=a*a;a%=mod;b>>=1;}return res;}

const int INF = 2e9 ;
const int dx[] = {-1,0,0,1};
const int dy[] = {0,-1,1,0};
const int lg = 21;
const ll mod = 1e9 + 7;
// #define int ll
const int N = 1e5 + 5;

int n, BIT[N];

void upd(int id, int x) {
    for(; id <= n; id += id&-id) {
        BIT[id] += x;
    }
}

int query(int id) {
    int ans = 0;
    for(; id > 0; id -= id&-id) {
        ans += BIT[id];
    }
    return ans;
}

signed main() {
    cin >> n;
    int a[n + 4];
    set<ii> s;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        s.insert({a[i], i});
        upd(i, 1);
    }
    ll ans = 0;
    ii last = {0, 0};
    for(int i = 1; i <= n; i++) {
        int curr = (*s.begin()).ft;
        last.ft = curr;
        auto it = s.lower_bound(last);
        if(it->ft == (*s.begin()).ft) {
            ans += 1ll*(query(it->sd) - query(last.sd));
        }
        else {
            it = s.begin();
            ans += 1ll*(query(n) - query(last.sd) + query(it->sd));
        }
        upd(it->sd, -1);
        last = *it;
        s.erase(it);
    }
    cout << ans << '\n';
    return 0;
}


// time is tough ...so don't stop ...keep learning ...keep growing :)

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef pair<int,int> ii;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef tree<pair<int,int>, null_type,less<pair<int, int> >,rb_tree_tag,tree_order_statistics_node_update > pbds;

#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
#define debug4(x,y,z,w) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<endl;

#define pb push_back
#define ft first
#define sd second
#define all(c)    (c).begin(),(c).end()
#define tr(c,i)   for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x)   ((c).find(x) != (c).end())
#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

ll poww(ll a,ll b){ll res=1;while(b){if(b&1){res*=a;}a=a*a;b>>=1;}return res;}
ll poww(ll a,ll b,ll mod){ll res=1;while(b){if(b&1){res*=a;res%=mod;}a=a*a;a%=mod;b>>=1;}return res;}

const int INF = 2e9 ;
const int dx[] = {-1,0,0,1};
const int dy[] = {0,-1,1,0};
const int lg = 21;
const ll mod = 1e9 + 7;
// #define int ll
const int N = 1e5 + 5;

int n, BIT[N];

void upd(int id, int x) {
    for(; id <= n; id += id&-id) {
        BIT[id] += x;
    }
}

int query(int id) {
    int ans = 0;
    for(; id > 0; id -= id&-id) {
        ans += BIT[id];
    }
    return ans;
}

signed main() {
    cin >> n;
    int a[n + 4];
    set<ii> s;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        s.insert({a[i], i});
        upd(i, 1);
    }
    ll ans = 0;
    ii last = {0, 0};
    for(int i = 1; i <= n; i++) {
        int curr = (*s.begin()).ft;
        last.ft = curr;
        auto it = s.lower_bound(last);
        if(it->ft == (*s.begin()).ft) {
            ans += 1ll*(query(it->sd) - query(last.sd));
        }
        else {
            it = s.begin();
            ans += 1ll*(query(n) - query(last.sd) + query(it->sd));
        }
        upd(it->sd, -1);
        last = *it;
        s.erase(s.begin());
    }
    cout << ans << '\n';
    return 0;
}


#include <iostream>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <utility>
#include <map>
#define MAXN 100003
#define inf 1000000000
#define X 15002
#define Y 71
#define V 26
#define ll long long
#define pb push_back
#define mp make_pair
#define mod1 1000000007
#define mod2 1000000009
using namespace std;
int ans = 0,last;
vector<int>vec[MAXN];
vector<int>unmarked;
int a[MAXN],b[MAXN],n,x;
//hld implementation
int main()
{

  cin>>n;
  for(int i=0;i<n;i++)
  {
    cin>>x;
    vec[x].pb(i);
    b[i]=x;
  }
  sort(b,b+n);
  int prev = -1;
  ans = n;
  int totalleft=n;
//  cout<<vec[3][0]<<endl;
  for(int i=0;i<n;i++)unmarked.pb(i);
  for(int i=0;i<n;i++)
  {
    int val = b[i];
    auto it = lower_bound(vec[b[i]].begin(),vec[b[i]].end(),prev);
   // cout<<val<<" "<<prev<<" "<<*it<<endl;
    if(it!=vec[b[i]].end())
    {
      //cout<<*it<<endl;
      auto it1 = lower_bound(unmarked.begin(),unmarked.end(),*it);
      totalleft--;
      prev = *it;
      unmarked.erase(it1);
      last = unmarked.back();
      vec[b[i]].erase(it);
    }
    else
    {
       // cout<<last<<endl;
        prev = vec[b[i]][0];
        vec[b[i]].erase(vec[b[i]].begin());
        ans+=(totalleft);
        totalleft--;
        auto it1 = lower_bound(unmarked.begin(),unmarked.end(),prev);
        unmarked.erase(it1);
        last = unmarked.back();
    }
  }
  cout<<ans<<endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <utility>
#include <map>
#define MAXN 100003
#define inf 1000000000
#define X 15002
#define Y 71
#define V 26
#define ll long long
#define pb push_back
#define mp make_pair
#define mod1 1000000007
#define mod2 1000000009
using namespace std;
ll int ans = 0,last;
vector<ll int>vec[MAXN];
vector<ll int>unmarked;
ll int a[MAXN],b[MAXN],n,x;
//hld implementation
int main()
{

  cin>>n;
  for(ll int i=0;i<n;i++)
  {
    cin>>x;
    vec[x].pb(i);
    b[i]=x;
  }
  sort(b,b+n);
  ll int prev = -1;
  ans = n;
  ll int totalleft=n;
//  cout<<vec[3][0]<<endl;
  for(ll int i=0;i<n;i++)unmarked.pb(i);
  for(ll int i=0;i<n;i++)
  {
    ll int val = b[i];
    auto it = lower_bound(vec[b[i]].begin(),vec[b[i]].end(),prev);
   // cout<<val<<" "<<prev<<" "<<*it<<endl;
    if(it!=vec[b[i]].end())
    {
      //cout<<*it<<endl;
      auto it1 = lower_bound(unmarked.begin(),unmarked.end(),*it);
      totalleft--;
      prev = *it;
      unmarked.erase(it1);
      last = unmarked.back();
      vec[b[i]].erase(it);
    }
    else
    {
       // cout<<last<<endl;
        prev = vec[b[i]][0];
        vec[b[i]].erase(vec[b[i]].begin());
        ans+=(totalleft);
        totalleft--;
        auto it1 = lower_bound(unmarked.begin(),unmarked.end(),prev);
        unmarked.erase(it1);
        last = unmarked.back();
    }
  }
  cout<<ans<<endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <utility>
#include <map>
#define MAXN 100003
#define inf 1000000000
#define X 15002
#define Y 71
#define V 26
#define ll long long
#define pb push_back
#define mp make_pair
#define mod1 1000000007
#define mod2 1000000009
using namespace std;
ll int ans = 0,last;
set<ll int>vec[MAXN];
set<ll int>unmarked;
ll int a[MAXN],b[MAXN],n,x;
//hld implementation
int main()
{

  cin>>n;
  for(ll int i=0;i<n;i++)
  {
    cin>>x;
    vec[x].insert(i);
    b[i]=x;
  }
  sort(b,b+n);
  ll int prev = -1;
  ans = n;
  ll int totalleft=n;
//  cout<<vec[3][0]<<endl;
  for(ll int i=0;i<n;i++)unmarked.insert(i);
  for(ll int i=0;i<n;i++)
  {
    ll int val = b[i];
    auto it = vec[b[i]].lower_bound(prev);
   // cout<<val<<" "<<prev<<" "<<*it<<endl;
    if(it!=vec[b[i]].end())
    {
      //cout<<*it<<endl;
      auto it1 = unmarked.lower_bound(*it);
      totalleft--;
      prev = *it;
      unmarked.erase(it1);
      if(!unmarked.empty())
      last = *(unmarked.rbegin());
      vec[b[i]].erase(it);
    }
    else
    {
       // cout<<last<<endl;
        prev = *(vec[b[i]].begin());
        vec[b[i]].erase(vec[b[i]].begin());
        ans+=(totalleft);
        totalleft--;
        auto it1 = unmarked.lower_bound(prev);
        unmarked.erase(it1);
        if(!unmarked.empty())
        last = *(unmarked.rbegin());
    }
  }
  cout<<ans<<endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <utility>
#include <map>
#define MAXN 100003
#define inf 1000000000
#define X 15002
#define Y 71
#define V 26
#define ll long long
#define pb push_back
#define mp make_pair
#define mod1 1000000007
#define mod2 1000000009
using namespace std;
int ans = 0,last;
vector<int>vec[MAXN];
vector<int>unmarked;
int a[MAXN],b[MAXN],n,x;
//hld implementation
int main()
{

  cin>>n;
  for(int i=0;i<n;i++)
  {
    cin>>x;
    vec[x].pb(i);
    b[i]=x;
  }
  sort(b,b+n);
  int prev = -1;
  ans = n;
//  cout<<vec[3][0]<<endl;
  for(int i=0;i<n;i++)unmarked.pb(i);
  for(int i=0;i<n;i++)
  {
    int val = b[i];
    auto it = lower_bound(vec[b[i]].begin(),vec[b[i]].end(),prev);
   // cout<<val<<" "<<prev<<" "<<*it<<endl;
    if(it!=vec[b[i]].end())
    {
      //cout<<*it<<endl;
      auto it1 = lower_bound(unmarked.begin(),unmarked.end(),*it);
      prev = *it;
      unmarked.erase(it1);
      last = unmarked.back();
      vec[b[i]].erase(it);
    }
    else
    {
       // cout<<last<<endl;
        prev = vec[b[i]][0];
        vec[b[i]].erase(vec[b[i]].begin());
        ans+=(last+1);
        auto it1 = lower_bound(unmarked.begin(),unmarked.end(),prev);
        unmarked.erase(it1);
        last = unmarked.back();
    }
  }
  cout<<ans<<endl;
  return 0;
}


/***
7
7 1 2 3 4 5 6
***/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
struct BIT
{
	int A[100007];
	void init()
	{
		fill_n(A, 100007, 0);
	}
	void add(int i, int k)
	{
		for (; i < 100007; i += (i) & (-i))
			A[i] += k;
	}
	int get(int i)
	{
		int s = 0;
		for (; i > 0; i -= (i) & (-i))
			s += A[i];
		return s;
	}
	int get(int l, int r)
	{
		return get(r) - get(l - 1);
	}
};
struct comp
{
	bool operator()(pair<int, int> a, pair<int, int> b)
	{
		if (a.first != b.first)
			return a.first > b.first;
		return a.second > b.second;
	}
};
set<int>Ko[100007];
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++)
	{
		int c;
		cin >> c;
		Ko[c].insert(i);
	}
	BIT X;
	X.init();
	for (int i = 1; i <= n; i++)
		X.add(i, 1);
	int st = 0;
	int ans = 0;
	for (int c = 0; c <= 100000; c++)
	{
		while (!Ko[c].empty())
		{
			auto it = Ko[c].upper_bound(st);
			if (it != Ko[c].end())
			{
				int i = *it;
				Ko[c].erase(it);
				ans += X.get(st, i);
				X.add(i, -1);
				st = i;
			}
			else
			{
				it = Ko[c].begin();
				int i = *it;
				Ko[c].erase(it);
				ans += X.get(st, n);
				ans += X.get(1, i);
				X.add(i, -1);
				st = i;
			}
		}
	}
	cout << ans;
}


/***
7
7 1 2 3 4 5 6
***/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
struct BIT
{
	ll A[100007];
	void init()
	{
		fill_n(A, 100007, 0);
	}
	void add(ll i, ll k)
	{
		for (; i < 100007; i += (i) & (-i))
			A[i] += k;
	}
	ll get(ll i)
	{
		ll s = 0;
		for (; i > 0; i -= (i) & (-i))
			s += A[i];
		return s;
	}
	ll get(ll l, ll r)
	{
		return get(r) - get(l - 1);
	}
};
struct comp
{
	bool operator()(pair<ll, ll> a, pair<ll, ll> b)
	{
		if (a.first != b.first)
			return a.first > b.first;
		return a.second > b.second;
	}
};
set<int>Ko[100007];
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
	ll n;
	cin >> n;
	for (ll i = 1; i <= n; i++)
	{
		ll c;
		cin >> c;
		Ko[c].insert(i);
	}
	BIT X;
	X.init();
	for (ll i = 1; i <= n; i++)
		X.add(i, 1);
	ll st = 0;
	ll ans = 0;
	for (int c = 0; c <= 100000; c++)
	{
		while (!Ko[c].empty())
		{
			auto it = Ko[c].upper_bound(st);
			if (it != Ko[c].end())
			{
				int i = *it;
				Ko[c].erase(it);
				ans += X.get(st, i);
				X.add(i, -1);
			}
			else
			{
				it = Ko[c].begin();
				int i = *it;
				Ko[c].erase(it);
				ans += X.get(st, n);
				ans += X.get(1, i);
				X.add(i, -1);
			}
			st = i;
		}
	}
	cout << ans;
}


/***
7
7 1 2 3 4 5 6
***/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
struct BIT
{
	ll A[100007];
	void init()
	{
		fill_n(A, 100007, 0);
	}
	void add(ll i, ll k)
	{
		for (; i < 100007; i += (i) & (-i))
			A[i] += k;
	}
	ll get(ll i)
	{
		ll s = 0;
		for (; i > 0; i -= (i) & (-i))
			s += A[i];
		return s;
	}
	ll get(ll l, ll r)
	{
		return get(r) - get(l - 1);
	}
};
struct comp
{
	bool operator()(pair<ll, ll> a, pair<ll, ll> b)
	{
		if (a.first != b.first)
			return a.first > b.first;
		return a.second > b.second;
	}
};
set<int>Ko[100007];
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
	ll n;
	cin >> n;
	for (ll i = 1; i <= n; i++)
	{
		ll c;
		cin >> c;
		Ko[c].insert(i);
	}
	BIT X;
	X.init();
	for (ll i = 1; i <= n; i++)
		X.add(i, 1);
	ll st = 0;
	ll ans = 0;
	for (int c = 0; c <= 100000; c++)
	{
		while (!Ko[c].empty())
		{
			if (Ko[c].upper_bound(st) != Ko[c].end())
			{
				int i = *Ko[c].upper_bound(st);
				Ko[c].erase(Ko[c].upper_bound(st));
				ans += X.get(st, i);
				X.add(i, -1);
				st = i;
			}
			else
			{
				int i = *Ko[c].begin();
				Ko[c].erase(Ko[c].begin());
				ans += X.get(st, n);
				ans += X.get(1, i);
				X.add(i, -1);
				st = i;
			}
		}
	}
	cout << ans;
}


/***
7
7 1 2 3 4 5 6
***/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
struct BIT
{
	int A[100007];
	void init()
	{
		fill_n(A, 100007, 0);
	}
	void add(int i, int k)
	{
		for (; i < 100007; i += (i) & (-i))
			A[i] += k;
	}
	int get(int i)
	{
		int s = 0;
		for (; i > 0; i -= (i) & (-i))
			s += A[i];
		return s;
	}
	int get(int l, int r)
	{
		return get(r) - get(l - 1);
	}
};
struct comp
{
	bool operator()(pair<int, int> a, pair<int, int> b)
	{
		if (a.first != b.first)
			return a.first > b.first;
		return a.second > b.second;
	}
};
set<int>Ko[100007];
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++)
	{
		int c;
		cin >> c;
		Ko[c].insert(i);
	}
	BIT X;
	X.init();
	for (int i = 1; i <= n; i++)
		X.add(i, 1);
	int st = 0;
	ll ans = 0;
	for (int c = 0; c <= 100000; c++)
	{
		while (!Ko[c].empty())
		{
			auto it = Ko[c].upper_bound(st);
			if (it != Ko[c].end())
			{
				int i = *it;
				Ko[c].erase(it);
				ans += X.get(st, i);
				X.add(i, -1);
				st = i;
			}
			else
			{
				it = Ko[c].begin();
				int i = *it;
				Ko[c].erase(it);
				ans += X.get(st, n);
				ans += X.get(1, i);
				X.add(i, -1);
				st = i;
			}
		}
	}
	cout << ans;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const ll mxn = 100005;
ll n, last, ans, a[mxn];
struct Node{
    ll idx, val, sum;
}seg[4 * mxn], mx_Node;
Node merge(Node lnode, Node rnode){
    Node ans = mx_Node;
    ans.sum = lnode.sum + rnode.sum;
    ans.val = min(lnode.val, rnode.val);
    if(lnode.val == rnode.val)
        ans.idx = min(lnode.idx, rnode.idx);
    else if(ans.val == lnode.val)
        ans.idx = lnode.idx;
    else
        ans.idx = rnode.idx;
    return ans;
}
void build(int s, int e, int idx){
    if(s == e){
        seg[idx].val = a[s];
        seg[idx].idx = s;
        seg[idx].sum = 1;
        return;
    }
    int mid = (s + e) / 2;
    build(s, mid, 2 * idx + 1);
    build(mid + 1, e, 2 * idx + 2);
    seg[idx] = merge(seg[2 * idx + 1], seg[2 * idx + 2]);
}
void update(int l, int r, int s, int e, int idx){
    if(s > r || l > e)
        return ;
    if(s >= l && e <= r){
        seg[idx].val = INT_MAX;
        seg[idx].sum = 0;
        return ;
    }
    int mid = (s + e) / 2;
    update(l, r, s, mid, 2 *idx + 1);
    update(l, r, mid + 1, e, 2 *idx + 2);
    seg[idx] = merge(seg[2 * idx + 1], seg[2 * idx + 2]);
}
Node qry(int l, int r, int s, int e, int idx){
    if(s > r || l > e)
        return mx_Node;
    if(s >= l && e <= r)
        return seg[idx];
    int mid = (s + e) / 2;
    return merge(qry(l, r, s, mid, 2 * idx + 1), qry(l, r, mid + 1, e, 2 * idx + 2));
}
int get_nxt(int last){
    Node mn = qry(0, n - 1, 0, n - 1, 0);
    Node rem = qry(last + 1, n - 1, 0, n - 1, 0);
    if(mn.val == rem.val || rem.val < mn.val)
        return rem.idx;
    return mn.idx;
}
int main()
{
    ios::sync_with_stdio(0);
    mx_Node.idx = mx_Node.val = INT_MAX;
    mx_Node.sum = 0;
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    build(0, n - 1, 0);
    last = -1;
    for(int i = 0; i < n; i++){
        int idx = get_nxt(last);
        if(idx > last)
            ans += qry(last + 1, idx, 0, n - 1, 0).sum;
        else
            ans += qry(last + 1, n - 1, 0, n - 1, 0).sum + qry(0, idx, 0, n - 1, 0).sum;
        update(idx, idx, 0, n - 1, 0);
        last = idx;
    }
    cout << ans << endl;
    return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const ll mxn = 1 << 17;
ll n, last, ans, a[mxn], fen[mxn];
map<int,set<int> >mp;
void update(int idx, int val){
    while(idx < mxn){
        fen[idx] += val;
        idx += idx & -idx;
    }
}
int qry(int s, int e){
    int l = 0, r = 0;
    s--;
    while(s > 0)
        l += fen[s], s -= s & -s;
    while(e > 0)
        r += fen[e], e -= e & -e;
     return r - l;
}
int get_nxt(int last, int cur){
    int idx;
    if(mp[cur].empty())
        mp.erase(cur), cur = mp.begin()->first;
    auto it = mp[cur].lower_bound(last);
    if(it == mp[cur].end())
        idx = *mp[cur].begin();
    else
        idx = *it;
    mp[cur].erase(idx);
    return idx;
}
int main()
{
    ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        mp[a[i]].insert(i);
        update(i, 1);
    }
    for(int i = 0; i < n; i++){
        int idx = get_nxt(last, mp.begin()->first);
        if(idx > last)
            ans += qry(last + 1, idx);
        else
            ans += qry(last + 1, n) + qry(1, idx);
        update(idx, -1);
        last = idx;
    }
    cout << ans << endl;
    return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const ll mxn = 1 << 17;
ll n, last, ans, a[mxn], fen[mxn];
set<int>s;
map<int,set<int> >mp;
void update(int idx, int val){
    while(idx < mxn){
        fen[idx] += val;
        idx += idx & -idx;
    }
}
int qry(int s, int e){
    int l = 0, r = 0;
    s--;
    while(s > 0)
        l += fen[s], s -= s & -s;
    while(e > 0)
        r += fen[e], e -= e & -e;
     return r - l;
}
int get_nxt(int last, int cur){
    int idx;
    if(mp[cur].empty())
        s.erase(cur), cur = *s.begin();
    auto it = mp[cur].lower_bound(last);
    if(it == mp[cur].end())
        idx = *mp[cur].begin();
    else
        idx = *it;
    mp[cur].erase(idx);
    return idx;
}
int main()
{
    ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        s.insert(a[i]);
        mp[a[i]].insert(i);
        update(i, 1);
    }
    for(int i = 0; i < n; i++){
        int idx = get_nxt(last, *s.begin());
        if(idx > last)
            ans += qry(last + 1, idx);
        else
            ans += qry(last + 1, n) + qry(1, idx);
        update(idx, -1);
        last = idx;
    }
    cout << ans << endl;
    return 0;
}


#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const ll mxn = 1 << 17;
ll n, last, ans, a[mxn], fen[mxn];
set<int>s;
map<int,set<int> >mp;
void update(int idx, int val){
    while(idx < mxn){
        fen[idx] += val;
        idx += idx & -idx;
    }
}
int qry(int s, int e){
    int l = 0, r = 0;
    s--;
    while(s > 0)
        l += fen[s], s -= s & -s;
    while(e > 0)
        r += fen[e], e -= e & -e;
     return r - l;
}
int get_nxt(int last, int cur){
    int idx;
    if(mp[cur].empty())
        mp.erase(cur), s.erase(cur), cur = *s.begin();
    auto it = mp[cur].lower_bound(last);
    if(it == mp[cur].end())
        idx = *mp[cur].begin();
    else
        idx = *it;
    mp[cur].erase(idx);
    return idx;
}
int main()
{
    ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        s.insert(a[i]);
        mp[a[i]].insert(i);
        update(i, 1);
    }
    for(int i = 0; i < n; i++){
        int idx = get_nxt(last, *s.begin());
        if(idx > last)
            ans += qry(last + 1, idx);
        else
            ans += qry(last + 1, n) + qry(1, idx);
        update(idx, -1);
        last = idx;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define TAM 100100
#define mid ((fr+to)>>1)

int card[TAM];
int mini ( int i, int j ) {
  if ( card[i] == card[j] )
    return i < j ? i : j;
  return card[i] < card[j] ? i : j;
}

int bit[TAM];

void updateBit ( int i, int x ) {
  for ( i++; i < TAM; i += (i&(-i)) )
    bit[i] += x;
}

int queryBit ( int i ) {
  int ans = 0;
  for ( i++; i; i -= (i&(-i)) )
    ans += bit[i];
  return ans;
}

int queryBit ( int i, int j ) {
  return queryBit(j) - queryBit(i-1);
}

struct Node {
  Node *L, *R;
  int fr, to, i;

  int query ( int a, int b ) {
    if ( fr == a && to == b ) return i;
    if ( b <= mid ) return L->query(a,b);
    if ( a > mid ) return R->query(a,b);
    return mini ( L->query(a,mid), R->query(mid+1,b) );
  }

  void update ( int pos ) {
    if ( fr == to ) return;
    if ( mid >= pos ) L->update(pos);
    else R->update(pos);
    i = mini ( L->i, R->i );
  }

  Node ( int a, int b ) : fr(a), to(b) {
    if ( a == b )
      L = R = 0, i = a;
    else {
      L = new Node( fr, mid );
      R = new Node( mid+1, to );
      i = mini ( L->i, R->i );
    }
  }
};

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

  for(int i = 0; i < n; ++i ) {
    scanf ( "%d", card+i );
    updateBit(i,1);
  }

  vector<int> s ( card, card+n );
  sort ( s.begin(), s.end() );

  Node stree ( 0, n-1 );

  ll ans = 0;
  int pos = 0;
  for ( int i = 0; i < n; ++i ) {
    const int minVal = card[stree.query(0,n-1)];
    assert ( minVal == s[i] );
    int newPos = stree.query ( pos, n-1 );
    if ( card[newPos] != minVal ) {
      ans += queryBit(pos, n-1);
      newPos = stree.query(0, pos);
      ans += queryBit(0, newPos);
    }
    else {
      //cout << "ans += queryBit(" << pos << "," << newPos << ")" << endl;
      ans += queryBit(pos, newPos);
    }
    pos = newPos;
    card[pos] = INT_MAX;
    stree.update(pos);
    updateBit(pos, -1);
  }

  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define TAM 100100
#define mid ((fr+to)>>1)

int card[TAM];
int mini ( int i, int j ) {
  if ( card[i] == card[j] )
    return i < j ? i : j;
  return card[i] < card[j] ? i : j;
}

struct Node {
  Node *L, *R;
  int fr, to, i;

  int query ( int a, int b ) {
    if ( fr == a && to == b ) return i;
    if ( b <= mid ) return L->query(a,b);
    if ( a > mid ) return R->query(a,b);
    return mini ( L->query(a,mid), R->query(mid+1,b) );
  }

  void remove ( int pos ) {
    if ( fr == to ) {
      card[pos] = INT_MAX;
      return;
    }
    if ( mid >= i ) L->remove(i);
    else R->remove(i);
    i = mini ( L->i, R->i );
  }

  Node ( int a, int b ) : fr(a), to(b) {
    if ( a == b )
      L = R = 0, i = a;
    else {
      L = new Node( fr, mid );
      R = new Node( mid+1, to );
      i = mini ( L->i, R->i );
    }
  }
};

int main() {
  int n;
  scanf ( "%d", &n );
  for(int i = 0; i < n; ++i ) scanf ( "%d", card+i );

  Node stree ( 0, n-1 );

  ll ans = 0;
  int pos = 0;
  for ( int i = 0; i < n; ++i ) {
    int newPos = stree.query ( pos, n-1 );
    if ( card[newPos] == INT_MAX ) {
      newPos = stree.query ( 0, pos );
      ans += n-1 - pos;
      ans += newPos;
    }
    else ans += newPos - pos;
    pos = newPos;
    stree.remove(pos);
  }

  cout << ans+1LL << endl;
  return 0;
}


#include <bits/stdc++.h>
#define snd second
#define fst first
#define mk make_pair
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
#define TAM 100100
#define mid ((fr+to)>>1)

int card[TAM];
int mini ( int i, int j ) {
  if ( card[i] == card[j] )
    return i < j ? i : j;
  return card[i] < card[j] ? i : j;
}

struct Node {
  Node *L, *R;
  int fr, to, i, cnt;

  pii query ( int a, int b ) {
    if ( fr == a && to == b ) return mk(i,cnt);
    if ( b <= mid ) return L->query(a,b);
    if ( a > mid ) return R->query(a,b);
    pii ansL = L->query(a,mid);
    pii ansR = R->query(mid+1,b);
    return mk ( mini(ansL.fst,ansR.fst), ansL.snd+ansR.snd );
  }

  void remove ( int pos ) {
    if ( fr == to ) {
      card[pos] = INT_MAX;
      cnt = 0;
      return;
    }
    if ( mid >= i ) L->remove(i);
    else R->remove(i);
    i = mini ( L->i, R->i );
    cnt = L->cnt + R->cnt;
  }

  Node ( int a, int b ) : fr(a), to(b) {
    cnt = to - fr + 1;
    if ( a == b )
      L = R = 0, i = a;
    else {
      L = new Node( fr, mid );
      R = new Node( mid+1, to );
      i = mini ( L->i, R->i );
    }
  }
};

int main() {
  int n;
  scanf ( "%d", &n );
  for(int i = 0; i < n; ++i ) scanf ( "%d", card+i );

  Node stree ( 0, n-1 );

  ll ans = 0;
  int pos = 0;
  for ( int i = 0; i < n; ++i ) {
    pii tmp = stree.query ( pos, n-1 );
    if ( card[tmp.fst] == INT_MAX ) {
      ans += tmp.snd;
      tmp = stree.query(0, pos);
      ans += stree.query(0, pos).snd;
    }
    else
      ans += stree.query(pos, tmp.fst).snd;
    stree.remove(pos = tmp.fst);
  }

  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
#define snd second
#define fst first
#define mk make_pair
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
#define TAM 100100
#define mid ((fr+to)>>1)

int card[TAM];
int mini ( int i, int j ) {
  if ( card[i] == card[j] )
    return i < j ? i : j;
  return card[i] < card[j] ? i : j;
}

struct Node {
  Node *L, *R;
  int fr, to, i, cnt;

  pii query ( int a, int b ) {
    if ( fr == a && to == b ) return mk(i,cnt);
    if ( b <= mid ) return L->query(a,b);
    if ( a > mid ) return R->query(a,b);
    pii ansL = L->query(a,mid);
    pii ansR = R->query(mid+1,b);
    return mk ( mini(ansL.fst,ansR.fst), ansL.snd+ansR.snd );
  }

  void remove ( int pos ) {
    if ( fr == to ) {
      card[pos] = INT_MAX;
      cnt = 0;
      return;
    }
    if ( mid >= i ) L->remove(i);
    else R->remove(i);
    i = mini ( L->i, R->i );
    cnt = L->cnt + R->cnt;
  }

  Node ( int a, int b ) : fr(a), to(b) {
    cnt = to - fr + 1;
    if ( a == b )
      L = R = 0, i = a;
    else {
      L = new Node( fr, mid );
      R = new Node( mid+1, to );
      i = mini ( L->i, R->i );
    }
  }
};

int main() {
  int n;
  scanf ( "%d", &n );
  for(int i = 0; i < n; ++i ) scanf ( "%d", card+i );

  Node stree ( 0, n-1 );

  ll ans = 0;
  int pos = 0;
  for ( int i = 0; i < n; ++i ) {
    pii tmp = stree.query ( pos, n-1 );
    if ( card[tmp.fst] == INT_MAX ) {
      ans += tmp.snd;
      tmp = stree.query(0, pos);
      ans += stree.query(0, tmp.fst).snd;
    }
    else
      ans += stree.query(pos, tmp.fst).snd;
    stree.remove(pos = tmp.fst);
  }

  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
#define snd second
#define fst first
#define mk make_pair
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
#define TAM 100100
#define mid ((fr+to)>>1)

int card[TAM];
int mini ( int i, int j ) {
  if ( card[i] == card[j] )
    return i < j ? i : j;
  return card[i] < card[j] ? i : j;
}

struct Node {
  Node *L, *R;
  int fr, to, i, cnt;

  pii query ( int a, int b ) {
    if ( fr == a && to == b ) return mk(i,cnt);
    if ( b <= mid ) return L->query(a,b);
    if ( a > mid ) return R->query(a,b);
    pii ansL = L->query(a,mid);
    pii ansR = R->query(mid+1,b);
    return mk ( mini(ansL.fst,ansR.fst), ansL.snd+ansR.snd );
  }

  void remove ( int pos ) {
    if ( fr == to ) {
      card[pos] = INT_MAX;
      cnt = 0;
      return;
    }
    if ( mid >= i ) L->remove(i);
    else R->remove(i);
    i = mini ( L->i, R->i );
    cnt = L->cnt + R->cnt;
  }

  Node ( int a, int b ) : fr(a), to(b) {
    cnt = to - fr + 1;
    if ( a == b )
      L = R = 0, i = a;
    else {
      L = new Node( fr, mid );
      R = new Node( mid+1, to );
      i = mini ( L->i, R->i );
    }
  }
};

int main() {
  int n;
  scanf ( "%d", &n );
  for(int i = 0; i < n; ++i ) scanf ( "%d", card+i );

  Node stree ( 0, n-1 );

  ll ans = 0;
  int pos = 0;
  for ( int i = 0; i < n; ++i ) {
    const int minVal = card[stree.query(0,n-1).fst];
    pii tmp = stree.query ( pos, n-1 );
    if ( card[tmp.fst] != minVal ) {
      ans += tmp.snd;
      tmp = stree.query(0, pos);
      ans += stree.query(0, tmp.fst).snd;
    }
    else
      ans += stree.query(pos, tmp.fst).snd;
    stree.remove(pos = tmp.fst);
  }

  cout << ans << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <queue>
#include <deque>
#include <iomanip>
#include <cmath>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>

#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
//#define int long long
#define ld long double
#define vi vector<int>
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define iii pair<ii,int>
#define il pair<ll,ll>
#define vv vector
#define endl '\n'

using namespace std;

const int MAXN = 1e6 + 5;
const int MAX_VER = 2e6;
const int INF = 1e9;
const int MAXT = 7e5;

struct FenwickTree {
    vector<int> bit;  // binary indexed tree
    int n;

    FenwickTree(int n) {
        this->n = n;
        bit.assign(n, 0);
    }

    FenwickTree(vector<int> a) : FenwickTree(a.size()) {
        for (size_t i = 0; i < a.size(); i++)
            add(i, a[i]);
    }

    int sum(int r) {
        int ret = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret += bit[r];
        return ret;
    }

    int sum(int l, int r) {
    	l = max(l,0);
        return sum(r) - sum(l - 1);
    }

    void add(int idx, int delta) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] += delta;
    }
};

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

	int n;
	cin >> n;
	int arr[n];
	FOR(i,n)cin >> arr[i];
	map<int,vi> mp;
	FOR(i,n)mp[arr[i]].pb(i);
	FenwickTree tree(n);

	ll sum = 0;
	int lst = 0;
	for(auto e: mp){
		int st = -1;
		FOR(i,e.ss.size()){
			if(st == -1 and e.ss[i] >= lst){
				st = i;
			}
			if(e.ss[i]>=lst){
				//cout << e.ss[i] - lst -tree.sum(lst,e.ss[i])<< endl;
				sum += e.ss[i] - lst - tree.sum(lst,e.ss[i]);
				tree.add(e.ss[i],1);
				lst = e.ss[i];
			}
		}
		//cout << st << endl;
		if(st == 0)continue;
		sum += n-lst - tree.sum(lst,n-1);
		sum += e.ss[0] - tree.sum(0,e.ss[0]);
		tree.add(e.ss[0],1);
		FORE(i,1,st-1){
			if(e.ss[i]>lst){
				sum += e.ss[i] - lst - tree.sum(lst,e.ss[i]);
				tree.add(e.ss[i],1);
				lst = e.ss[i];
			}
		}
	}
	cout << sum+n << endl;
	return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <bitset>
#include <cmath>
using namespace std;
#define all(M) (M).begin(), (M).end()
#define vi vector<int>
#define vl vector<ll>
#define sort(v) sort(all(v))
#define fo(i,m,n) for(auto i = m ; i < n ; i++)
#define rep(i,n) fo(i,0,n)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pqueue priority_queue<pll,vector<pll>, greater<pll>>
#define sz(s) s.size()
#define trace(a) {for(auto i:a) cout << i << ' '; cout << '\n';}
//#define set(a) memset(a,0,sizeof(a))
#define si(n) scanf("%d",&n)
#define pi(n) printf("%d\n",n)
#define sl(n) scanf("%lld",&n)
#define pl(n) printf("%lld\n",n)
#define smi(n,m) scanf("%d%d",&n,&m)
#define pmi(n,m) printf("%d %d\n",n,m)
#define sml(n,m) scanf("%lld%lld",&n,&m)
#define pml(n,m) printf("%lld %lld\n",n,m)
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int n;
vi a;

int main()
{
	si(n);
	a.resize(n);
	ll s=0, mx=0, l;
	rep(i,n) sl(l), s+=l, mx = max(mx, l);
	pi((int)(ceil(s/(n-1)) >= mx ? ceil(s/(double)(n-1)) : 0));
	return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <bitset>
#include <cmath>
using namespace std;
#define all(M) (M).begin(), (M).end()
#define vi vector<int>
#define vl vector<ll>
#define sort(v) sort(all(v))
#define fo(i,m,n) for(auto i = m ; i < n ; i++)
#define rep(i,n) fo(i,0,n)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pqueue priority_queue<pll,vector<pll>, greater<pll>>
#define sz(s) s.size()
#define trace(a) {for(auto i:a) cout << i << ' '; cout << '\n';}
//#define set(a) memset(a,0,sizeof(a))
#define si(n) scanf("%d",&n)
#define pi(n) printf("%d\n",n)
#define sl(n) scanf("%lld",&n)
#define pl(n) printf("%lld\n",n)
#define smi(n,m) scanf("%d%d",&n,&m)
#define pmi(n,m) printf("%d %d\n",n,m)
#define sml(n,m) scanf("%lld%lld",&n,&m)
#define pml(n,m) printf("%lld %lld\n",n,m)
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int n;
vi a;

bool check(int r)
{
	if(r <= a[n-1]) return false;
	return (a[n-1] <= (r/n)*(n-1)+(r%n));
}

int main()
{
	si(n);
	a.resize(n);
	rep(i,n) si(a[i]);
	sort(a);
	int l=1,r=int(1e9+7);
	while(l < r)
	{
		int m = (l+r)/2;
		if(check(m)) r = m;
		else l = m+1;
	}
	pi(l);
	return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <bitset>
#include <cmath>
using namespace std;
#define all(M) (M).begin(), (M).end()
#define vi vector<int>
#define vl vector<ll>
#define sort(v) sort(all(v))
#define fo(i,m,n) for(auto i = m ; i < n ; i++)
#define rep(i,n) fo(i,0,n)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pqueue priority_queue<pll,vector<pll>, greater<pll>>
#define sz(s) s.size()
#define trace(a) {for(auto i:a) cout << i << ' '; cout << '\n';}
//#define set(a) memset(a,0,sizeof(a))
#define si(n) scanf("%d",&n)
#define pi(n) printf("%d\n",n)
#define sl(n) scanf("%lld",&n)
#define pl(n) printf("%lld\n",n)
#define smi(n,m) scanf("%d%d",&n,&m)
#define pmi(n,m) printf("%d %d\n",n,m)
#define sml(n,m) scanf("%lld%lld",&n,&m)
#define pml(n,m) printf("%lld %lld\n",n,m)
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int n;
vi a;
ll mx = 0, s = 0;

bool check(int k)
{
	return (ceil(s/(double)(n-1)) <= k && mx <= k);
}

int main()
{
	si(n);
	int l;
	a.resize(n);
	rep(i,n) si(l), mx = max((int)mx,l), s += l;
	int lo=0, h=1e9+7;
	while(lo < h)
	{
		int m = (lo+h)>>1;
		if(check(m))
			h = m;
		else lo = m+1;
	}
	pi(lo);
	return 0;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <bitset>
#include <cmath>
using namespace std;
#define all(M) (M).begin(), (M).end()
#define vi vector<int>
#define vl vector<ll>
#define sort(v) sort(all(v))
#define fo(i,m,n) for(auto i = m ; i < n ; i++)
#define rep(i,n) fo(i,0,n)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pqueue priority_queue<pll,vector<pll>, greater<pll>>
#define sz(s) s.size()
#define trace(a) {for(auto i:a) cout << i << ' '; cout << '\n';}
//#define set(a) memset(a,0,sizeof(a))
#define si(n) scanf("%d",&n)
#define pi(n) printf("%d\n",n)
#define sl(n) scanf("%lld",&n)
#define pl(n) printf("%lld\n",n)
#define smi(n,m) scanf("%d%d",&n,&m)
#define pmi(n,m) printf("%d %d\n",n,m)
#define sml(n,m) scanf("%lld%lld",&n,&m)
#define pml(n,m) printf("%lld %lld\n",n,m)
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int n;
vi a;
ll mx = 0, s = 0;

bool check(int k)
{
	return (ceil(s/(double)(n-1)) <= k && mx <= k);
}

int main()
{
	si(n);
	int l;
	a.resize(n);
	rep(i,n) si(l), mx = max((int)mx,l), s += l;
	ll lo=0, h=1e14;
	while(lo < h)
	{
		int m = (lo+h)>>1;
		if(check(m))
			h = m;
		else lo = m+1;
	}
	pl(lo);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{ios::sync_with_stdio(false);
 vector<int> v;
int n;
cin>>n;
for (int i=0;i<n;i++)
{
    int o; cin>>o;
    v.push_back(o);
}
sort(v.begin(),v.end());
int  S=v[0];
int m=0;
for (int i=0;i<v.size();i++)if (v[i]==S)m++;
int res=0;
if (m>v[0]){if (m==n) res=ceil(v[0]+1.0*v[0]/(m-1)); else res=v[v.size()-1];}
else{
while (m<=v[0] & m!=n & v[0]>0)
{
    for (int i=m;i<n;i++){v[i]-=m;}
    res+=m;
    v[0]-=m;
    v[0]++;
    for (int j=m;j<n;j++){if (v[j]==v[0])m++;}

}

if (m==n){res+=ceil(v[0]+1.0*v[0]/(m-1));}
else res+=v[v.size()-1];

}
cout<<res;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{ios::sync_with_stdio(false);
 vector<int> v;
int n;
cin>>n;
for (int i=0;i<n;i++)
{
    int o; cin>>o;
    v.push_back(o);
}
sort(v.begin(),v.end());
int  S=v[0];
int m=0;
for (int i=0;i<v.size();i++)if (v[i]==S)m++;
int res=0;
if (m>v[0]){if (m==n) res=ceil(v[v.size()-1]*1.0/(m-1)); else res=v[v.size()-1];}
else{
while (m<=v[0] & m!=n)
{
    for (int i=m;i<n;i++){v[i]-=m;}
    res+=m;
    v[0]-=m;
    v[0]++;
    for (int j=m;j<n;j++){if (v[j]==v[0])m++;}

}

if (m==n){res+=ceil(v[0]+1.0*v[0]/(m-1));}
else res+=v[v.size()-1];

}
cout<<res;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{ios::sync_with_stdio(false);
 vector<int> v;
int n;
cin>>n;
for (int i=0;i<n;i++)
{
    int o; cin>>o;
    v.push_back(o);
}
sort(v.begin(),v.end());
int  S=v[0];
int m=0;
for (int i=0;i<v.size();i++)if (v[i]==S)m++;
int res=0;
if (m>v[0]){if (m==n) res=ceil(v[0]+1.0*v[0]/(m-1)); else res=v[v.size()-1];}
else{
while (m<=v[0] & m!=n)
{
    for (int i=m;i<n;i++){v[i]-=m;}
    res+=m;
    v[0]-=m;
    v[0]++;
    for (int j=m;j<n;j++){if (v[j]==v[0])m++;}

}

if (m==n){res+=ceil(v[0]+1.0*v[0]/(m-1));}
else res+=v[v.size()-1];

}
cout<<res;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{ios::sync_with_stdio(false);
 vector<int> v;
int n;
cin>>n;
for (int i=0;i<n;i++)
{
    int o; cin>>o;
    v.push_back(o);
}
sort(v.begin(),v.end());
int  S=v[0];
int m=0;
for (int i=0;i<v.size();i++)if (v[i]==S)m++;
int res=0;
if (m>v[0])res=v[v.size()-1]+1;
else{
while (m>v[0])
{
    for (int i=m;i<n;i++){v[i]-=m;res+=m;}
    v[0]-=m-1;
    //cout<<v[0]<<" "<<m<<" "<<res<<endl;
    for (int i=m;i<n;i++){if (v[i]==v[0])m++;}

}
res+=v[v.size()-1];
res+=1;
}
cout<<res;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{ios::sync_with_stdio(false);
 vector<long long > v;
int n;
cin>>n;
for (int i=0;i<n;i++)
{
    long long  o; cin>>o;
    v.push_back(o);
}
sort(v.begin(),v.end());
long long  m=0;
for (int i=0;i<v.size();i++)m+=v[i];
cout<<max((long long) (ceil(m*1.0/(n-1))),v[v.size()-1]);

}


#include <bits/stdc++.h>

using namespace std;

int main()
{ios::sync_with_stdio(false);
 vector<int> v;
int n;
cin>>n;
for (int i=0;i<n;i++)
{
    int o; cin>>o;
    v.push_back(o);
}
sort(v.begin(),v.end());
int  S=v[0];
int m=0;
for (int i=0;i<v.size();i++)m+=v[i];
cout<<ceil(m*1.0/(n-1));

}


#include <bits/stdc++.h>

using namespace std;

int main()
{ios::sync_with_stdio(false);
 vector<int> v;
int n;
cin>>n;
for (int i=0;i<n;i++)
{
    int o; cin>>o;
    v.push_back(o);
}
sort(v.begin(),v.end());
int  S=v[0];
int m=0;
for (int i=0;i<v.size();i++)if (v[i]==S)m++;
int res=0;
if (m>v[0]){if (m==n) res=v[v.size()-1]+1; else res=v[v.size()-1];}
else{
while (m<=v[0])
{
    for (int i=m;i<n;i++){v[i]-=m;}
    res+=m;
    v[0]-=m;
    v[0]++;
    for (int j=m;j<n;j++){if (v[j]==v[0])m++;}

}

if (m==n){res+=v[v.size()-1];res+=1;}
else res+=v[v.size()-1];

}
cout<<res;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{ios::sync_with_stdio(false);
 vector<int> v;
int n;
cin>>n;
for (int i=0;i<n;i++)
{
    int o; cin>>o;
    v.push_back(o);
}
sort(v.begin(),v.end());
int  S=v[0];
int m=0;
for (int i=0;i<v.size();i++)if (v[i]==S)m++;
int res=0;
if (m>v[0])res=v[v.size()-1]+1;
else{
while (m>v[0])
{
    for (int i=m;i<n;i++){v[i]-=m;res+=m;}
    v[0]-=m-1;
    //cout<<v[0]<<" "<<m<<" "<<res<<endl;
    for (int i=m;i<n;i++){if (v[i]==v[0])m++;}

}
res+=v[v.size()-1];

}
cout<<res;
}


#include<bits/stdc++.h>
#define int long long
#define rep(i,x,n) for(int i=x;i<n;i++)
#define reprev(i,n,x) for(int i=n;i>=x;i--)
#define mp(a,b) make_pair(a,b)
#define mod 998244353
#define PI 3.141592653589793
#define pb push_back
#define vi vector<int>
#define pi pair<int,int>
#define INFINITY1 1000000009
#pragma GCC target ("sse4.2")
using namespace std;

void grayserge()
{
 int n;
 cin>>n;
 int a[n];
 int sum=0;
 for(int i=0;i<n;i++){
  cin>>a[i];
  sum=sum+a[i];
 }
 sort(a,a+n);
 cout<<floor(sum/(n-1))+1;
}

signed main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    // cin>>t;
   while(t--){
    grayserge();
   }
  return 0;
}


#include<bits/stdc++.h>
#define int long long
#define rep(i,x,n) for(int i=x;i<n;i++)
#define reprev(i,n,x) for(int i=n;i>=x;i--)
#define mp(a,b) make_pair(a,b)
#define mod 998244353
#define PI 3.141592653589793
#define pb push_back
#define vi vector<int>
#define pi pair<int,int>
#define INFINITY1 1000000009
#pragma GCC target ("sse4.2")
using namespace std;

void grayserge()
{
 int n;
 cin>>n;
 int a[n];
 int sum=0;
 int mx=-1;
 for(int i=0;i<n;i++){
  cin>>a[i];
  sum=sum+a[i];
  mx=max(mx,a[i]);
 }
 int mx1=((sum-1)/(n-1))+1;
 cout<<max(mx1,mx);
}

signed main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    // cin>>t;
   while(t--){
    grayserge();
   }
  return 0;
}


#include<bits/stdc++.h>
#define int long long
#define rep(i,x,n) for(int i=x;i<n;i++)
#define reprev(i,n,x) for(int i=n;i>=x;i--)
#define mp(a,b) make_pair(a,b)
#define mod 998244353
#define PI 3.141592653589793
#define pb push_back
#define vi vector<int>
#define pi pair<int,int>
#define INFINITY1 1000000009
#pragma GCC target ("sse4.2")
using namespace std;

void grayserge()
{
 int n;
 cin>>n;
 int a[n];
 int sum=0;
 int mx=-1;
 for(int i=0;i<n;i++){
  cin>>a[i];
  sum=sum+a[i];
  mx=max(mx,a[i]);
 }
 int mx1= long(ceil(float(float(sum)/float(n-1))));
 cout<<max(mx1,mx);
}

signed main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    // cin>>t;
   while(t--){
    grayserge();
   }
  return 0;
}


#include<bits/stdc++.h>
#define int long long
#define rep(i,x,n) for(int i=x;i<n;i++)
#define reprev(i,n,x) for(int i=n;i>=x;i--)
#define mp(a,b) make_pair(a,b)
#define mod 998244353
#define PI 3.141592653589793
#define pb push_back
#define vi vector<int>
#define pi pair<int,int>
#define INFINITY1 1000000009
#pragma GCC target ("sse4.2")
using namespace std;

void grayserge()
{
 int n;
 cin>>n;
 int a[n];
 int sum=0;
 int mx=-1;
 for(int i=0;i<n;i++){
  cin>>a[i];
  sum=sum+a[i];
  mx=max(mx,a[i]);
 }
 int mx1=(ceil(float(float(sum)/float(n-1))));
 cout<<max(mx1,mx);
}

signed main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    // cin>>t;
   while(t--){
    grayserge();
   }
  return 0;
}


#include<bits/stdc++.h>

#define rep(i,x,n) for(int i=x;i<n;i++)
#define reprev(i,n,x) for(int i=n;i>=x;i--)
#define mp(a,b) make_pair(a,b)
#define mod 998244353
#define PI 3.141592653589793
#define pb push_back
#define vi vector<int>
#define pi pair<int,int>
#define INFINITY1 1000000009
#pragma GCC target ("sse4.2")
using namespace std;

void grayserge()
{
 int n;
 cin>>n;
 int a[n];
 int sum=0;
 int mx=-1;
 for(int i=0;i<n;i++){
  cin>>a[i];
  sum=sum+a[i];
  mx=max(mx,a[i]);
 }
 cout<<max(int(ceil(float(float(sum)/float(n-1)))),mx);
}

signed main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    // cin>>t;
   while(t--){
    grayserge();
   }
  return 0;
}


#include<bits/stdc++.h>

#define rep(i,x,n) for(int i=x;i<n;i++)
#define reprev(i,n,x) for(int i=n;i>=x;i--)
#define mp(a,b) make_pair(a,b)
#define mod 998244353
#define PI 3.141592653589793
#define pb push_back
#define vi vector<int>
#define pi pair<int,int>
#define INFINITY1 1000000009
#pragma GCC target ("sse4.2")
using namespace std;

void grayserge()
{
 int n;
 cin>>n;
 int a[n];
 int sum=0;
 int mx=-1;
 for(int i=0;i<n;i++){
  cin>>a[i];
  sum=sum+a[i];
  mx=max(mx,a[i]);
 }
 sort(a,a+n);
 cout<<max(int(ceil(sum/(n-1))),mx);
}

signed main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    // cin>>t;
   while(t--){
    grayserge();
   }
  return 0;
}


#include<iostream>
#include<cmath>
#include<list>
#include<climits>
#include<cstring>
#include<set>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<numeric>
using namespace std;
#define etm cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll  long long int
#define ull unsigned long long int
#define doub long double
#define mp make_pair
#define pb push_back
#define pii pair<int , int>
#define pdd pair<double , double>
#define pll pair<long long int,long long int>
#define vpl vector<pll >
#define vll vector<ll >
#define vi vector<int >
#define mi map<int, int>
#define mull map<ull, ull>
#define stp setprecision(20)
#define N 100005
#define rep(i,a,b,c)            for(ll i=(a);i<=(b);i+=(c))
#define repb(i,a,b,c)           for(ll i=(a);i>=(b);i-=(c))
#define MOD                     998244353
#define ld                      long double
#define inf                     1e18
#define mp                      make_pair
#define vpll                    vector<pair<ll,ll>>
#define vvpll                   vector<vector<pair<ll,ll>>>
#define vvll                    vector<vector<ll>>
#define all(x)                  x.begin(),x.end()
#define fi                      first
#define se                      second
#define test                    ll T;cin>>T;while(T--)
#define isvowel(a)              (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
#define show(w,size)            for(ll i=0;i<size;i++) cout<<w[i]<<" ";
#define print(a)                cout<<a<<"\n";
#define pqll                    priority_queue<ll>
#define mset(dp,no)             memset(dp,no,sizeof(dp))
#define umll                    unordered_map<ll,ll>
#define mll                     map<ll,ll>
#define input(a,n)              ll I;rep(I,0,n-1,1) cin>>a[I];
#define countbit                __builtin_popcount // Number of set bits .
#define fbo(k)                  find_by_order // K-th element in a set (counting from zero) .
#define ook(k)                  order_of_key //  Number of items strictly smaller than k .
#define lb                      lower_bound
#define up                      upper_bound
#define in                      insert
//#define db(x) cout <<#x<<": "<<x<<'\n';
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
ll gcd(ll a,ll b){if(a<b)return gcd(b,a);else if(b==0)return a;else return gcd(b,a%b);}
ll isPrime(ll n){ll p=(ll)sqrt(n);rep(i,2,p,1) if(n%i==0) return 0;return 1;}// reuturn 1 if prime
ll pow(ll b,ll e){if(e==0)return 1;else if(e%2==0){ll a=pow(b,e/2);return a*a;}else {ll a=pow(b,e/2);return b*a*a;}}
ll powm(ll x,ll y,ll m=MOD){x=x%m;ll res=1;while(y){if(y&1)res=res*x;res%=m;y=y>>1;x=x*x;x%=m;}return res;}
ll ceil(long double a){ll b=a;if(a==b){return b;}else{return b+1;}}
ll floor(long double a){ll b=a;return b;}
ll lcm(ll a,ll b){ return (a*b)/gcd(a,b);}
ll modInverse(ll a, ll m) { return powm(a, m-2, m);}
bool issq(ll n){ ll p=sqrt(n);return p*p==n;}
vll prime;// if i==prime[i] then prime otherwise smallest prime factor of that number
void sieve(ll n)
{
    prime.resize(n+1,1);
    prime[0] = 0, prime[1] = 0;
    for (ll i = 2; i*i <= n; i++)
        if (prime[i])
            for (ll j = i * i; j <= n; j += i)
                prime[j] = 0;
}
ll extended_GCD(ll a ,ll b ,ll &x ,ll &y){
    if(a == 0){
        x = 0;
        y = 1;
        return b;
    }
    ll x1 , y1;
    ll gcd = extended_GCD(b%a , a , x1 , y1);
    x = y1 - (b/a)*x1;
    y = x1;
    return gcd;
}
    ll mulInv(ll a , ll mod=26){
    ll x , y;
    extended_GCD(a , mod , x , y);
    if(x < 0) x += mod;
    return x;
}
ll find(ll num[], ll rem[], ll k,ll prod)
{
    // Compute product of all numbers
    ll result = 0;
    // Apply above formula
    for (ll i = 0; i < k; i++)
    {
        ll pp = prod / num[i];
        result += rem[i] * mulInv(pp, num[i]) * pp;
    }
    return result % prod;
}
ll nCr(ll n,ll k)
{
  ll res = 1;

      // Since C(n, k) = C(n, n-k)
      if ( k > n - k )
          k = n - k;

      // Calculate value of
      // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
      for (ll i = 0; i < k; ++i)
      {
          res *= (n - i);
          res /= (i + 1);
      }

      return res;
}
class DSU
{
 public:vll parent,size;
public: DSU(ll n)
{

        parent.resize(n+1);size.resize(n+1);
        for(ll i=1;i<=n;i++)
            {
            parent[i]=i;
            size[i]=1;
          }
    }
public:ll find_set(ll x)
    {
      if(parent[x]==x)
      {
        return x;
      }
      return parent[x]=find_set(parent[x]);
    }
public:void union_set(ll x,ll y)
    {
        x=find_set(x);y=find_set(y);
        if(x!=y)
        {
          parent[y]=x;
        }
}
};
bool cmp(pair<ll,ll>&p1,pair<ll,ll>&p2)
{
  if(p1.fi==p2.fi)
  {
    return p1.se>p2.se;
  }
  return p1.fi>p2.fi;
}
bool isPalindrome(string s)
{
  ll i,j;
  for(i=0,j=s.length()-1;i<=j;i++,j--)
  {
    if(s[i]!=s[j])
    {
      return 0;
    }
  }
return 1;
}
ll ToInt(char ch)
{
  return ch-'0';
}
char ToChar(ll a)
{
  return a+'0';
}
bool isSubSeq(string str1, string str2, ll m, ll n)
{
    // Base Cases
    if (m == 0) return true;
    if (n == 0) return false;
    // If last characters of two strings are matching
    if (str1[m-1] == str2[n-1])
        return isSubSeq(str1, str2, m-1, n-1);
    // If last characters are not matching
    return isSubSeq(str1, str2, m, n-1);
}
void printVectorPair(vpll v)
{
  for(ll i=0;i<v.size();i++)
  {
    cout<<v[i].fi<<" "<<v[i].se<<"\n";
  }
}
void modBigNumber(string num, ll m)
{
    // Store the modulus of big number
    vector<int> vec;
    ll mod = 0;
    // Do step by step division
    for (int i = 0; i < num.size(); i++) {
        int digit = num[i] - '0';
        mod = mod * 10 + digit;
        int quo = mod / m;
        if((vec.size()!=0)||(quo!=0))        //to remove initiale zeros
        vec.push_back(quo);
        mod = mod % m;
    }
   // cout << "\nRemainder : " << mod << "\n";
   // cout << "Quotient : ";rep(i,0,vec.size()-1,1)cout<<vec[i];cout<<"\n";
    return;
}
struct SegmentTree
{
  ll n;
  vll v;
  SegmentTree(ll size)
  {
    n=4*size+1;
    v.resize(n,0);
  }
  void build(ll ar[],ll ipos,ll fpos,ll pos=1)
  {
    if(ipos>fpos)
    return ;
    if(ipos==fpos)
    v[pos]=ar[ipos];
    else
    {
    ll mid=(ipos+fpos)/2;
    build(ar,ipos,mid,pos*2);
    build(ar,mid+1,fpos,pos*2+1);
    v[pos]=v[pos*2]+v[pos*2+1];
    }
  }
  void update(ll index,ll val,ll ipos,ll fpos,ll pos=1)
  {
    if(ipos>fpos)
    return ;
    if(ipos==fpos)
    v[pos]+=val;
    else
    {
      v[pos]+=val;
      ll mid=(ipos+fpos)/2;
      if(mid>=index)
      update(index,val,ipos,mid,pos*2);
      else
      update(index,val,mid+1,fpos,pos*2+1);
    }
  }
  ll get_sum(ll l,ll r,ll ipos,ll fpos,ll pos=1)
  {
    if(ipos>fpos)
    return 0;
    if(l>r)
    return 0;
    ll mid=(ipos+fpos)/2;
    if((l==ipos)&&(r==fpos))
    return v[pos];
    else return get_sum(l,min(mid,r),ipos,mid,pos*2)+get_sum(max(mid+1,l),r,mid+1,fpos,pos*2+1);
  }
};
struct BIT
{
vector<ll>bitree;ll n;
BIT(ll n)
{
this->n=n;
bitree.resize(n+1,0);
}
void update(ll idx,ll val)
{
  idx++;
  while(idx<=n)
  {
    bitree[idx]+=val;
    idx+=idx&(-idx);
  }
}
ll Sum(ll idx)
{
  ll sum=0;
  idx++;
  while(idx>0)
  {
    sum+=bitree[idx];
    idx-=idx&(-idx);
  }
  return sum;
}
};
ll sumofdigits(ll a)
{
  ll val=0;
  while(a>0)
  {
    val+=a%10;
    a/=10;
  }
  return val;
}

ll noOfDig(ll n){
  ll cnt=0;
  while(n){
    cnt++;n/=10;
  }
  return cnt;
}

vll arr;
ll n;
bool check(ll x){
  ll ans=0;
  rep(i, 0, n-1, 1){
    if(x<arr[i])return false;
    ans+=max(0ll, x-arr[i]);
  }
  if(ans>=x)return true;
  return false;
}

int main(){
  fastio;
  cin>>n;arr.resize(n);
  rep(i, 0, n-1, 1)cin>>arr[i];
  ll l=1, r=(ll)1e9+5;
  ll mid=0;
  while(l<r){
    // cout<<mid<<endl;
    mid=l+(r-l)/2;
    if(check(mid) and (mid==1 or !check(mid-1))) break;
    else if(check(mid))r=mid;
    else l=mid;
  }
  cout<<mid<<endl;
  return 0;
}


#include<iostream>
#include<cmath>
#include<list>
#include<climits>
#include<cstring>
#include<set>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<numeric>
using namespace std;
#define etm cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll  long long int
#define ull unsigned long long int
#define doub long double
#define mp make_pair
#define pb push_back
#define pii pair<int , int>
#define pdd pair<double , double>
#define pll pair<long long int,long long int>
#define vpl vector<pll >
#define vll vector<ll >
#define vi vector<int >
#define mi map<int, int>
#define mull map<ull, ull>
#define stp setprecision(20)
#define N 100005
#define rep(i,a,b,c)            for(ll i=(a);i<=(b);i+=(c))
#define repb(i,a,b,c)           for(ll i=(a);i>=(b);i-=(c))
#define MOD                     998244353
#define ld                      long double
#define inf                     1e18
#define mp                      make_pair
#define vpll                    vector<pair<ll,ll>>
#define vvpll                   vector<vector<pair<ll,ll>>>
#define vvll                    vector<vector<ll>>
#define all(x)                  x.begin(),x.end()
#define fi                      first
#define se                      second
#define test                    ll T;cin>>T;while(T--)
#define isvowel(a)              (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
#define show(w,size)            for(ll i=0;i<size;i++) cout<<w[i]<<" ";
#define print(a)                cout<<a<<"\n";
#define pqll                    priority_queue<ll>
#define mset(dp,no)             memset(dp,no,sizeof(dp))
#define umll                    unordered_map<ll,ll>
#define mll                     map<ll,ll>
#define input(a,n)              ll I;rep(I,0,n-1,1) cin>>a[I];
#define countbit                __builtin_popcount // Number of set bits .
#define fbo(k)                  find_by_order // K-th element in a set (counting from zero) .
#define ook(k)                  order_of_key //  Number of items strictly smaller than k .
#define lb                      lower_bound
#define up                      upper_bound
#define in                      insert
//#define db(x) cout <<#x<<": "<<x<<'\n';
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
ll gcd(ll a,ll b){if(a<b)return gcd(b,a);else if(b==0)return a;else return gcd(b,a%b);}
ll isPrime(ll n){ll p=(ll)sqrt(n);rep(i,2,p,1) if(n%i==0) return 0;return 1;}// reuturn 1 if prime
ll pow(ll b,ll e){if(e==0)return 1;else if(e%2==0){ll a=pow(b,e/2);return a*a;}else {ll a=pow(b,e/2);return b*a*a;}}
ll powm(ll x,ll y,ll m=MOD){x=x%m;ll res=1;while(y){if(y&1)res=res*x;res%=m;y=y>>1;x=x*x;x%=m;}return res;}
ll ceil(long double a){ll b=a;if(a==b){return b;}else{return b+1;}}
ll floor(long double a){ll b=a;return b;}
ll lcm(ll a,ll b){ return (a*b)/gcd(a,b);}
ll modInverse(ll a, ll m) { return powm(a, m-2, m);}
bool issq(ll n){ ll p=sqrt(n);return p*p==n;}
vll prime;// if i==prime[i] then prime otherwise smallest prime factor of that number
void sieve(ll n)
{
    prime.resize(n+1,1);
    prime[0] = 0, prime[1] = 0;
    for (ll i = 2; i*i <= n; i++)
        if (prime[i])
            for (ll j = i * i; j <= n; j += i)
                prime[j] = 0;
}
ll extended_GCD(ll a ,ll b ,ll &x ,ll &y){
    if(a == 0){
        x = 0;
        y = 1;
        return b;
    }
    ll x1 , y1;
    ll gcd = extended_GCD(b%a , a , x1 , y1);
    x = y1 - (b/a)*x1;
    y = x1;
    return gcd;
}
    ll mulInv(ll a , ll mod=26){
    ll x , y;
    extended_GCD(a , mod , x , y);
    if(x < 0) x += mod;
    return x;
}
ll find(ll num[], ll rem[], ll k,ll prod)
{
    // Compute product of all numbers
    ll result = 0;
    // Apply above formula
    for (ll i = 0; i < k; i++)
    {
        ll pp = prod / num[i];
        result += rem[i] * mulInv(pp, num[i]) * pp;
    }
    return result % prod;
}
ll nCr(ll n,ll k)
{
  ll res = 1;

      // Since C(n, k) = C(n, n-k)
      if ( k > n - k )
          k = n - k;

      // Calculate value of
      // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
      for (ll i = 0; i < k; ++i)
      {
          res *= (n - i);
          res /= (i + 1);
      }

      return res;
}
class DSU
{
 public:vll parent,size;
public: DSU(ll n)
{

        parent.resize(n+1);size.resize(n+1);
        for(ll i=1;i<=n;i++)
            {
            parent[i]=i;
            size[i]=1;
          }
    }
public:ll find_set(ll x)
    {
      if(parent[x]==x)
      {
        return x;
      }
      return parent[x]=find_set(parent[x]);
    }
public:void union_set(ll x,ll y)
    {
        x=find_set(x);y=find_set(y);
        if(x!=y)
        {
          parent[y]=x;
        }
}
};
bool cmp(pair<ll,ll>&p1,pair<ll,ll>&p2)
{
  if(p1.fi==p2.fi)
  {
    return p1.se>p2.se;
  }
  return p1.fi>p2.fi;
}
bool isPalindrome(string s)
{
  ll i,j;
  for(i=0,j=s.length()-1;i<=j;i++,j--)
  {
    if(s[i]!=s[j])
    {
      return 0;
    }
  }
return 1;
}
ll ToInt(char ch)
{
  return ch-'0';
}
char ToChar(ll a)
{
  return a+'0';
}
bool isSubSeq(string str1, string str2, ll m, ll n)
{
    // Base Cases
    if (m == 0) return true;
    if (n == 0) return false;
    // If last characters of two strings are matching
    if (str1[m-1] == str2[n-1])
        return isSubSeq(str1, str2, m-1, n-1);
    // If last characters are not matching
    return isSubSeq(str1, str2, m, n-1);
}
void printVectorPair(vpll v)
{
  for(ll i=0;i<v.size();i++)
  {
    cout<<v[i].fi<<" "<<v[i].se<<"\n";
  }
}
void modBigNumber(string num, ll m)
{
    // Store the modulus of big number
    vector<int> vec;
    ll mod = 0;
    // Do step by step division
    for (int i = 0; i < num.size(); i++) {
        int digit = num[i] - '0';
        mod = mod * 10 + digit;
        int quo = mod / m;
        if((vec.size()!=0)||(quo!=0))        //to remove initiale zeros
        vec.push_back(quo);
        mod = mod % m;
    }
   // cout << "\nRemainder : " << mod << "\n";
   // cout << "Quotient : ";rep(i,0,vec.size()-1,1)cout<<vec[i];cout<<"\n";
    return;
}
struct SegmentTree
{
  ll n;
  vll v;
  SegmentTree(ll size)
  {
    n=4*size+1;
    v.resize(n,0);
  }
  void build(ll ar[],ll ipos,ll fpos,ll pos=1)
  {
    if(ipos>fpos)
    return ;
    if(ipos==fpos)
    v[pos]=ar[ipos];
    else
    {
    ll mid=(ipos+fpos)/2;
    build(ar,ipos,mid,pos*2);
    build(ar,mid+1,fpos,pos*2+1);
    v[pos]=v[pos*2]+v[pos*2+1];
    }
  }
  void update(ll index,ll val,ll ipos,ll fpos,ll pos=1)
  {
    if(ipos>fpos)
    return ;
    if(ipos==fpos)
    v[pos]+=val;
    else
    {
      v[pos]+=val;
      ll mid=(ipos+fpos)/2;
      if(mid>=index)
      update(index,val,ipos,mid,pos*2);
      else
      update(index,val,mid+1,fpos,pos*2+1);
    }
  }
  ll get_sum(ll l,ll r,ll ipos,ll fpos,ll pos=1)
  {
    if(ipos>fpos)
    return 0;
    if(l>r)
    return 0;
    ll mid=(ipos+fpos)/2;
    if((l==ipos)&&(r==fpos))
    return v[pos];
    else return get_sum(l,min(mid,r),ipos,mid,pos*2)+get_sum(max(mid+1,l),r,mid+1,fpos,pos*2+1);
  }
};
struct BIT
{
vector<ll>bitree;ll n;
BIT(ll n)
{
this->n=n;
bitree.resize(n+1,0);
}
void update(ll idx,ll val)
{
  idx++;
  while(idx<=n)
  {
    bitree[idx]+=val;
    idx+=idx&(-idx);
  }
}
ll Sum(ll idx)
{
  ll sum=0;
  idx++;
  while(idx>0)
  {
    sum+=bitree[idx];
    idx-=idx&(-idx);
  }
  return sum;
}
};
ll sumofdigits(ll a)
{
  ll val=0;
  while(a>0)
  {
    val+=a%10;
    a/=10;
  }
  return val;
}

ll noOfDig(ll n){
  ll cnt=0;
  while(n){
    cnt++;n/=10;
  }
  return cnt;
}

vll arr;
ll n;
bool check(ll x){
  if(x<=0)return false;
  ll ans=0;
  rep(i, 0, n-1, 1){
    if(x<arr[i])return false;
    ans+=max(0ll, x-arr[i]);
  }
  if(ans>=x)return true;
  return false;
}

int main(){
  fastio;
  cin>>n;arr.resize(n);
  rep(i, 0, n-1, 1)cin>>arr[i];
  ll l=1, r=(ll)1e18;
  // ll mid=0;
  while(l<r){
    ll mid=l+(r-l)/2;
    // cout<<mid<<endl;
    if(check(mid) and !check(mid-1)){
      l=mid;
      break;
    }
    else if(check(mid))r=mid;
    else l=mid;
  }
  cout<<l<<endl;
  return 0;
}


#include<iostream>
#include<cmath>
#include<list>
#include<climits>
#include<cstring>
#include<set>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<numeric>
using namespace std;
#define etm cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll  long long int
#define ull unsigned long long int
#define doub long double
#define mp make_pair
#define pb push_back
#define pii pair<int , int>
#define pdd pair<double , double>
#define pll pair<long long int,long long int>
#define vpl vector<pll >
#define vll vector<ll >
#define vi vector<int >
#define mi map<int, int>
#define mull map<ull, ull>
#define stp setprecision(20)
#define N 100005
#define rep(i,a,b,c)            for(ll i=(a);i<=(b);i+=(c))
#define repb(i,a,b,c)           for(ll i=(a);i>=(b);i-=(c))
#define MOD                     998244353
#define ld                      long double
#define inf                     1e18
#define mp                      make_pair
#define vpll                    vector<pair<ll,ll>>
#define vvpll                   vector<vector<pair<ll,ll>>>
#define vvll                    vector<vector<ll>>
#define all(x)                  x.begin(),x.end()
#define fi                      first
#define se                      second
#define test                    ll T;cin>>T;while(T--)
#define isvowel(a)              (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
#define show(w,size)            for(ll i=0;i<size;i++) cout<<w[i]<<" ";
#define print(a)                cout<<a<<"\n";
#define pqll                    priority_queue<ll>
#define mset(dp,no)             memset(dp,no,sizeof(dp))
#define umll                    unordered_map<ll,ll>
#define mll                     map<ll,ll>
#define input(a,n)              ll I;rep(I,0,n-1,1) cin>>a[I];
#define countbit                __builtin_popcount // Number of set bits .
#define fbo(k)                  find_by_order // K-th element in a set (counting from zero) .
#define ook(k)                  order_of_key //  Number of items strictly smaller than k .
#define lb                      lower_bound
#define up                      upper_bound
#define in                      insert
//#define db(x) cout <<#x<<": "<<x<<'\n';
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
ll gcd(ll a,ll b){if(a<b)return gcd(b,a);else if(b==0)return a;else return gcd(b,a%b);}
ll isPrime(ll n){ll p=(ll)sqrt(n);rep(i,2,p,1) if(n%i==0) return 0;return 1;}// reuturn 1 if prime
ll pow(ll b,ll e){if(e==0)return 1;else if(e%2==0){ll a=pow(b,e/2);return a*a;}else {ll a=pow(b,e/2);return b*a*a;}}
ll powm(ll x,ll y,ll m=MOD){x=x%m;ll res=1;while(y){if(y&1)res=res*x;res%=m;y=y>>1;x=x*x;x%=m;}return res;}
ll ceil(long double a){ll b=a;if(a==b){return b;}else{return b+1;}}
ll floor(long double a){ll b=a;return b;}
ll lcm(ll a,ll b){ return (a*b)/gcd(a,b);}
ll modInverse(ll a, ll m) { return powm(a, m-2, m);}
bool issq(ll n){ ll p=sqrt(n);return p*p==n;}
vll prime;// if i==prime[i] then prime otherwise smallest prime factor of that number
void sieve(ll n)
{
    prime.resize(n+1,1);
    prime[0] = 0, prime[1] = 0;
    for (ll i = 2; i*i <= n; i++)
        if (prime[i])
            for (ll j = i * i; j <= n; j += i)
                prime[j] = 0;
}
ll extended_GCD(ll a ,ll b ,ll &x ,ll &y){
    if(a == 0){
        x = 0;
        y = 1;
        return b;
    }
    ll x1 , y1;
    ll gcd = extended_GCD(b%a , a , x1 , y1);
    x = y1 - (b/a)*x1;
    y = x1;
    return gcd;
}
    ll mulInv(ll a , ll mod=26){
    ll x , y;
    extended_GCD(a , mod , x , y);
    if(x < 0) x += mod;
    return x;
}
ll find(ll num[], ll rem[], ll k,ll prod)
{
    // Compute product of all numbers
    ll result = 0;
    // Apply above formula
    for (ll i = 0; i < k; i++)
    {
        ll pp = prod / num[i];
        result += rem[i] * mulInv(pp, num[i]) * pp;
    }
    return result % prod;
}
ll nCr(ll n,ll k)
{
  ll res = 1;

      // Since C(n, k) = C(n, n-k)
      if ( k > n - k )
          k = n - k;

      // Calculate value of
      // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
      for (ll i = 0; i < k; ++i)
      {
          res *= (n - i);
          res /= (i + 1);
      }

      return res;
}
class DSU
{
 public:vll parent,size;
public: DSU(ll n)
{

        parent.resize(n+1);size.resize(n+1);
        for(ll i=1;i<=n;i++)
            {
            parent[i]=i;
            size[i]=1;
          }
    }
public:ll find_set(ll x)
    {
      if(parent[x]==x)
      {
        return x;
      }
      return parent[x]=find_set(parent[x]);
    }
public:void union_set(ll x,ll y)
    {
        x=find_set(x);y=find_set(y);
        if(x!=y)
        {
          parent[y]=x;
        }
}
};
bool cmp(pair<ll,ll>&p1,pair<ll,ll>&p2)
{
  if(p1.fi==p2.fi)
  {
    return p1.se>p2.se;
  }
  return p1.fi>p2.fi;
}
bool isPalindrome(string s)
{
  ll i,j;
  for(i=0,j=s.length()-1;i<=j;i++,j--)
  {
    if(s[i]!=s[j])
    {
      return 0;
    }
  }
return 1;
}
ll ToInt(char ch)
{
  return ch-'0';
}
char ToChar(ll a)
{
  return a+'0';
}
bool isSubSeq(string str1, string str2, ll m, ll n)
{
    // Base Cases
    if (m == 0) return true;
    if (n == 0) return false;
    // If last characters of two strings are matching
    if (str1[m-1] == str2[n-1])
        return isSubSeq(str1, str2, m-1, n-1);
    // If last characters are not matching
    return isSubSeq(str1, str2, m, n-1);
}
void printVectorPair(vpll v)
{
  for(ll i=0;i<v.size();i++)
  {
    cout<<v[i].fi<<" "<<v[i].se<<"\n";
  }
}
void modBigNumber(string num, ll m)
{
    // Store the modulus of big number
    vector<int> vec;
    ll mod = 0;
    // Do step by step division
    for (int i = 0; i < num.size(); i++) {
        int digit = num[i] - '0';
        mod = mod * 10 + digit;
        int quo = mod / m;
        if((vec.size()!=0)||(quo!=0))        //to remove initiale zeros
        vec.push_back(quo);
        mod = mod % m;
    }
   // cout << "\nRemainder : " << mod << "\n";
   // cout << "Quotient : ";rep(i,0,vec.size()-1,1)cout<<vec[i];cout<<"\n";
    return;
}
struct SegmentTree
{
  ll n;
  vll v;
  SegmentTree(ll size)
  {
    n=4*size+1;
    v.resize(n,0);
  }
  void build(ll ar[],ll ipos,ll fpos,ll pos=1)
  {
    if(ipos>fpos)
    return ;
    if(ipos==fpos)
    v[pos]=ar[ipos];
    else
    {
    ll mid=(ipos+fpos)/2;
    build(ar,ipos,mid,pos*2);
    build(ar,mid+1,fpos,pos*2+1);
    v[pos]=v[pos*2]+v[pos*2+1];
    }
  }
  void update(ll index,ll val,ll ipos,ll fpos,ll pos=1)
  {
    if(ipos>fpos)
    return ;
    if(ipos==fpos)
    v[pos]+=val;
    else
    {
      v[pos]+=val;
      ll mid=(ipos+fpos)/2;
      if(mid>=index)
      update(index,val,ipos,mid,pos*2);
      else
      update(index,val,mid+1,fpos,pos*2+1);
    }
  }
  ll get_sum(ll l,ll r,ll ipos,ll fpos,ll pos=1)
  {
    if(ipos>fpos)
    return 0;
    if(l>r)
    return 0;
    ll mid=(ipos+fpos)/2;
    if((l==ipos)&&(r==fpos))
    return v[pos];
    else return get_sum(l,min(mid,r),ipos,mid,pos*2)+get_sum(max(mid+1,l),r,mid+1,fpos,pos*2+1);
  }
};
struct BIT
{
vector<ll>bitree;ll n;
BIT(ll n)
{
this->n=n;
bitree.resize(n+1,0);
}
void update(ll idx,ll val)
{
  idx++;
  while(idx<=n)
  {
    bitree[idx]+=val;
    idx+=idx&(-idx);
  }
}
ll Sum(ll idx)
{
  ll sum=0;
  idx++;
  while(idx>0)
  {
    sum+=bitree[idx];
    idx-=idx&(-idx);
  }
  return sum;
}
};
ll sumofdigits(ll a)
{
  ll val=0;
  while(a>0)
  {
    val+=a%10;
    a/=10;
  }
  return val;
}

ll noOfDig(ll n){
  ll cnt=0;
  while(n){
    cnt++;n/=10;
  }
  return cnt;
}

vll arr;
ll n;
bool check(ll x){
  ll ans=0;
  rep(i, 0, n-1, 1){
    if(x<arr[i])return false;
    ans+=max(0ll, x-arr[i]);
  }
  if(ans>=x)return true;
  return false;
}

int main(){
  fastio;
  cin>>n;arr.resize(n);
  rep(i, 0, n-1, 1)cin>>arr[i];
  ll l=1, r=(ll)1e13;
  ll mid=0;
  while(l<r){
    // cout<<mid<<endl;
    mid=l+(r-l)/2;
    if(check(mid) and (mid==1 or !check(mid-1))) break;
    else if(check(mid))r=mid;
    else l=mid;
  }
  cout<<mid<<endl;
  return 0;
}


#include<iostream>
#include<cmath>
#include<list>
#include<climits>
#include<cstring>
#include<set>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<numeric>
using namespace std;
#define etm cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll  long long int
#define ull unsigned long long int
#define doub long double
#define mp make_pair
#define pb push_back
#define pii pair<int , int>
#define pdd pair<double , double>
#define pll pair<long long int,long long int>
#define vpl vector<pll >
#define vll vector<ll >
#define vi vector<int >
#define mi map<int, int>
#define mull map<ull, ull>
#define stp setprecision(20)
#define N 100005
#define rep(i,a,b,c)            for(ll i=(a);i<=(b);i+=(c))
#define repb(i,a,b,c)           for(ll i=(a);i>=(b);i-=(c))
#define MOD                     998244353
#define ld                      long double
#define inf                     1e18
#define mp                      make_pair
#define vpll                    vector<pair<ll,ll>>
#define vvpll                   vector<vector<pair<ll,ll>>>
#define vvll                    vector<vector<ll>>
#define all(x)                  x.begin(),x.end()
#define fi                      first
#define se                      second
#define test                    ll T;cin>>T;while(T--)
#define isvowel(a)              (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
#define show(w,size)            for(ll i=0;i<size;i++) cout<<w[i]<<" ";
#define print(a)                cout<<a<<"\n";
#define pqll                    priority_queue<ll>
#define mset(dp,no)             memset(dp,no,sizeof(dp))
#define umll                    unordered_map<ll,ll>
#define mll                     map<ll,ll>
#define input(a,n)              ll I;rep(I,0,n-1,1) cin>>a[I];
#define countbit                __builtin_popcount // Number of set bits .
#define fbo(k)                  find_by_order // K-th element in a set (counting from zero) .
#define ook(k)                  order_of_key //  Number of items strictly smaller than k .
#define lb                      lower_bound
#define up                      upper_bound
#define in                      insert
//#define db(x) cout <<#x<<": "<<x<<'\n';
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
ll gcd(ll a,ll b){if(a<b)return gcd(b,a);else if(b==0)return a;else return gcd(b,a%b);}
ll isPrime(ll n){ll p=(ll)sqrt(n);rep(i,2,p,1) if(n%i==0) return 0;return 1;}// reuturn 1 if prime
ll pow(ll b,ll e){if(e==0)return 1;else if(e%2==0){ll a=pow(b,e/2);return a*a;}else {ll a=pow(b,e/2);return b*a*a;}}
ll powm(ll x,ll y,ll m=MOD){x=x%m;ll res=1;while(y){if(y&1)res=res*x;res%=m;y=y>>1;x=x*x;x%=m;}return res;}
ll ceil(long double a){ll b=a;if(a==b){return b;}else{return b+1;}}
ll floor(long double a){ll b=a;return b;}
ll lcm(ll a,ll b){ return (a*b)/gcd(a,b);}
ll modInverse(ll a, ll m) { return powm(a, m-2, m);}
bool issq(ll n){ ll p=sqrt(n);return p*p==n;}
vll prime;// if i==prime[i] then prime otherwise smallest prime factor of that number
void sieve(ll n)
{
    prime.resize(n+1,1);
    prime[0] = 0, prime[1] = 0;
    for (ll i = 2; i*i <= n; i++)
        if (prime[i])
            for (ll j = i * i; j <= n; j += i)
                prime[j] = 0;
}
ll extended_GCD(ll a ,ll b ,ll &x ,ll &y){
    if(a == 0){
        x = 0;
        y = 1;
        return b;
    }
    ll x1 , y1;
    ll gcd = extended_GCD(b%a , a , x1 , y1);
    x = y1 - (b/a)*x1;
    y = x1;
    return gcd;
}
    ll mulInv(ll a , ll mod=26){
    ll x , y;
    extended_GCD(a , mod , x , y);
    if(x < 0) x += mod;
    return x;
}
ll find(ll num[], ll rem[], ll k,ll prod)
{
    // Compute product of all numbers
    ll result = 0;
    // Apply above formula
    for (ll i = 0; i < k; i++)
    {
        ll pp = prod / num[i];
        result += rem[i] * mulInv(pp, num[i]) * pp;
    }
    return result % prod;
}
ll nCr(ll n,ll k)
{
  ll res = 1;

      // Since C(n, k) = C(n, n-k)
      if ( k > n - k )
          k = n - k;

      // Calculate value of
      // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
      for (ll i = 0; i < k; ++i)
      {
          res *= (n - i);
          res /= (i + 1);
      }

      return res;
}
class DSU
{
 public:vll parent,size;
public: DSU(ll n)
{

        parent.resize(n+1);size.resize(n+1);
        for(ll i=1;i<=n;i++)
            {
            parent[i]=i;
            size[i]=1;
          }
    }
public:ll find_set(ll x)
    {
      if(parent[x]==x)
      {
        return x;
      }
      return parent[x]=find_set(parent[x]);
    }
public:void union_set(ll x,ll y)
    {
        x=find_set(x);y=find_set(y);
        if(x!=y)
        {
          parent[y]=x;
        }
}
};
bool cmp(pair<ll,ll>&p1,pair<ll,ll>&p2)
{
  if(p1.fi==p2.fi)
  {
    return p1.se>p2.se;
  }
  return p1.fi>p2.fi;
}
bool isPalindrome(string s)
{
  ll i,j;
  for(i=0,j=s.length()-1;i<=j;i++,j--)
  {
    if(s[i]!=s[j])
    {
      return 0;
    }
  }
return 1;
}
ll ToInt(char ch)
{
  return ch-'0';
}
char ToChar(ll a)
{
  return a+'0';
}
bool isSubSeq(string str1, string str2, ll m, ll n)
{
    // Base Cases
    if (m == 0) return true;
    if (n == 0) return false;
    // If last characters of two strings are matching
    if (str1[m-1] == str2[n-1])
        return isSubSeq(str1, str2, m-1, n-1);
    // If last characters are not matching
    return isSubSeq(str1, str2, m, n-1);
}
void printVectorPair(vpll v)
{
  for(ll i=0;i<v.size();i++)
  {
    cout<<v[i].fi<<" "<<v[i].se<<"\n";
  }
}
void modBigNumber(string num, ll m)
{
    // Store the modulus of big number
    vector<int> vec;
    ll mod = 0;
    // Do step by step division
    for (int i = 0; i < num.size(); i++) {
        int digit = num[i] - '0';
        mod = mod * 10 + digit;
        int quo = mod / m;
        if((vec.size()!=0)||(quo!=0))        //to remove initiale zeros
        vec.push_back(quo);
        mod = mod % m;
    }
   // cout << "\nRemainder : " << mod << "\n";
   // cout << "Quotient : ";rep(i,0,vec.size()-1,1)cout<<vec[i];cout<<"\n";
    return;
}
struct SegmentTree
{
  ll n;
  vll v;
  SegmentTree(ll size)
  {
    n=4*size+1;
    v.resize(n,0);
  }
  void build(ll ar[],ll ipos,ll fpos,ll pos=1)
  {
    if(ipos>fpos)
    return ;
    if(ipos==fpos)
    v[pos]=ar[ipos];
    else
    {
    ll mid=(ipos+fpos)/2;
    build(ar,ipos,mid,pos*2);
    build(ar,mid+1,fpos,pos*2+1);
    v[pos]=v[pos*2]+v[pos*2+1];
    }
  }
  void update(ll index,ll val,ll ipos,ll fpos,ll pos=1)
  {
    if(ipos>fpos)
    return ;
    if(ipos==fpos)
    v[pos]+=val;
    else
    {
      v[pos]+=val;
      ll mid=(ipos+fpos)/2;
      if(mid>=index)
      update(index,val,ipos,mid,pos*2);
      else
      update(index,val,mid+1,fpos,pos*2+1);
    }
  }
  ll get_sum(ll l,ll r,ll ipos,ll fpos,ll pos=1)
  {
    if(ipos>fpos)
    return 0;
    if(l>r)
    return 0;
    ll mid=(ipos+fpos)/2;
    if((l==ipos)&&(r==fpos))
    return v[pos];
    else return get_sum(l,min(mid,r),ipos,mid,pos*2)+get_sum(max(mid+1,l),r,mid+1,fpos,pos*2+1);
  }
};
struct BIT
{
vector<ll>bitree;ll n;
BIT(ll n)
{
this->n=n;
bitree.resize(n+1,0);
}
void update(ll idx,ll val)
{
  idx++;
  while(idx<=n)
  {
    bitree[idx]+=val;
    idx+=idx&(-idx);
  }
}
ll Sum(ll idx)
{
  ll sum=0;
  idx++;
  while(idx>0)
  {
    sum+=bitree[idx];
    idx-=idx&(-idx);
  }
  return sum;
}
};
ll sumofdigits(ll a)
{
  ll val=0;
  while(a>0)
  {
    val+=a%10;
    a/=10;
  }
  return val;
}

ll noOfDig(ll n){
  ll cnt=0;
  while(n){
    cnt++;n/=10;
  }
  return cnt;
}

vll arr;
ll n;
bool check(ll x){
  ll ans=0;
  rep(i, 0, n-1, 1){
    if(x<arr[i])return false;
    ans+=max(0ll, x-arr[i]);
  }
  if(ans>=x)return true;
  return false;
}

int main(){
  fastio;
  cin>>n;arr.resize(n);
  rep(i, 0, n-1, 1)cin>>arr[i];
  ll l=1, r=(ll)1e18;
  // ll mid=0;
  while(l<r){
    ll mid=l+(r-l)/2;
    // cout<<mid<<endl;
    if(check(mid) and (mid==1 or !check(mid-1))){
      l=mid;
      break;
    }
    else if(check(mid))r=mid-1;
    else l=mid+1;
  }
  cout<<l<<endl;
  return 0;
}