#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define fi first
#define se second
const ll mod=1e9+7;
ll n,p[100005],ketqua;
int pa[100005];
map<pair<int,int>,int> siuu;
int find_pa(int u)
{
if (pa[u]==u) return u;
return pa[u]=find_pa(pa[u]);
}
void Merge(int u,int v,int x)
{
u=find_pa(u);
v=find_pa(v);
if (v==u) return;
ketqua+=x;
pa[u]=v;
}
vector<pair<int,int>> vec;
vector<pair<int,pair<int,int>>> edge;
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin>>n;
for (int i=1;i<=n;++i)
{
cin>>p[i];
pa[i]=i;
vec.push_back({p[i],i});
}
sort(vec.begin(),vec.end());
for (int i=1;i<=n;++i)
{
int hao=upper_bound(vec.begin(),vec.end(),make_pair(p[i],0ll))-vec.begin();
if (!siuu[{i,vec[hao].se}] && hao!=vec.size())
{
edge.push_back({vec[hao].fi%p[i],{i,vec[hao].se}});
siuu[{i,vec[hao].se}]=1;
siuu[{vec[hao].se,i}]=1;
}
for (int j=2;j*p[i]<=10000000;++j)
{
int hao=lower_bound(vec.begin(),vec.end(),make_pair(p[i]*j,0ll))-vec.begin();
//edge.push_back({vec[hao].fi%(p[i]*j),{i,vec[hao].se}});
if (!siuu[{i,vec[hao].se}] && hao!=vec.size())
{
edge.push_back({vec[hao].fi%(p[i]*j),{i,vec[hao].se}});
siuu[{i,vec[hao].se}]=1;
siuu[{vec[hao].se,i}]=1;
}
}
}
sort(edge.begin(),edge.end());
for (auto it: edge)
{
Merge(it.se.fi,it.se.se,it.fi);
//cout<<it.fi<<'\n';
}
cout<<ketqua;
}
/*/
/*/
Compilation message
sirni.cpp:26:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
26 | main()
| ^~~~
sirni.cpp: In function 'int main()':
sirni.cpp:41:42: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if (!siuu[{i,vec[hao].se}] && hao!=vec.size())
| ~~~^~~~~~~~~~~~
sirni.cpp:51:46: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | if (!siuu[{i,vec[hao].se}] && hao!=vec.size())
| ~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
183 ms |
1528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1751 ms |
159168 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
152 ms |
23964 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5098 ms |
403784 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1101 ms |
59076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1640 ms |
180132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1562 ms |
178604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
165 ms |
28544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |