#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;
template <class T>
using VV = vector<vector<T>>;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using VL = vector<long long>;
using VVL = vector<vector<long long>>;
using VC = vector<char>;
using VVC = vector<vector<char>>;
using VB = vector<bool>;
using VVB = vector<vector<bool>>;
using VD = vector<double>;
using VVD = vector<vector<double>>;
using PII = pair<int, int>;
using PLL = pair<long long, long long>;
using PIL = pair<int, long long>;
using PLI = pair<long long, int>;
using VPII = vector<pair<int, int>>;
using VPLL = vector<pair<long long, long long>>;
#define LINE '\n'
#define SPACE ' '
#define PB push_back
#define FOR(i, a, b) for (int i = (a); i < (int(b)); i++)
#define FORE(i, a, b) for (int i = (a); i <= (int)((b)); i++)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define sq(a) ((a) * (a))
#define sz(x) ((int)s.size())
// zero indexed
template <class T>
struct segtree
{
const T def = 0;
int n;
vector<T> seg;
segtree(int _size) : n(_size), seg(2 * _size, def) {}
T merge(T a, T b)
{
return max(a, b);
}
void update(int pos, T val)
{
for (seg[pos += n] = val; pos /= 2;)
{
seg[pos] = merge(seg[pos * 2], seg[pos * 2 + 1]);
}
}
T query(int l, int r)
{ // get [l, r]
T a = def, b = def;
for (l += n, r += n + 1; l < r; l /= 2, r /= 2)
{
if (l % 2)
a = merge(a, seg[l++]);
if (r % 2)
b = merge(b, seg[--r]);
}
return merge(a, b);
}
};
const ll MOD = 1e9 + 7;
template <class T>
void maxi(T &a, T b)
{
a = max(a, b);
}
template <class T>
void mini(T &a, T b)
{
a = min(a, b);
}
template <class T>
void addi(T &a, T b)
{
a = (a + b) % MOD;
}
template <class T>
void subi(T &a, T b)
{
a = (a - b + MOD) % MOD;
}
template <class T>
T add(T a, T b)
{
return (a + b) % MOD;
}
template <class T>
T sub(T a, T b)
{
return (a - b + MOD) % MOD;
}
template <class T>
T mul(T a, T b)
{
return ((a % MOD) * (b % MOD)) % MOD;
}
ll binpow(ll a, ll b, ll mod)
{
ll res = 1;
while (b > 0)
{
if (b & 1)
{
res = (res * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return res;
}
struct DSU
{
VI par;
VI size;
DSU(int _n) : par(_n), size(_n, 1)
{
iota(ALL(par), 0);
}
bool merge(int x, int y)
{
x = find(x);
y = find(y);
if (x == y)
return false;
if (size[y] > size[x])
swap(x, y);
size[x] += size[y];
par[y] = x;
return true;
}
int find(int x)
{
return (par[x] == x ? x : par[x] = find(par[x]));
}
bool connected(int x, int y)
{
return find(x) == find(y);
}
void reset()
{
FOR(i, 0, par.size())
par[i] = i,
size[i] = 1;
}
};
const int INF = 1e9;
const int MAX_N = 1e3 + 4;
const ll LG = 1e8;
void solve()
{
int n;
cin >> n;
VI p(n);
FOR(i, 0, n)
{
cin >> p[i];
}
sort(ALL(p));
p.erase(unique(ALL(p)), p.end());
n = p.size();
int mx = p.back();
VI next(mx + 1, -1);
FOR(i, 0, n)
{
next[p[i]] = i;
}
for (int i = mx - 1; i >= 0; i--)
{
if (next[i] == -1)
{
next[i] = next[i + 1];
}
}
VPLL g;
FOR(i, 0, n - 1)
{
g.PB({p[i+1]%p[i], ll(i+1) * LG + i + 2});
for (int at = 2 * p[i]; at <= mx; at += p[i])
{
int tmp = next[at];
g.PB({p[tmp] % p[i], ll(i+1) * LG + tmp + 1});
}
}
DSU dsu(n + 3);
sort(ALL(g));
int ans = 0;
for(auto [cost, code] : g) {
int a = code / LG;
int b = code % LG;
assert(a < n + 3 & a >= 0);
assert(b < n + 3 & a >= 0);
//cerr << a << SPACE << b << LINE;
if(dsu.merge(a, b)) ans += cost;
}
cout << ans << LINE;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while (t--)
solve();
}
Compilation message
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from sirni.cpp:1:
sirni.cpp: In function 'void solve()':
sirni.cpp:241:18: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
241 | assert(a < n + 3 & a >= 0);
| ~~^~~~~~~
sirni.cpp:242:18: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
242 | assert(b < n + 3 & a >= 0);
| ~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
39768 KB |
Output is correct |
2 |
Correct |
223 ms |
105112 KB |
Output is correct |
3 |
Correct |
26 ms |
39900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Runtime error |
746 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
39772 KB |
Output is correct |
2 |
Correct |
24 ms |
39516 KB |
Output is correct |
3 |
Correct |
26 ms |
39764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
37564 KB |
Output is correct |
2 |
Correct |
549 ms |
70944 KB |
Output is correct |
3 |
Correct |
252 ms |
70524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
8652 KB |
Output is correct |
2 |
Correct |
320 ms |
70484 KB |
Output is correct |
3 |
Correct |
141 ms |
35768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
304 ms |
70560 KB |
Output is correct |
2 |
Correct |
726 ms |
136100 KB |
Output is correct |
3 |
Correct |
214 ms |
37560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
9680 KB |
Output is correct |
2 |
Correct |
733 ms |
136036 KB |
Output is correct |
3 |
Correct |
205 ms |
37564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
214 ms |
72800 KB |
Output is correct |
2 |
Runtime error |
812 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
231 ms |
72892 KB |
Output is correct |
2 |
Runtime error |
802 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
43724 KB |
Output is correct |
2 |
Runtime error |
862 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |