#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;
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.PB({(nov << 10) + (i & con), (i >> 10)});
if (nw == 0)
it += x;
else it += x - nw;
}
}
}
bool cmp(pis _x, pis _y){
int x1 = (_x.ft >> 10), x2 = (_x.sd << 10) + (_x.ft & con);
int y1 = (_y.ft >> 10), y2 = (_y.sd << 10) + (_y.ft & con);
return (a[x1] % a[x2]) < (a[y1] % a[y2]);
}
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();
sort(all(edg), cmp);
for (int i = 1; i <= n; i++)
nt[i] = i;
for (pis _x : edg){
int x = (_x.ft >> 10), y = (_x.sd << 10) + (_x.ft & con);
int xx = get(x), yy = get(y);
if (xx == yy) continue;
ans += a[x] % a[y];
nt[xx] = yy;
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
74 ms |
2552 KB |
Output is correct |
3 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
23 ms |
1024 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
429 ms |
9172 KB |
Output is correct |
2 |
Correct |
1680 ms |
33728 KB |
Output is correct |
3 |
Correct |
615 ms |
17236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
2552 KB |
Output is correct |
2 |
Correct |
883 ms |
33728 KB |
Output is correct |
3 |
Correct |
434 ms |
9304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
991 ms |
33856 KB |
Output is correct |
2 |
Correct |
2193 ms |
66552 KB |
Output is correct |
3 |
Correct |
562 ms |
17236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
137 ms |
4716 KB |
Output is correct |
2 |
Correct |
2061 ms |
66584 KB |
Output is correct |
3 |
Correct |
539 ms |
17236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
547 ms |
17240 KB |
Output is correct |
2 |
Execution timed out |
5068 ms |
263640 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
532 ms |
17276 KB |
Output is correct |
2 |
Execution timed out |
5077 ms |
263512 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
2672 KB |
Output is correct |
2 |
Execution timed out |
5087 ms |
263604 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |