This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ar array
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 1e5 + 5, inf = 1e9;
int n, a[maxn];
vector<ar<int, 2>> ed[maxn * 10];
int lab[maxn];
int finds(int u)
{
  if(lab[u] < 0) return u;
  return lab[u] = finds(lab[u]);
}
bool merges(int u, int v)
{
  u = finds(u); v = finds(v);
  if(u == v) return false;
  if(lab[v] < lab[u]) swap(u, v);
  lab[u] += lab[v]; lab[v] = u;
  return true;
}
signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
  cin >> n;
  fill(lab + 1, lab + 1 + n, -1);
  set<int> se;
  for(int i = 1; i <= n; ++i)
    cin >> a[i], se.insert(a[i]);
  int n = 0;
  for(int i : se){
    a[++n] = i;
  }
  for(int i = 1; i <= n; ++i){
    if(i < n)
      ed[a[i + 1] % a[i]].pb({i, i + 1});
    int j = i + 1;
    for(int num = 2 * a[i]; num <= a[n]; num += a[i]){
      while(a[j] < num) ++j;
      ed[a[j] % a[i]].pb({j, i});
    }
  }
  ll mst = 0;
  for(int i = 0; i <= a[n]; ++i){
    for(auto j : ed[i]){
      if(merges(j[0], j[1]))
        mst += i;
    }
  }
  cout << mst;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |