#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;}
ll nxt[10000005];
int main()
{
ll n, ans=0;
input(n);
dis.resize(n);
for (ll i=0; i<n; ++i) {input(dis[i]);}
discretize(dis);
for (ll i=0; i<=dis.back(); ++i) nxt[i] = -1;
for (ll i=0; i<dis.size(); ++i) nxt[dis[i]] = i;
for (ll i=dis.back(); i>=0; --i) if (nxt[i]==-1) nxt[i] = nxt[i+1];
ll now = 0;
for (ll i=0; i<ll(dis.size()) - 1; ++i)
{
ll last = -1;
edges[now++] =mp( mp(i, i+1), dis[i+1]%dis[i]);
for (ll j=dis[i]; j<=dis.back(); j+=dis[i])
{
ll nxtnow = nxt[j];
edges[now++] = mp( mp(i, nxtnow), dis[nxtnow]%dis[i]);
}
}
sort(edges, edges+now, compare);
for (ll i=0; i<dis.size(); ++i) par[i] = i;
for (pii u: edges)
{
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:59: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]
59 | for (ll i=0; i<dis.size(); ++i) nxt[dis[i]] = i;
| ~^~~~~~~~~~~
sirni.cpp:64:6: warning: unused variable 'last' [-Wunused-variable]
64 | ll last = -1;
| ^~~~
sirni.cpp:73: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]
73 | for (ll i=0; i<dis.size(); ++i) par[i] = i;
| ~^~~~~~~~~~~
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:2: note: in expansion of macro 'input'
54 | 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:56:26: note: in expansion of macro 'input'
56 | for (ll i=0; i<n; ++i) {input(dis[i]);}
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
79184 KB |
Output is correct |
2 |
Correct |
327 ms |
143532 KB |
Output is correct |
3 |
Correct |
72 ms |
79332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
1108 KB |
Output is correct |
2 |
Runtime error |
364 ms |
635040 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
79188 KB |
Output is correct |
2 |
Correct |
71 ms |
78940 KB |
Output is correct |
3 |
Correct |
70 ms |
79184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
37804 KB |
Output is correct |
2 |
Correct |
512 ms |
110724 KB |
Output is correct |
3 |
Correct |
271 ms |
63568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
13392 KB |
Output is correct |
2 |
Correct |
320 ms |
62800 KB |
Output is correct |
3 |
Correct |
179 ms |
33612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
299 ms |
67668 KB |
Output is correct |
2 |
Correct |
664 ms |
150524 KB |
Output is correct |
3 |
Correct |
242 ms |
58144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
12632 KB |
Output is correct |
2 |
Correct |
723 ms |
155728 KB |
Output is correct |
3 |
Correct |
253 ms |
60244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
253 ms |
115104 KB |
Output is correct |
2 |
Runtime error |
369 ms |
637780 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
292 ms |
127056 KB |
Output is correct |
2 |
Runtime error |
387 ms |
637520 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
84308 KB |
Output is correct |
2 |
Runtime error |
392 ms |
637520 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |