#include <bits/stdc++.h>
#define ft first
#define sd second
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define PB push_back
#define i2 array<int,2>
#define pis pair<int,short>
using namespace std;
typedef long long ll;
const int N = 100100;
const int BIG = int(1e7) + 10;
const int con = (1 << 10) - 1;
const int oo = 2e9;
vector<pis> edg[BIG];
int n, a[N], nt[N];
ll ans = 0;
int get(int x) { return (x == nt[x] ? x : nt[x] = get(nt[x])); }
void calc(){
for (int i = 1; i <= n; i++){
if (i > 1 && a[i] == a[i - 1]) continue;
int x = a[i];
for (int it = x + 1; ; ){
int nov = lower_bound(a + 1, a + n + 1, it) - a;
if (nov == n + 1) break;
int nw = a[nov] % x;
it = a[nov];
edg[nw].PB({(nov << 10) + (i & con), (i >> 10)});
if (nw == 0)
it += x;
else it += x - nw;
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n;
for (int i = 1; i <= n; i++){
int x; cin >> x;
a[i] = x;
}
sort(a + 1, a + n + 1);
calc();
for (int i = 1; i <= n; i++)
nt[i] = i;
int siz = n;
for (int i = 0; i < BIG && siz > 1; i++)
for (pis _x : edg[i]){
int x = (_x.ft >> 10), y = (_x.sd << 10) + (_x.ft & con);
int xx = get(x), yy = get(y);
if (xx == yy) continue;
siz--;
ans += a[x] % a[y];
if (siz == 1) break;
nt[xx] = yy;
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
235384 KB |
Output is correct |
2 |
Correct |
149 ms |
237596 KB |
Output is correct |
3 |
Correct |
124 ms |
235512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
235256 KB |
Output is correct |
2 |
Correct |
142 ms |
235896 KB |
Output is correct |
3 |
Correct |
138 ms |
235384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
235364 KB |
Output is correct |
2 |
Correct |
138 ms |
235256 KB |
Output is correct |
3 |
Correct |
122 ms |
235384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
280 ms |
245096 KB |
Output is correct |
2 |
Correct |
561 ms |
272040 KB |
Output is correct |
3 |
Correct |
311 ms |
250568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
236880 KB |
Output is correct |
2 |
Correct |
323 ms |
257812 KB |
Output is correct |
3 |
Correct |
274 ms |
245428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
432 ms |
257204 KB |
Output is correct |
2 |
Correct |
713 ms |
286852 KB |
Output is correct |
3 |
Correct |
306 ms |
248788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
238680 KB |
Output is correct |
2 |
Correct |
620 ms |
281412 KB |
Output is correct |
3 |
Correct |
296 ms |
248808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
299 ms |
247800 KB |
Output is correct |
2 |
Correct |
2155 ms |
443444 KB |
Output is correct |
3 |
Correct |
331 ms |
250228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
290 ms |
247768 KB |
Output is correct |
2 |
Correct |
3114 ms |
534796 KB |
Output is correct |
3 |
Correct |
348 ms |
254224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
237560 KB |
Output is correct |
2 |
Correct |
3028 ms |
530648 KB |
Output is correct |
3 |
Correct |
316 ms |
250336 KB |
Output is correct |