#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define f first
#define s second
#define FOR(i, a, b, s) for (int i = (a); i < (b); i += s)
#define F0R(i, a) for (int i = 0; i < (a); i++)
struct Edge {
int u,v; ll w;
Edge (int u, int v, ll w) : u(u),v(v),w(w) {};
bool operator>(Edge& y) {return w > y.w;}
};
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
vector<int> a(n);
F0R(i,n) {cin >> a[i];}
//F0R(i,n) {a[i]=i+1;} a[n-1] = 10000000;
sort(a.begin(),a.end(),greater<int>());
a.resize(distance(a.begin(),unique(a.begin(),a.end())));
n = a.size();
vector<int> ptr(10000002,INT_MAX-1);
unordered_map<int,int> rev;
vector<Edge> e;
F0R(i,n) {ptr[a[i]]=0; rev[a[i]]=i;}
for (int i=ptr.size()-2;i>=0;i--) {ptr[i] = min(ptr[i],ptr[i+1]+1);}
F0R(i,n) {
int x = a[i];
if (ptr[x+1]!=INT_MAX-1) {e.pb(Edge(i,rev[x+1+ptr[x+1]],(x+1+ptr[x+1])%x));}
FOR(j,x*2,1e7+2,x) {
if (ptr[j]==INT_MAX-1) {break;}
e.pb(Edge(i,rev[j+ptr[j]],(j+ptr[j])%j));
}
}
vector<int> par(n),sz(n,1);
F0R(i,n) {par[i]=i;}
auto get = [&](auto& get, int x)->int {return (par[x]==x ? x : par[x]=get(get,par[x]));};
auto unite = [&](int x, int y)->bool {
x = get(get,x), y = get(get,y);
if (x != y) {
if (sz[y]<sz[x]) {swap(x,y);}
sz[y] += sz[y]; par[x] = y;
return true;
} return false;
};
sort(e.begin(),e.end(),greater<>());
ll ans = 0;
while (e.size()) {
Edge x = e.back();
if (unite(x.u,x.v)) {ans += x.w;}
e.pop_back();
} cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
39760 KB |
Output is correct |
2 |
Correct |
294 ms |
105336 KB |
Output is correct |
3 |
Correct |
29 ms |
40140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
39772 KB |
Output is correct |
2 |
Runtime error |
918 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
39764 KB |
Output is correct |
2 |
Correct |
30 ms |
39516 KB |
Output is correct |
3 |
Correct |
32 ms |
40024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
78752 KB |
Output is correct |
2 |
Correct |
387 ms |
111172 KB |
Output is correct |
3 |
Correct |
273 ms |
109840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
44336 KB |
Output is correct |
2 |
Correct |
255 ms |
106628 KB |
Output is correct |
3 |
Correct |
138 ms |
76872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
271 ms |
111584 KB |
Output is correct |
2 |
Correct |
526 ms |
177208 KB |
Output is correct |
3 |
Correct |
196 ms |
78772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
49824 KB |
Output is correct |
2 |
Correct |
560 ms |
175308 KB |
Output is correct |
3 |
Correct |
204 ms |
77160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
249 ms |
78904 KB |
Output is correct |
2 |
Runtime error |
1401 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
238 ms |
79108 KB |
Output is correct |
2 |
Runtime error |
1675 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
44736 KB |
Output is correct |
2 |
Runtime error |
1569 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |