#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,sse4")
#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int32_t> p, r;
DSU(int32_t n) {
p.assign(n,0);
r.assign(n,1);
for (int i=0;i<n;i++)p[i]=i;
}
int get(int32_t i) {
if (i != p[i]) p[i]=get(p[i]);
return p[i];
}
bool unite(int32_t a,int32_t b) {
a=get(a);
b=get(b);
if (a==b)return false;
if (r[a]<r[b])swap(a,b);
p[b]=a;
r[a]+=r[b];
return true;
}
};
signed test(int n, vector<int> &b) {
int lr=b.back();
n=b.size();
vector<int32_t> clos(lr+1,-1);
for (int i=0;i<n;i++) {
clos[b[i]]=i;
}
for (int32_t i=lr-1;i>=0;i--) {
if (clos[i] == -1) {
clos[i]=clos[i+1];
}
}
typedef array<int32_t,2> pii;
vector<vector<pii>> edg(lr+1);
for (int i=0;i<n-1;i++) {
int x=b[i];
vector<int> tmp;
edg[(b[clos[x+1]]%x)].push_back({i, clos[x+1]});
tmp.push_back(clos[x+1]);
for (int32_t j=2*x;j<=b.back();j+=x) {
if (clos[j] != -1) {
if (tmp.back() != clos[j]) {
edg[(b[clos[j]]%x)].push_back({clos[j], i});
tmp.push_back(clos[j]);
}
}
}
if (tmp.size() == 0 || tmp.back() != n-1) {
edg[(b.back()%x)].push_back({n-1, i});
}
}
DSU d(b.size());
int cost=0;
int steps=0;
bool term=false;
for (int c=0;c<=b.back();c++) {
for (auto &x:edg[c]) {
if (d.unite(x[0], x[1])) {
cost+=c;
steps++;
if (steps == n-1) {
term=true;
break;
}
}
}
if (term)break;
}
return cost;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
vector<int> a(n);
for (int &x:a)cin>>x;
if (n == 1) {
cout<<0<<"\n";
return 0;
}
sort(a.begin(),a.end());
vector<int> b;
for (int x:a) {
if (b.size()==0||b.back()!=x)b.push_back(x);
}
cout<<test(n,b)<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
274256 KB |
Output is correct |
2 |
Correct |
97 ms |
275732 KB |
Output is correct |
3 |
Correct |
78 ms |
273488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
271 ms |
274512 KB |
Output is correct |
3 |
Correct |
77 ms |
274512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
274260 KB |
Output is correct |
2 |
Correct |
76 ms |
273748 KB |
Output is correct |
3 |
Correct |
79 ms |
274484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
40512 KB |
Output is correct |
2 |
Correct |
104 ms |
67484 KB |
Output is correct |
3 |
Correct |
55 ms |
44424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
29784 KB |
Output is correct |
2 |
Correct |
65 ms |
50004 KB |
Output is correct |
3 |
Correct |
44 ms |
26300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
51872 KB |
Output is correct |
2 |
Correct |
116 ms |
80416 KB |
Output is correct |
3 |
Correct |
58 ms |
43952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
10232 KB |
Output is correct |
2 |
Correct |
111 ms |
74864 KB |
Output is correct |
3 |
Correct |
54 ms |
42768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
290092 KB |
Output is correct |
2 |
Correct |
711 ms |
491564 KB |
Output is correct |
3 |
Correct |
166 ms |
292796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
290244 KB |
Output is correct |
2 |
Correct |
1263 ms |
575156 KB |
Output is correct |
3 |
Correct |
180 ms |
295756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
277328 KB |
Output is correct |
2 |
Correct |
1391 ms |
569896 KB |
Output is correct |
3 |
Correct |
58 ms |
44572 KB |
Output is correct |