#include<bits/stdc++.h>
//#define int long long
#define pii pair<int,int>
#define fi first
#define se second
#pragma GCC tarGet ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC tarGet("avx,avx2,fma")
using namespace std;
using ll = long long;
const int mod = 1e9+7;
const int maxN = 1e5+5;
const int N = 1e7;
const ll infty = 1e16;
void InputFile()
{
freopen("test.inp","r",stdin);
//freopen("SHOPPING.out","w",stdout);
//freopen("test.out","r",stdin);
}
struct TEdge
{
int a,b,c;
};
bool FA(TEdge a,TEdge b)
{
return a.c < b.c;
}
vector<TEdge> vc;
int n;
int a[maxN];
int marc[N+1];
int lab[N+1];
int maxi = 0;
int Findset(int u)
{
if(lab[u] < 0) return u;
return lab[u] = Findset(lab[u]);
}
ll res = 0;
int cnt_e = 0;
bool Merge(int u,int v,int val)
{
u = Findset(u);
v = Findset(v);
if(u == v)
return false;
if(lab[u] > lab[v]) swap(u,v);
res += val;
cnt_e++;
lab[u] += lab[v];
lab[v] = u;
return true;
}
void Read()
{
cin >> n;
for(int i = 1;i <= n;i++)
{
cin >> a[i];
marc[a[i]] = 1;
maxi = max(maxi,a[i]);
lab[a[i]] = -1;
}
if(a[1] == 1)
{
cout << 0 <<'\n';
return;
}
sort(a + 1,a + n + 1);
for(int i = 1;i <= maxi;i++)
{
marc[i] += marc[i-1];
}
for(int i = 1;i <= n;i++)
{
if(a[i] == a[i-1]) {cnt_e++;continue;}
for(int j = a[i];j <= maxi;j += a[i])
{
if(marc[j+a[1]-1] - marc[j-1])
{
int x = j;
if(x == a[i])
x++;
int v = lower_bound(a + 1,a + n + 1,x) - a;
if(v > n) continue;
v = a[v];
int val = v % a[i];
vc.push_back({a[i],v,val});
}
}
}
//cout <<"Dark";return;
sort(vc.begin(),vc.end(), FA);
for(TEdge x : vc)
{
Merge(x.a,x.b,x.c);
}
//cout << cnt_e;return;
cout << res + (ll)(n - 1 - cnt_e) * (ll)a[1];
}
void Solve()
{
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//InputFile();
//int sub_type;
//cin >> sub_type;
//Sieve();
//Prepare();
int test;
//cin >> test;
test = 1;
while(test--)
//for(int prc = 1; prc <= test; prc++)
{
Read();
Solve();
//Debug();
}
}
Compilation message
sirni.cpp:6: warning: ignoring '#pragma GCC tarGet' [-Wunknown-pragmas]
6 | #pragma GCC tarGet ("avx2")
|
sirni.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
7 | #pragma GCC optimization ("O3")
|
sirni.cpp:8: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
8 | #pragma GCC optimization ("unroll-loops")
|
sirni.cpp:10: warning: ignoring '#pragma GCC tarGet' [-Wunknown-pragmas]
10 | #pragma GCC tarGet("avx,avx2,fma")
|
sirni.cpp: In function 'void InputFile()':
sirni.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen("test.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
43236 KB |
Output is correct |
2 |
Correct |
78 ms |
44364 KB |
Output is correct |
3 |
Correct |
32 ms |
43336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
556 KB |
Output is correct |
2 |
Correct |
269 ms |
40036 KB |
Output is correct |
3 |
Correct |
29 ms |
43300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
43500 KB |
Output is correct |
2 |
Correct |
26 ms |
41848 KB |
Output is correct |
3 |
Correct |
29 ms |
43512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
21608 KB |
Output is correct |
2 |
Correct |
315 ms |
33920 KB |
Output is correct |
3 |
Correct |
79 ms |
15436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
9168 KB |
Output is correct |
2 |
Correct |
191 ms |
20548 KB |
Output is correct |
3 |
Correct |
151 ms |
17680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
339 ms |
33924 KB |
Output is correct |
2 |
Correct |
207 ms |
33952 KB |
Output is correct |
3 |
Correct |
101 ms |
15488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
8548 KB |
Output is correct |
2 |
Correct |
186 ms |
21584 KB |
Output is correct |
3 |
Correct |
79 ms |
15508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
92192 KB |
Output is correct |
2 |
Correct |
450 ms |
92092 KB |
Output is correct |
3 |
Correct |
265 ms |
92128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
86024 KB |
Output is correct |
2 |
Correct |
654 ms |
103436 KB |
Output is correct |
3 |
Correct |
133 ms |
82932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
76960 KB |
Output is correct |
2 |
Correct |
1504 ms |
170052 KB |
Output is correct |
3 |
Correct |
101 ms |
15440 KB |
Output is correct |