#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX_N = 100005;
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 |
171 ms |
313684 KB |
Output is correct |
2 |
Correct |
312 ms |
369104 KB |
Output is correct |
3 |
Correct |
152 ms |
312856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Runtime error |
847 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
313476 KB |
Output is correct |
2 |
Correct |
138 ms |
312852 KB |
Output is correct |
3 |
Correct |
142 ms |
313680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
53188 KB |
Output is correct |
2 |
Correct |
179 ms |
124008 KB |
Output is correct |
3 |
Correct |
100 ms |
78500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
35676 KB |
Output is correct |
2 |
Correct |
125 ms |
78208 KB |
Output is correct |
3 |
Correct |
65 ms |
37880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
82904 KB |
Output is correct |
2 |
Correct |
188 ms |
152324 KB |
Output is correct |
3 |
Correct |
89 ms |
77476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
14012 KB |
Output is correct |
2 |
Correct |
195 ms |
149884 KB |
Output is correct |
3 |
Correct |
86 ms |
77996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
233 ms |
341120 KB |
Output is correct |
2 |
Runtime error |
801 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
241 ms |
351476 KB |
Output is correct |
2 |
Runtime error |
815 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
318416 KB |
Output is correct |
2 |
Runtime error |
1174 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |