#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX_N = 100000;
int parent[MAX_N], siz[MAX_N];
int find_parent(int x) {
if(parent[x] == x) return x;
return parent[x] = find_parent(parent[x]);
}
void unite(int a, int b) {
int x = find_parent(a), y = find_parent(b);
if(x==y) return;
if(siz[x] < siz[y]) swap(x,y);
siz[x] += siz[y];
parent[y] = x;
}
void solve() {
int n;
cin >> n;
vector<int>a;
for(int i=0; i<n; ++i) {
int tmp;
cin >> tmp;
assert(tmp>=1);
a.push_back(tmp);
parent[i] = i;
siz[i] = 1;
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
int largest = a.back();
vector<int>T(largest+1, -1);
for(int i=0; i<a.size(); ++i) T[a[i]] = i;
for(int i=largest-1; i>=0; --i) {
if(T[i] != -1) continue;
T[i] = T[i+1];
}
int ans = 0;
vector<vector<pair<int,int>>>pq(largest+1);
for(int i=0; i<a.size()-1; ++i) {
pq[a[i+1]%a[i]].push_back({i+1, i});
for(int j=2*a[i]; j<=largest; j+=a[i]) {
int p = T[j];
pq[a[p]%a[i]].push_back({p, i});
}
}
for(int i=0; i<=largest; ++i) {
for(pair<int,int>cur: pq[i]) {
if(find_parent(cur.first) == find_parent(cur.second)) continue;
unite(cur.first,cur.second);
ans += i;
}
}
cout << ans << endl;
}
signed main()
{
int t=1;
while(t--) solve();
return 0;
}
Compilation message
sirni.cpp: In function 'void solve()':
sirni.cpp:39:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i=0; i<a.size(); ++i) T[a[i]] = i;
| ~^~~~~~~~~
sirni.cpp:46:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i=0; i<a.size()-1; ++i) {
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
313432 KB |
Output is correct |
2 |
Correct |
295 ms |
369184 KB |
Output is correct |
3 |
Correct |
156 ms |
312812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Runtime error |
746 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
313524 KB |
Output is correct |
2 |
Correct |
139 ms |
312844 KB |
Output is correct |
3 |
Correct |
138 ms |
313508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
53700 KB |
Output is correct |
2 |
Correct |
162 ms |
117856 KB |
Output is correct |
3 |
Correct |
91 ms |
78508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
35672 KB |
Output is correct |
2 |
Correct |
125 ms |
78776 KB |
Output is correct |
3 |
Correct |
58 ms |
38648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
81528 KB |
Output is correct |
2 |
Correct |
191 ms |
151616 KB |
Output is correct |
3 |
Correct |
90 ms |
75060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
14308 KB |
Output is correct |
2 |
Correct |
220 ms |
152836 KB |
Output is correct |
3 |
Correct |
91 ms |
78152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
225 ms |
341952 KB |
Output is correct |
2 |
Runtime error |
792 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
352380 KB |
Output is correct |
2 |
Runtime error |
795 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
318716 KB |
Output is correct |
2 |
Runtime error |
1164 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |