#include<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
struct DSU {
vector<int> e;
DSU(int N) {e = vector<int>(N, -1);}
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y) {
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y]; e[y] = x; return true;
}
};
const int N=1e7+10;
vector<int> great(N,1e9);
int main () {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector<int> v;
{
set<int> s;
for(int i = 0;i<n;i++) {
int a;
cin >> a;
s.insert(a);
}
v=vector<int>(s.begin(),s.end());
}
n=v.size();
for(int i = 0;i<n;i++)great[v[i]]=i;
for(int j = N-3;j>=0;j--) {
great[j]=min(great[j],great[j+1]);
}
vector<pair<int, pair<int,int>>> ed;
for(int i=0;i<n;i++) {
for(int j=v[i];j<N-3;j+=v[i]) {
if(j!=v[i]) {
if(great[j]<n) {
ed.push_back({v[great[j]]-j,{i,great[j]}});
}
}
else if(great[j+1]<n) {
ed.push_back({v[great[j+1]]-j,{i,great[j+1]}});
}
}
}
sort(ed.begin(),ed.end());
DSU d(n+1);
long long sum=0;
for(auto i:ed) {
sum+=(int)d.unite(i.second.first,i.second.second)*i.first;
}
cout << sum << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
39668 KB |
Output is correct |
2 |
Correct |
410 ms |
88960 KB |
Output is correct |
3 |
Correct |
27 ms |
39984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
39576 KB |
Output is correct |
2 |
Runtime error |
811 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
39600 KB |
Output is correct |
2 |
Correct |
27 ms |
39508 KB |
Output is correct |
3 |
Correct |
26 ms |
39780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
322 ms |
68868 KB |
Output is correct |
2 |
Correct |
982 ms |
93144 KB |
Output is correct |
3 |
Correct |
507 ms |
92544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
43216 KB |
Output is correct |
2 |
Correct |
585 ms |
89116 KB |
Output is correct |
3 |
Correct |
382 ms |
68100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
611 ms |
93412 KB |
Output is correct |
2 |
Correct |
1409 ms |
142516 KB |
Output is correct |
3 |
Correct |
441 ms |
68852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
232 ms |
47276 KB |
Output is correct |
2 |
Correct |
1460 ms |
141692 KB |
Output is correct |
3 |
Correct |
460 ms |
68308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
69096 KB |
Output is correct |
2 |
Runtime error |
939 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
382 ms |
69104 KB |
Output is correct |
2 |
Runtime error |
959 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
43460 KB |
Output is correct |
2 |
Runtime error |
1333 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |