#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define PF push_front
#define PB push_back
#define MP make_pair
#define max(a, b) ((a > b)? a : b)
#define min(a, b) ((a < b)? a : b)
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
const int N = 1e5 + 5;
const int P = 1e7 + 5;
const int M = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll int INF = 1e18;
struct DSU {
vector<int> parent;
DSU(int n){
parent = vector<int>(n);
iota(parent.begin(), parent.end(), 0);
}
int find_parent(int p){
return (parent[p] == p)? p : parent[p] = find_parent(parent[p]);
}
bool merge(int a, int b){
a = find_parent(a);
b = find_parent(b);
parent[a] = b;
return (a != b);
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
// ifstream cin();
// ofstream cout();
int n, p[N];
cin >> n;
for(int i = 0; i < n; i++){
cin >> p[i];
}
sort(p, p+n);
vector<pair<int, int>> nearest(P, MP(0, 0));
nearest[P-1] = {-1, -1};
for(int i = 0; i < n; i++) nearest[p[i]] = {p[i], i};
for(int i = P-2; i >= 0; i--){
if(nearest[i].f == 0) nearest[i] = nearest[i+1];
}
vector<pair<int, pair<int, int>>> edge;
for(int i = 0; i < n; i++){
if(i+1 < n && p[i] == p[i+1]) continue;
if(nearest[p[i]+1].f == -1) break;
edge.PB(MP(nearest[p[i]+1].f%p[i], MP(i, nearest[p[i]+1].s)));
for(int cur = p[i]*2; cur < P; cur += p[i]){
if(nearest[cur].f == -1) break;
edge.PB(MP(nearest[cur].f%p[i], MP(i, nearest[cur].s)));
}
}
sort(edge.begin(), edge.end());
DSU dsu(n);
ll int ans = 0;
for(auto [w, p] : edge){
if(dsu.merge(p.f, p.s)){
ans += w;
}
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
79184 KB |
Output is correct |
2 |
Correct |
287 ms |
130212 KB |
Output is correct |
3 |
Correct |
40 ms |
79580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
79184 KB |
Output is correct |
2 |
Runtime error |
942 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
79184 KB |
Output is correct |
2 |
Correct |
38 ms |
79184 KB |
Output is correct |
3 |
Correct |
39 ms |
79388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
205 ms |
105912 KB |
Output is correct |
2 |
Correct |
626 ms |
130232 KB |
Output is correct |
3 |
Correct |
308 ms |
129808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
83160 KB |
Output is correct |
2 |
Correct |
332 ms |
130728 KB |
Output is correct |
3 |
Correct |
197 ms |
106292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
377 ms |
129964 KB |
Output is correct |
2 |
Correct |
834 ms |
179364 KB |
Output is correct |
3 |
Correct |
275 ms |
104380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
87232 KB |
Output is correct |
2 |
Correct |
844 ms |
178340 KB |
Output is correct |
3 |
Correct |
276 ms |
105412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
246 ms |
106376 KB |
Output is correct |
2 |
Runtime error |
780 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
104632 KB |
Output is correct |
2 |
Runtime error |
864 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
82872 KB |
Output is correct |
2 |
Runtime error |
986 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |