#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 3e5+5;
const int MAXM = (int) 1e7+5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;
int p[MAXN];
int buy[MAXM];
vector<pii> edg[MAXM];
int par[MAXN];
int sz[MAXN];
int find_set(int p){
if(par[p] == p)
return p;
return par[p] = find_set(par[p]);
}
void merge(int a, int b){
if(sz[b] > sz[a])
swap(a, b);
par[b] = a;
sz[a] += sz[b];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
#ifdef Local
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
#endif
/*cout<<"100000"<<endl;
for(int i = 2; i < 1e7; i+=100)
cout<<i<<" ";
return 0;
*/
cin>>n;
memset(buy, -1, sizeof(buy));
for(int i = 0; i < n; i++){
cin>>p[i];
if(p[i] == 1){
cout<<0<<endl;
return 0;
}
}
sort(p, p + n);
n = (unique(p, p + n) - p);
for(int i = 0; i < n; i++){
buy[p[i]] = i;
par[i] = i;
sz[i] = 1;
}
for(int i = MAXM - 1; i > 0; i--){
if(buy[i - 1] == -1)
buy[i - 1] = buy[i];
}
for(int i = 0; i < n; i++){
int fir = buy[p[i] + 1];
if(fir != -1 && p[fir] < 2*p[i]){
assert(p[fir] - p[i] >= 0);
edg[p[fir] - p[i]].push_back({i, fir});
}
for(ll j = 2LL*p[i]; j < MAXM; j += p[i])
if(buy[j] != -1 && p[buy[j]] < j + p[i]){
assert(p[buy[j]] - j >= 0);
edg[p[buy[j]] - j].push_back({i, buy[j]});
}
}
ll ans = 0;
for(int i = 0; i < MAXM; i++){
for(int j = 0; j < edg[i].size(); j++){
pii &u = edg[i][j];
int a = find_set(u.ff), b = find_set(u.ss);
if(a != b){
ans += i;
merge(a, b);
}
}
}
assert(sz[find_set(0)] == n);
cout<<ans<<endl;
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:89:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(int j = 0; j < edg[i].size(); j++){
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
233 ms |
274668 KB |
Output is correct |
2 |
Correct |
286 ms |
276844 KB |
Output is correct |
3 |
Correct |
233 ms |
274668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
274668 KB |
Output is correct |
2 |
Correct |
572 ms |
275148 KB |
Output is correct |
3 |
Correct |
231 ms |
274540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
274540 KB |
Output is correct |
2 |
Correct |
231 ms |
274412 KB |
Output is correct |
3 |
Correct |
231 ms |
274668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
472 ms |
284980 KB |
Output is correct |
2 |
Correct |
1006 ms |
312264 KB |
Output is correct |
3 |
Correct |
529 ms |
290276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
271 ms |
276076 KB |
Output is correct |
2 |
Correct |
858 ms |
296664 KB |
Output is correct |
3 |
Correct |
580 ms |
284992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
795 ms |
297376 KB |
Output is correct |
2 |
Correct |
1234 ms |
326820 KB |
Output is correct |
3 |
Correct |
518 ms |
288340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
503 ms |
278076 KB |
Output is correct |
2 |
Correct |
1318 ms |
321520 KB |
Output is correct |
3 |
Correct |
511 ms |
287968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
390 ms |
287492 KB |
Output is correct |
2 |
Correct |
2019 ms |
483896 KB |
Output is correct |
3 |
Correct |
430 ms |
289896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
392 ms |
287324 KB |
Output is correct |
2 |
Correct |
4010 ms |
574504 KB |
Output is correct |
3 |
Correct |
466 ms |
293892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
265 ms |
276972 KB |
Output is correct |
2 |
Correct |
4396 ms |
569996 KB |
Output is correct |
3 |
Correct |
538 ms |
289760 KB |
Output is correct |