We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N−1 or smaller. (The operation is the same as the one in Problem D.)
Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.
It can be proved that the largest element in the sequence becomes N−1 or smaller after a finite number of operations.
You are given the sequence ai. Find the number of times we will perform the above operation.
Constraints
2≤N≤50
0≤ai≤10
16+1000
Print the number of times the operation will be performed.
#include <iostream>
#include<cstdio>
#include<algorithm>
typedef long long ll;
using namespace std;
ll a[55];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
ll ans=0;
for(;;){
ll maxn=a[1];int ind=1;
for(int i=2;i<=n;i++) if(a[i]>maxn) maxn=a[i],ind=i;
if(maxn<=n-1) break;
for(int i=1;i<=n;i++){
if(i==ind) a[i]=maxn%n;
else a[i]+=maxn/n;
}
ans+=(maxn/n);
}
printf("%lld\n",ans);
return 0;
}