#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 M = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll int INF = 1e18;
struct DSU {
vector<int> parent;
DSU(int n){
parent = vector<int>(n+1);
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;
cin >> n;
vector<int> p(n);
for(int i = 0; i < n; i++){
cin >> p[i];
}
sort(p.begin(), p.end());
const int P = p.back() + 5;
vector<int> nearest(P, -1);
for(int i = 0; i < n; i++) nearest[p[i]] = i;
for(int i = P-2; i >= 0; i--){
if(nearest[i] == -1) nearest[i] = nearest[i+1];
}
vector<pair<ll, 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] == -1) break;
int remainder = p[nearest[p[i]+1]] % p[i];
edge.PB(MP(remainder, MP(i, nearest[p[i]+1])));
for(int cur = p[i]*2; cur < P; cur += p[i]){
if(nearest[cur] == -1) break;
remainder = p[nearest[cur]] % p[i];
edge.PB(MP(remainder, MP(i, nearest[cur])));
}
}
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;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:100:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
100 | for(auto [w, p] : edge){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
39764 KB |
Output is correct |
2 |
Correct |
286 ms |
106920 KB |
Output is correct |
3 |
Correct |
30 ms |
39896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Runtime error |
800 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
39768 KB |
Output is correct |
2 |
Correct |
29 ms |
39516 KB |
Output is correct |
3 |
Correct |
29 ms |
39832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
38080 KB |
Output is correct |
2 |
Correct |
595 ms |
71092 KB |
Output is correct |
3 |
Correct |
275 ms |
70588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
8908 KB |
Output is correct |
2 |
Correct |
322 ms |
71004 KB |
Output is correct |
3 |
Correct |
161 ms |
35520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
346 ms |
70712 KB |
Output is correct |
2 |
Correct |
822 ms |
137556 KB |
Output is correct |
3 |
Correct |
239 ms |
38588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
10444 KB |
Output is correct |
2 |
Correct |
821 ms |
136360 KB |
Output is correct |
3 |
Correct |
242 ms |
38088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
238 ms |
74944 KB |
Output is correct |
2 |
Runtime error |
710 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
282 ms |
73844 KB |
Output is correct |
2 |
Runtime error |
687 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
45764 KB |
Output is correct |
2 |
Runtime error |
880 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |