#include<bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
#define debug(y) cout<<y,exit(0)
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 1e7 + 9;
const ll inf = 1e9 + 7;
vector<LL> g[N];
vector<ll> a;
ll n,x,near[N],lab[N/100];
ll Find(ll u){
if (lab[u] < 0) return u;
return lab[u] = Find(lab[u]);
}
void Union(ll r,ll s){
r = Find(r); s = Find(s);
if (r == s) return;
if (lab[r] > lab[s]) swap(r,s);
lab[r] += lab[s]; lab[s] = r;
}
int main(){
ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
#define task "tst"
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
//freopen(task".out","w",stdout);
}
cin>>n;
for (ll i = 1;i <= n;i++) cin>>x,a.push_back(x);
sort(a.begin(),a.end()); a.resize(unique(a.begin(),a.end()) - a.begin());
ll prv = 0;
for (ll i = 0;i < a.size();i++){
for (ll j = prv + 1;j < a[i];j++) near[j] = i;
prv = a[i] - 1;
}
memset(lab,-1,sizeof(lab));
for (ll i = 0;i < a.size();i++){
ll now = a[i];
for (ll j = now;j < a.back();j += now){
ll val = a[near[j]] % now;
g[val].push_back({i,near[j]});
//cout<<now<<" "<<a[near[j]]<<" "<<val<<"\n";
}
}
ll ans = 0; //return 0;
for (ll i = 0;i <= a.back();i++){
for (auto j : g[i]){
if (Find(j.fs) != Find(j.sc)) ans += i;
Union(j.fs,j.sc);
}
}
cout<<ans;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:38:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (ll i = 0;i < a.size();i++){
| ~~^~~~~~~~~~
sirni.cpp:43:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (ll i = 0;i < a.size();i++){
| ~~^~~~~~~~~~
sirni.cpp:31:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
176 ms |
274796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
134 ms |
235668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
174 ms |
274804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
182 ms |
250136 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
144 ms |
241416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
209 ms |
262224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
141 ms |
240392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
236 ms |
288568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
254 ms |
292904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
185 ms |
277152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |