#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));}
int last = (ptr[x+1]!=INT_MAX-1 ? x+1+ptr[x+1] : -1);
FOR(j,x*2,1e7+2,x) {
if (ptr[j]==INT_MAX-1) {break;}
if (j+ptr[j]==last) {e.pop_back();}
e.pb(Edge(i,rev[j+ptr[j]],(j+ptr[j])%j));
last = j+ptr[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 |
18 ms |
40024 KB |
Output is correct |
2 |
Correct |
63 ms |
43716 KB |
Output is correct |
3 |
Correct |
22 ms |
40028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
39768 KB |
Output is correct |
2 |
Correct |
530 ms |
40640 KB |
Output is correct |
3 |
Correct |
22 ms |
40024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
39772 KB |
Output is correct |
2 |
Correct |
20 ms |
39476 KB |
Output is correct |
3 |
Correct |
19 ms |
39768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
121 ms |
63208 KB |
Output is correct |
2 |
Correct |
346 ms |
110732 KB |
Output is correct |
3 |
Correct |
170 ms |
76336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
44492 KB |
Output is correct |
2 |
Correct |
208 ms |
105944 KB |
Output is correct |
3 |
Correct |
118 ms |
59988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
274 ms |
111212 KB |
Output is correct |
2 |
Correct |
477 ms |
176456 KB |
Output is correct |
3 |
Correct |
180 ms |
77996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
49576 KB |
Output is correct |
2 |
Correct |
522 ms |
174796 KB |
Output is correct |
3 |
Correct |
152 ms |
76360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
78256 KB |
Output is correct |
2 |
Correct |
2466 ms |
571336 KB |
Output is correct |
3 |
Correct |
199 ms |
79008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
78236 KB |
Output is correct |
2 |
Correct |
3524 ms |
569524 KB |
Output is correct |
3 |
Correct |
288 ms |
78772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
44484 KB |
Output is correct |
2 |
Correct |
3330 ms |
569184 KB |
Output is correct |
3 |
Correct |
166 ms |
78300 KB |
Output is correct |