/*
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⡤⢶⡺⠛⢓⡶⢤⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡟⠀⠀⢀⡗⠀⠸⡇⠀⠈⢻⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣼⠁⠀⢀⡞⠀⣀⠀⢳⡀⠀⠈⣧⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⠴⠶⢿⣉⢡⡷⠦⠤⠬⠴⠊⣍⡳⣮⡷⠤⣴⢿⣏⣹⠷⠶⠤⣤⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠠⣶⣶⣛⡉⠁⣀⣀⣀⣀⣈⣿⠲⠦⣤⣀⡴⣊⣭⡑⢬⣻⣷⠷⠖⣿⠤⠤⣤⣤⣀⣀⣉⣉⣻⣿⡿⠃⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠙⠯⣟⡻⠿⣯⣤⣠⠤⠟⠓⢲⡦⣬⣷⣝⣶⣻⣾⣥⣴⣶⡛⠛⠧⢄⣀⣠⡴⠾⠛⣋⡽⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⢶⣤⣉⠙⠿⣶⣞⡁⠀⠀⢸⠋⠛⢹⡇⠀⠀⠀⢉⣽⠶⠛⠉⣁⣴⠖⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠷⣦⣈⠙⢿⣦⣀⣸⡇⠀⠈⡇⢀⣤⡾⠛⢁⣠⠖⣫⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣨⡽⠳⢦⡈⠛⢿⣇⣀⣠⡿⠟⢅⣤⠖⠋⣠⡴⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠻⣇⠀⠸⣿⣷⣄⡉⠛⢋⣠⣾⣿⠗⠀⢠⣿⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⠀⠈⠢⡀⠈⠳⠽⠟⠒⠋⠻⠟⠁⠀⢴⠟⣡⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⠀⠀⣀⡈⠓⠦⡄⠀⠀⠀⠀⢀⣠⠞⢁⠔⠹⣿⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠋⠀⣰⣷⣭⣲⡤⠀⠀⠀⠀⠀⠉⠀⠒⠁⠀⠀⢯⠳⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⡞⠳⢦⣠⣿⠃⣹⠀⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡤⠷⠿⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢀⣠⢶⣫⣿⣿⢿⣦⣀⡿⢿⣰⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣴⡋⠉⢳⡀⢀⢠⣾⣿⣶⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⢀⣠⠖⣫⣵⢿⣽⡿⢿⡽⢻⣏⣀⠴⣻⢳⠦⢤⣤⣤⣤⣤⠴⠖⣛⣧⡙⢟⢦⡀⠙⣟⣿⡺⣾⣝⡿⣯⣗⢤⣀⠀⠀⠀⠀⠀⠀
⠀⠀⣴⣟⣿⣟⣯⣾⢟⣫⣾⡿⠞⠁⢨⠏⡴⣱⠯⠶⠒⠚⣉⣩⠤⠖⠋⠁⠀⠙⣎⢳⡘⣟⠋⠲⣝⣾⣝⣿⣾⣝⠿⣮⣗⣤⣤⡀⠀⠀
⠀⣠⣿⢿⣷⣿⣮⣵⢟⡽⠋⠀⠀⢀⡞⢰⣳⠃⡴⠖⠚⠉⠉⠀⠀⠀⠀⠀⣠⠞⢈⣧⣳⡘⣆⠀⠀⠑⢯⣳⣮⡛⢿⣮⡟⣻⣿⣧⡀⠀
⢘⣿⣷⡞⠻⣿⣟⡵⠋⠀⠀⠀⠀⣼⢀⣧⡏⣸⠃⠀⠀⠀⠀⠀⠀⠀⠀⡴⠁⣠⠞⠈⢧⢧⠸⡄⠀⠀⠀⠉⠫⡻⢿⢟⣾⣿⡙⠻⢷⣄
⠋⠀⠙⠿⣦⣽⣿⣿⡀⠀⠀⠀⢀⡇⢸⣼⢀⡏⠀⠀⠀⠀⠀⠀⠀⢀⡞⠀⡴⠁⠀⠀⠘⡟⡆⢻⠀⠀⠀⠀⠀⢘⣦⣞⡞⢀⣿⣶⣿⣿
⡇⠀⢀⠔⢻⡘⡟⣟⣷⡀⠀⠀⢸⡇⣿⡇⣸⠁⠀⠀⠀⠀⠀⠀⢠⠞⢀⠞⠀⠀⠀⠀⠀⢻⣷⠘⡇⠀⠀⠀⢀⣾⢯⣾⠶⣷⠋⠁⠀⠀
⡟⠛⠁⠀⠘⡇⣧⠘⣿⣷⡄⠀⢸⡇⣿⣇⣿⠀⠀⠀⠀⠀⠀⢀⡎⢠⠏⠀⠀⠀⠀⠀⠀⠸⣿⡄⣇⠀⠀⢀⣾⢏⡞⣼⢸⠇⠉⠲⠤⣀
⡇⠀⠀⠀⠀⣷⢻⠀⠘⢿⣿⠶⠾⢧⡟⢿⡇⠀⠀⠀⠀⠀⠀⡼⢀⡞⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⣿⠀⢀⣾⢯⡞⠀⡟⣸⠀⠀⠀⠀⠀
⣧⠀⠀⠀⠀⢻⢸⠀⠀⠘⣿⢦⠀⠀⣷⡿⡇⠀⠀⠀⠀⢀⠼⢁⡾⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡟⠉⠉⢹⣯⠏⠀⠀⡇⣿⠀⠀⠀⠀⠀
⢸⡀⠀⠀⠀⢸⣾⡇⠀⠀⣇⠹⣿⠿⠿⠷⡙⢆⢀⡠⠞⣁⠴⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣷⠤⠴⡿⣿⠀⠀⠀⡇⡿⠀⠀⠀⠀⠀
⠀⢷⡀⠀⣠⢞⡽⠁⠀⢀⡏⣠⠏⠀⠀⠀⠈⢪⡉⣤⠚⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠙⣏⠁⣿⠀⠀⢠⡇⣇⠀⠀⠀⠀⢰
⠀⢺⣍⠉⠹⠊⠀⠀⠀⢸⡛⠁⠀⠀⠀⠀⠀⠘⡇⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢦⣸⡄⠀⠀⠓⠈⠢⣀⣠⠔⢳
⠀⢠⡏⠀⠀⠀⠀⠀⠀⠈⣳⠀⠀⠀⠀⠀⠀⠀⡇⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⠀⠀⠀⠀⠀⠀⠀⢠⠏
⠀⠀⡇⠀⠀⠀⠀⠀⠀⢸⣟⣇⠀⠀⠀⠀⠀⠀⣷⢸⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⠀⠀⠀⠀⠀⠀⠀⢸⠀
*/
// made by djsksbrbf, 2025
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
#define pb push_back
#define fi first
#define se second
const int MOD = 1e9 + 7;
const int MAX = 2e5 + 5;
#define int ll
int n;
int par[MAX], sz[MAX];
int find(int x){
if(x == par[x])return x;
return par[x] = find(par[x]);
}
bool merge(int x, int y){
x = find(x);
y = find(y);
if(x == y)return 0;
if(sz[x] < sz[y])swap(x, y);
sz[x] += sz[y];
par[y] = x;
return 1;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
vector <int> a(n + 1);
for(int i = 1 ; i <= n ; i++)cin >> a[i];
for(int i = 1 ; i <= n ; i++)par[i] = i, sz[i] = 1;
sort(a.begin() + 1, a.end());
a.erase(unique(a.begin() + 1, a.end()), a.end());
n = a.size() - 1;
vector<pii> we[a[n] + 5];
for(int i = 1 ; i <= n ; i++){
int tmp = i + 1;
for(int mx = a[i] ; mx <= a[n] ; mx += a[i]){
while(tmp <= n && a[tmp] < mx)tmp++;
if(tmp > n)continue;
we[a[tmp] % a[i]].pb({i, tmp});
}
}
ll ans = 0;
for(int i = 0 ; i <= a[n] ; i++){
for(auto it : we[i]){
if(merge(it.fi, it.se))ans += i;
}
}
cout << ans << endl;
return 0;
}
# | 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... |