#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define endl "\n"
const int N = 1e5 + 5, M = 1e7, mod = 1e9 + 7; //!
int t, p[N], last[M + 2], id[M + 5];
vector<pair<int,int> > E[M + 2];
int find(int u) {
return (p[u] == u ? u : p[u] = find(p[u]));
}
bool merge(int u,int v) {
u = find(u), v = find(v);
if(u == v) return 0;
p[u] = v;
return 1;
}
main() {
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n;
cin >> n;
vector<int> x;
for(int i = 1; i <= n; i++) {
int a; cin >> a;
x.push_back(a);
}
sort(x.begin(), x.end());
x.erase(unique(x.begin(), x.end()), x.end());
for(int i = 0; i < x.size(); i++) {
id[x[i]] = i;
last[x[i]] = x[i];
p[i] = i;
}
for(int i = M; i >= 0; i--) {
if(!last[i]) last[i] = last[i + 1];
}
for(int i = 0; i < x.size(); i++) {
for(int j = 0; j <= M; j += x[i]) {
if(!last[j]) break;
E[last[j] % x[i]].push_back({id[x[i]], id[last[j]]});
}
}
ll ans = 0;
for(int i = 0; i <= M; i++) {
for(int j = 0; j < E[i].size(); j++) {
int u = E[i][j].f, v = E[i][j].s;
ans += i * merge(u, v);
}
}
cout << ans;
}
Compilation message
sirni.cpp:20:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
20 | main() {
| ^~~~
sirni.cpp: In function 'int main()':
sirni.cpp:31:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int i = 0; i < x.size(); i++) {
| ~~^~~~~~~~~~
sirni.cpp:39:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 0; i < x.size(); i++) {
| ~~^~~~~~~~~~
sirni.cpp:47:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int j = 0; j < E[i].size(); j++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
166 ms |
278076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
167 ms |
274400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
184 ms |
278348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
250 ms |
290160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
202 ms |
280544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
326 ms |
302392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
197 ms |
279212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
344 ms |
328332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
346 ms |
333132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
233 ms |
311100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |