Problem:
Electric Metronome
A boy named PCK is playing with
N
electric metronomes. The
i
-th metronome is set to tick every
t_i
seconds. He started all of them simultaneously.
He noticed that, even though each metronome has its own ticking interval, all of them tick simultaneously from time to time in certain intervals. To explore this interesting phenomenon more fully, he is now trying to shorten the interval of ticking in unison by adjusting some of the metronomes’ interval settings. Note, however, that the metronomes do not allow any shortening of the intervals.
Given the number of metronomes and their preset intervals
t_i
(sec), write a program to make the tick-in-unison interval shortest by adding a non-negative integer
d_i
to the current interval setting of the
i
-th metronome, and report the minimum value of the sum of all
d_i
.
Input
The input is given in the following format.
N
t_1
t_2
:
t_N
The first line provides the number of metronomes
N
(1 ≤
N
≤ 10
5
). Each of the subsequent
N
lines provides the preset ticking interval
t_i
(1 ≤
t_i
≤ 10
4
) of the
i
-th metronome.
Output
Output the minimum value.
Sample Input 1
3
3
6
8
Sample Output 1
3
If we have three metronomes each with a ticking interval of 3, 6, and 8 seconds, respectively, simultaneous activation of these will produce a tick in unison every 24 seconds. By extending the interval by 1 second for the first, and 2 seconds for the second metronome, the interval of ticking in unison will be reduced to 8 seconds, which is the shortest possible for the system.
Sample Input 2
2
10
10
Sample Output 2
0
If two metronomes are both set to 10 seconds, then simultaneous activation will produce a tick in unison every 10 seconds, which is the shortest possible for this system.

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    int N;
    cin>>N;
    vector<int> A(N);
    for(int i=0;i<N;i++) cin>>A[i];
    sort(A.begin(),A.end());
    int ans = 0;
    int now = 1;
    for(int x:A){
        while(A[N-1]%now || now < x) now++;
        ans += x-now;
    }
    cout<<-ans<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3904
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	vector<int> vec(n);
	for(int i=0;i<n;i++){
		cin>>vec[i];
	}
	int ma=*max_element(vec.begin(),vec.end());
	vector<int> div;
	for(int i=1;i*i<=ma;i++){
		if(ma%i==0){
			div.push_back(i);
			if(i!=ma/i)div.push_back(ma/i);
		}
	}
	sort(div.begin(),div.end());
	int ans=0;
	for(int i=0;i<n;i++){
		ans+=*lower_bound(div.begin(),div.end(),vec[i])-vec[i];
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3556
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
  int n;cin >>n;
  vector<int> t(n);
  int mx=0;
  for (int i=0;i<n;i++) {
    cin>>t[i];
    mx=max(mx,t[i]);
  }
  vector<int> ds;
  for (int x=1;x<=mx;x++) if (mx%x==0)ds.push_back(x);
  long long ans=0;
  for (auto x : t) {
    for (auto v : ds) {
      if (v>=x) {
        ans+=v-x;
        break;
      }
    }
  }
  cout << ans<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3712
final_rank: 6

code_snippet: #include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

const size_t N_MIN = 1;
const size_t N_MAX = 1e5;
const size_t T_MIN = 1;
const size_t T_MAX = 1e4;

int main()
{
    int n = 0;
    cin >> n;
    if (n < N_MIN || N_MAX < n)
    {
        cout << N_MIN << " <= n <= " << N_MAX << ") but input " << n << endl;
        exit(1);
    }
    vector<int> tlist(n);
    for (int i = 0; i < n; i++)
    {
        cin >> tlist[i];
        if (tlist[i] < T_MIN || T_MAX < tlist[i])
        {
            cout << T_MIN << " <= xi <= " << T_MAX << ") but input " << tlist[i] << endl;
            exit(1);
        }
    }
    // 昇順ソート
    sort(tlist.begin(), tlist.end());
    // maxの約数を列挙
    vector<int> divisor(tlist[n - 1]);
    int         maxt   = tlist[n - 1];
    int         dCount = 0;
    for (int i = 1; i <= maxt; i++)
    {
        if ((maxt % i) == 0)
        {
            divisor[dCount++] = i;
        }
    }
    long long ans = 0;
    for (int i = 0; i < n; i++)
    {
        // tを増やして、最初に一致する約数を検索
        for (int j = 0; j < dCount; j++)
        {
            if (divisor[j] >= tlist[i])
            {
                ans += (divisor[j] - tlist[i]);
                break;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3608
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
long long n,t,i,j,a,flg[9999],d,mx;
int main(void){
	cin>>n;
	for(i=0;i<n;i++){
		cin>>t;
		if(mx<t) mx=t;
		flg[t-1]++;
	}
	for(i=0;i<mx;i++){
		if(flg[i]>0){
			for(j=0;j<=10000;j++){
				if(mx%(i+1+j)==0){
					d+=flg[i]*j;
					break;
				}
			}
		}
	}
	cout<<d<<endl;
	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3164
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(void){
	int n,t[100000],i,j,a,d=0,mx=0;
	cin>>n;
	for(i=0;i<n;i++){
		cin>>t[i];
		if(mx<t[i]) mx=t[i];
	}
	for(i=0;i<n;i++){
		for(j=0;j<=4500;j++){
			a=t[i]+j;
			if(mx%a==0){
				d+=j;
				break;
			}
		}
	}
	cout<<d<<endl;
	return 0;
}
accuracy: 0.36363636363636365
time_ms: 220
memory_kb: 3272
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(void){
	int n,t[100000],i,j,a,d=0,mx=0;
	cin>>n;
	for(i=0;i<n;i++){
		cin>>t[i];
		if(mx<t[i]) mx=t[i];
	}
	for(i=0;i<n;i++){
		for(j=0;j<=4000;j++){
			a=t[i]+j;
			if(mx%a==0){
				d+=j;
				break;
			}
		}
	}
	cout<<d<<endl;
	return 0;
}
accuracy: 0.36363636363636365
time_ms: 220
memory_kb: 3240
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(void){
	int n,t[100000],i,j,a,d=0,mx=0;
	cin>>n;
	for(i=0;i<n;i++){
		cin>>t[i];
		if(mx<t[i]) mx=t[i];
	}
	for(i=0;i<n;i++){
		for(j=0;j<=2500;j++){
			a=t[i]+j;
			if(mx%a==0){
				d+=j;
				break;
			}
		}
	}
	cout<<d<<endl;
	return 0;
}
accuracy: 0.36363636363636365
time_ms: 180
memory_kb: 3324
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(void){
	int n,t[100000],i,j,a,d=0,mx=0;
	cin>>n;
	for(i=0;i<n;i++){
		cin>>t[i];
		if(mx<t[i]) mx=t[i];
	}
	for(i=0;i<n;i++){
		for(j=0;j<=1000;j++){
			a=t[i]+j;
			if(mx%a==0){
				d+=j;
				break;
			}
		}
	}
	cout<<d<<endl;
	return 0;
}
accuracy: 0.36363636363636365
time_ms: 100
memory_kb: 3236
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
int n,i,t[100001];

int main()
{
	long long max=0,k=0;
	cin>>n;
	for(i=0;i<n;i++) {
		cin>>t[i];
		if(max<t[i])max=t[i];
	}
	int j;
	for(i=0;i<n;i++){
		if(max%t[i]!=0){
			for(j=0;j<1001;j++){
				if(max%t[i]==0)break;
				t[i]++;
				k++;
			}
		}
	}
	cout<<k<<endl;
	return 0;
}
accuracy: 0.36363636363636365
time_ms: 320
memory_kb: 3272
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
int n,i,t[100001],k=0;

int main()
{
	long long max=0;
	cin>>n;
	for(i=0;i<n;i++) {
		cin>>t[i];
		if(max<t[i])max=t[i];
	}
	int j;
	for(i=0;i<n;i++){
		if(max%t[i]!=0){
			for(j=0;j<100;j++){
				if(max%t[i]==0)break;
				t[i]++;
				k++;
			}
		}
	}
	cout<<k<<endl;
	return 0;
}
accuracy: 0.36363636363636365
time_ms: 50
memory_kb: 3276
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
int n,i,t[100001],k=0;
int main()
{
	int max=0;
	cin>>n;
	for(i=0;i<n;i++) {
		cin>>t[i];
		if(max<t[i])max=t[i];
	}
	int j;
	for(i=0;i<n;i++){
		if(max%t[i]!=0){
			for(j=0;j<100;j++){
				if(max%t[i]==0)break;
				t[i]++;
				k++;
			}
		}
	}
	cout<<k<<endl;
	return 0;
}
accuracy: 0.36363636363636365
time_ms: 20
memory_kb: 3324
final_rank: 14

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

int main()
{
	long long n;
	cin >> n;
	vector<long long> a(n);
	for(long long i = 0;i < n;i++){
		cin >> a[i];
	}
	sort(a.begin(),a.end());
	long long it = a.back();
	vector<long long> p;
	for(long long i = 1;i <= it;i++){
		if(it % i == 0){
			p.push_back(i);
		}
	}
	long long ans = 0;
	for(long long i = 0;i < n - 1;i++){
		auto pos = lower_bound(p.begin(),p.end(),a[i]);
		ans += *pos - a[i];
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3888
final_rank: 10

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int N, t[100000];
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> t[i];
    }

    int max_t = *max_element(t, t + N);
    vector<int> divs_mt;
    for (int i = 1; i*i <= max_t; i++) {
        if (max_t % i == 0) {
            divs_mt.push_back(i);
            divs_mt.push_back(max_t / i);
        }
    }
    sort(divs_mt.begin(), divs_mt.end());
        
    
    int ans = 0;
    for (int i = 0; i < N; i++) {
        ans += *lower_bound(divs_mt.begin(),
                divs_mt.end(), t[i]) - t[i];
    }

    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3732
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int N, t[100000];
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> t[i];
    }

    int max_t = *max_element(t, t + N);
    vector<int> divs_mt;
    for (int i = 1; i <= max_t; i++) {
        if (max_t % i == 0) {
            divs_mt.push_back(i);
        }
    }
        
    
    int ans = 0;
    for (int i = 0; i < N; i++) {
        ans += *lower_bound(divs_mt.begin(),
                divs_mt.end(), t[i]) - t[i];
    }

    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3808
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin>>a;
    vector<int> b(a);
    for(int i=0;i<a;i+=1){
        cin>>b.at(i);
    }
    sort(b.begin(),b.end());
    int cycle=b.at(a-1);
    vector<int> c(0);
    for(int i=1;i*i<=cycle;i+=1){
        if(cycle%i==0){
            c.push_back(i);
            c.push_back(cycle/i);
        }
    }
    sort(c.begin(),c.end());
    int ans=0;
    for(int i=0;i<a-1;i+=1){
        ans+=*lower_bound(c.begin(),c.end(),b.at(i))-b.at(i);
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3276
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod1 = 1000000007;


int main(){
    ll N;cin >> N;
    vector<ll> t(N);
    ll ma = 0;
    vector<ll> a(0);
    for(int i = 0; i < N; i++) cin >> t[i];
    for(int i = 0; i < N; i++){
        ma = max(ma,t[i]);
    }
    for(int i = 1; i <= ma; i++){
        if((ma%i) == 0) a.push_back(i);
    }
    ll d = 0;
    for(int i = 0; i < N; i++){
        for(auto aa: a){
            if(t[i]<=aa){
                d+=aa-t[i];
                break;
            }
        }
    }
    cout << d << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3876
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using ll=long long;
using namespace std;
vector<ll>AllDivisors(ll a)
{
    auto back=vector<ll>();
    for(int i=1; pow(i,2)<=a; i++)
    {
        if(a%i==0)
        {
            back.push_back(i);
            back.push_back(a/i);
        }
    }
    return back;
}


int main()
{
    int n; cin>>n;
    auto t=vector<ll>();
    ll big=0;
    for(int i=0; i<n; i++)
    {
        ll a; cin>>a;
        t.push_back(a);
        big=max(big,a);
    }
    auto d=AllDivisors(big);
    sort(d.begin(),d.end());
    ll ans=0;
    for(auto i:t)
    {
        auto p=lower_bound(d.begin(),d.end(),i);
        ans+=*p-i;
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4076
final_rank: 13

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//using mint2 = modint998244353;
typedef long long ll;
#define rep1(i, m, n) for(int i = m; i < (int)(n); i++)
#define rep2(i, m, n) for(int i = m; i <= (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
template<class T> inline bool chmax(T& a, T b) { if(a < b) {a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if(a > b) {a = b; return true; } return false; }
const ll INF = 1LL << 60;
//#define _GLIBCXX_DEBUG
// 範囲外エラーを教えてくれる。使うときはincludeより上に置く。
 
// const char newl='\n';

vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    sort(ret.begin(), ret.end()); // 昇順に並べる
    return ret;
}

int lcm(int a, int b){
  return a*b / __gcd(a, b);
}

int main() {
  int n; cin >> n;
  vector<int> t(n);
  int mt=0;
  for (int i=0; i<n; i++) {
    cin >> t[i];
    mt=max(mt,t[i]);
  }
  vector<ll> d=divisor(ll(mt));
  int ans=0;
  for (int i=0; i<n; i++) {
    std::vector<long long>::iterator ite=lower_bound(all(d),t[i]);
    ans+=*ite-t[i];
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3244
final_rank: 1

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <cstdint>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <fstream>
#include <cmath>
#include <limits>
#include <chrono>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
#include <cassert>
#include <random>
using namespace std;
using ll = long long;

ll ll_gcd(ll a, ll b) {
    if (a < b) return ll_gcd(b, a);
    ll r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

int main() {
    ll n;
    cin >> n;
    vector<ll> z(n);
    for (int i = 0; i < n; i++) {
        cin >> z[i];
    }
    sort(z.begin(), z.end());
    ll t = z[n-1];
    vector<ll> c;
    for (int i = 0; i < t; i++) {
        if (t % (i + 1) == 0)c.push_back(i + 1);
    }
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < c.size(); j++) {
            if (c[j] >= z[i]) {
                ans += c[j] - z[i];
                break;
            }
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3948
final_rank: 12