#include <bits/stdc++.h>
//#define int long long
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000007;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 1e5 + 1;
int n;
vector <int> a;
vector <vector <pii>> edge;
struct TDSU{
vector <pii> save;
vector <int> lab;
TDSU(){}
TDSU(int _n){
lab.resize(_n, -1);
}
inline void resize(int _n){
lab.resize(_n, -1);
}
inline int find_set(int u){
if (lab[u] < 0) return u;
return lab[u] = find_set(lab[u]);
}
inline int find(int u){
if (lab[u] < 0) return u;
return find(lab[u]);
}
inline void merge(int u, int v){
if (lab[u] > lab[v]) swap(u, v);
save.pb({v, lab[v]});
lab[u] += lab[v];
lab[v] = u;
}
inline void undo(){
int v = save.back().fi;
int u = lab[v];
lab[v] = save.back().se;
lab[u] -= lab[v];
save.pop_back();
}
inline void clear(){
lab.clear();
save.clear();
}
inline void reset(int _n){
clear();
resize(_n);
}
inline void roll_back(int _n){
while (save.size() > _n) undo();
}
};
TDSU dsu;
void Init(){
cin >> n;
for (int i = 1; i <= n; ++i){
int x; cin >> x;
a.pb(x);
}
sort(a.begin(), a.end());
a.resize(unique(a.begin(), a.end()) - a.begin());
edge.resize(a.back() + 1);
for (int _ = 0; _ < a.size() - 1; ++_){
int i = a[_], j = 1;
while (i * j <= a.back()){
int k;
if (j == 1) k = upper_bound(a.begin(), a.end(), i) - a.begin();
else k = lower_bound(a.begin(), a.end(), i * j) - a.begin();
edge[a[k] % i].pb({_, k});
j = a[k] / i + 1;
}
}
dsu.resize(n);
ll ans = 0;
for (int i = 0; i <= a.back(); ++i){
for (auto e: edge[i]){
int u = e.fi, v = e.se;
int x = dsu.find_set(u), y = dsu.find_set(v);
if (x == y && dsu.lab[x] == -n){
cout << ans;
return;
}
else if (x != y){
ans += i;
dsu.merge(x, y);
}
}
}
cout << ans << "\n";
}
#define debu
#define taskname "test"
signed main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
#ifdef debug
freopen(taskname".out", "w", stdout);
#endif
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
timeout << 1000 * double(clock()) / CLOCKS_PER_SEC;
#ifndef debug
cerr << "\nTime elapsed: " << 1000 * double(clock()) / CLOCKS_PER_SEC << "ms\n";
#endif
}
}
Compilation message
sirni.cpp: In member function 'void TDSU::roll_back(int)':
sirni.cpp:74:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
74 | while (save.size() > _n) undo();
| ~~~~~~~~~~~~^~~~
sirni.cpp: In function 'void Init()':
sirni.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int _ = 0; _ < a.size() - 1; ++_){
| ~~^~~~~~~~~~~~~~
sirni.cpp: In function 'int main()':
sirni.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
235068 KB |
Output is correct |
2 |
Correct |
145 ms |
236692 KB |
Output is correct |
3 |
Correct |
112 ms |
234440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
144 ms |
235500 KB |
Output is correct |
3 |
Correct |
141 ms |
235312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
235212 KB |
Output is correct |
2 |
Correct |
141 ms |
234700 KB |
Output is correct |
3 |
Correct |
116 ms |
235268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
36036 KB |
Output is correct |
2 |
Correct |
426 ms |
61256 KB |
Output is correct |
3 |
Correct |
180 ms |
40576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
25784 KB |
Output is correct |
2 |
Correct |
186 ms |
46992 KB |
Output is correct |
3 |
Correct |
123 ms |
24088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
296 ms |
47252 KB |
Output is correct |
2 |
Correct |
523 ms |
76204 KB |
Output is correct |
3 |
Correct |
166 ms |
39672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
9328 KB |
Output is correct |
2 |
Correct |
472 ms |
71560 KB |
Output is correct |
3 |
Correct |
161 ms |
38596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
318 ms |
249872 KB |
Output is correct |
2 |
Correct |
2196 ms |
444604 KB |
Output is correct |
3 |
Correct |
289 ms |
252628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
296 ms |
250352 KB |
Output is correct |
2 |
Correct |
3084 ms |
536168 KB |
Output is correct |
3 |
Correct |
329 ms |
256520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
237968 KB |
Output is correct |
2 |
Correct |
2967 ms |
532048 KB |
Output is correct |
3 |
Correct |
178 ms |
40384 KB |
Output is correct |