#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
signed main(){
// ios_base::sync_with_stdio(false);
// cin.tie(nullptr);
int n; cin >> n;
vector <int> a(n);
for ( auto &u: a ) cin >> u;
sort(all(a));
a.resize(unique(all(a)) - a.begin());
n = a.size();
int N = *max_element(all(a)) + 1;
vector <int> fa(N);
vector <ar<int,2>> nxt(n, {N, -1});
vector <vector<int>> C(n);
set <int> st;
for ( int i = 0; i < n; i++ ){
C[i].pb(i);
st.insert(a[i]);
fa[a[i]] = i;
}
int ans = 0;
do{
int c = 0;
for ( auto &u: C ){
c += !u.empty();
}
if ( c == 1 ) break;
for ( int i = 0; i < n; i++ ){
for ( auto &u: C[i] ){
st.erase(a[u]);
}
assert(!st.empty());
for ( auto &p: C[i] ){
int u = a[p];
for ( int j = u; j < N; j += u ){
auto it = st.lower_bound(j);
if ( it != st.end() ){
chmin(nxt[i], ar<int,2> {*it % j, fa[*it]});
chmin(nxt[fa[*it]], ar<int,2> {*it % j, i});
}
}
}
for ( auto &u: C[i] ){
st.insert(a[u]);
}
}
auto upd = [&](int i, int j){
for ( auto &u: C[j] ){
fa[a[u]] = i;
C[i].pb(u);
} C[j].clear();
};
for ( int i = 0; i < n; i++ ){
if ( C[i].empty() || nxt[i][1] == -1 ){
nxt[i] = {N, -1};
continue;
}
auto [w, j] = nxt[i];
nxt[i] = {N, -1};
if ( !C[j].empty() ){
ans += w;
if ( C[j].size() < C[i].size() ){
upd(i, j);
} else upd(j, i);
}
}
} while ( true );
cout << ans;
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
78684 KB |
Output is correct |
2 |
Correct |
283 ms |
78480 KB |
Output is correct |
3 |
Correct |
17 ms |
78428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
2007 ms |
78656 KB |
Output is correct |
3 |
Correct |
23 ms |
78556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
78848 KB |
Output is correct |
2 |
Correct |
12 ms |
78680 KB |
Output is correct |
3 |
Correct |
14 ms |
78680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1044 ms |
21976 KB |
Output is correct |
2 |
Correct |
2348 ms |
20968 KB |
Output is correct |
3 |
Correct |
809 ms |
19332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
10064 KB |
Output is correct |
2 |
Correct |
712 ms |
10364 KB |
Output is correct |
3 |
Correct |
681 ms |
15892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2019 ms |
22088 KB |
Output is correct |
2 |
Correct |
2753 ms |
21220 KB |
Output is correct |
3 |
Correct |
943 ms |
21460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
7048 KB |
Output is correct |
2 |
Correct |
2174 ms |
18944 KB |
Output is correct |
3 |
Correct |
886 ms |
19716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1723 ms |
93236 KB |
Output is correct |
2 |
Execution timed out |
5023 ms |
91736 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1272 ms |
92584 KB |
Output is correct |
2 |
Execution timed out |
5053 ms |
89668 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
240 ms |
82036 KB |
Output is correct |
2 |
Execution timed out |
5100 ms |
88556 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |