#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 |
9 ms |
39512 KB |
Output is correct |
2 |
Correct |
250 ms |
39528 KB |
Output is correct |
3 |
Correct |
14 ms |
39516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
604 KB |
Output is correct |
2 |
Correct |
1912 ms |
39592 KB |
Output is correct |
3 |
Correct |
15 ms |
39512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
39512 KB |
Output is correct |
2 |
Correct |
8 ms |
39512 KB |
Output is correct |
3 |
Correct |
9 ms |
39540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
960 ms |
15988 KB |
Output is correct |
2 |
Correct |
2234 ms |
15040 KB |
Output is correct |
3 |
Correct |
715 ms |
13664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
6092 KB |
Output is correct |
2 |
Correct |
657 ms |
5720 KB |
Output is correct |
3 |
Correct |
652 ms |
12244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1911 ms |
15928 KB |
Output is correct |
2 |
Correct |
2464 ms |
15504 KB |
Output is correct |
3 |
Correct |
862 ms |
15680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
202 ms |
5456 KB |
Output is correct |
2 |
Correct |
2090 ms |
13460 KB |
Output is correct |
3 |
Correct |
946 ms |
14448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1484 ms |
51768 KB |
Output is correct |
2 |
Execution timed out |
5026 ms |
50772 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1004 ms |
51744 KB |
Output is correct |
2 |
Execution timed out |
5039 ms |
48908 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
42176 KB |
Output is correct |
2 |
Execution timed out |
5056 ms |
47756 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |