#include <bits/stdc++.h>
//#include "bits_stdc++.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i)
#define RFOR(i, x, n) for (ll i =x; i>=n; --i)
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<pi, pi> piii;
ll p;
vector<ll> dis;
pii edges[10000005];
bool compare(pii a , pii b)
{
return a.s < b.s;
}
ll par[100005];
ll find(ll x) {return ((x==par[x])?x:par[x] = find(par[x]));}
void unite(ll x, ll y) {if ((x=find(x)) != (y=find(y))) par[x] = y;}
int main()
{
ll n, ans=0;
input(n);
dis.resize(n);
for (ll i=0; i<n; ++i) {input(dis[i]);}
discretize(dis);
ll now = 0;
for (ll i=0; i<dis.size(); ++i)
{
ll last = -1;
for (ll j=0; j<=dis.back(); j+=dis[i])
{
ll nxt = lb(all(dis), j)-dis.begin();
if (nxt == last) continue;
if (nxt < ll(dis.size())) edges[now++] = mp(mp(i, nxt), min(dis[i]%dis[nxt], dis[nxt]%dis[i]));
if (nxt > 0 ) edges[now++] = mp(mp(i, nxt-1), min(dis[i]%dis[nxt-1], dis[nxt-1]%dis[i]));
last = nxt;
}
}
sort(edges, edges+now, compare);
for (ll i=0; i<dis.size(); ++i) par[i] = i;
for (ll i=0; i<now; ++i)
{
pi & u = edges[i];
if (find(u.f.f)==find(u.f.s)) continue;
ans += u.s;
unite(u.f.f, u.f.s);
}
print(ans, '\n');
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:57:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (ll i=0; i<dis.size(); ++i)
| ~^~~~~~~~~~~
sirni.cpp:70:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (ll i=0; i<dis.size(); ++i) par[i] = i;
| ~^~~~~~~~~~~
sirni.cpp:73:23: error: invalid initialization of reference of type 'pi&' {aka 'std::pair<long long int, long long int>&'} from expression of type 'pii' {aka 'std::pair<std::pair<long long int, long long int>, long long int>'}
73 | pi & u = edges[i];
| ~~~~~~~^
sirni.cpp:6:11: error: request for member 'first' in 'u.std::pair<long long int, long long int>::first', which is of non-class type 'long long int'
6 | #define f first
| ^~~~~
sirni.cpp:74:16: note: in expansion of macro 'f'
74 | if (find(u.f.f)==find(u.f.s)) continue;
| ^
sirni.cpp:7:11: error: request for member 'second' in 'u.std::pair<long long int, long long int>::first', which is of non-class type 'long long int'
7 | #define s second
| ^~~~~~
sirni.cpp:74:29: note: in expansion of macro 's'
74 | if (find(u.f.f)==find(u.f.s)) continue;
| ^
sirni.cpp:6:11: error: request for member 'first' in 'u.std::pair<long long int, long long int>::first', which is of non-class type 'long long int'
6 | #define f first
| ^~~~~
sirni.cpp:76:13: note: in expansion of macro 'f'
76 | unite(u.f.f, u.f.s);
| ^
sirni.cpp:7:11: error: request for member 'second' in 'u.std::pair<long long int, long long int>::first', which is of non-class type 'long long int'
7 | #define s second
| ^~~~~~
sirni.cpp:76:20: note: in expansion of macro 's'
76 | unite(u.f.f, u.f.s);
| ^
sirni.cpp:12:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | #define input(x) scanf("%lld", &x);
| ~~~~~^~~~~~~~~~~~
sirni.cpp:52:2: note: in expansion of macro 'input'
52 | input(n);
| ^~~~~
sirni.cpp:12:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | #define input(x) scanf("%lld", &x);
| ~~~~~^~~~~~~~~~~~
sirni.cpp:54:26: note: in expansion of macro 'input'
54 | for (ll i=0; i<n; ++i) {input(dis[i]);}
| ^~~~~