/*-----------------------------
Author : TgX.2
11Ti - K28 - CHV
-----------------------------*/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i += 1)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; i -= 1)
#define FORC(i, a, b, c) for (int i = (a), _b = (b), _c = (c); i <= _b; i += _c)
#define fi first
#define se second
#define pb push_back
#define len(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define _ << " " <<
#define __ << "\n"
#define ______________TgX______________ main()
// #define int long long
#define intmax 1e9
#define intmin -1e9
#define llongmax 1e18
#define llongmin -1e18
#define memo(a, val) memset((a), (val), sizeof((a)))
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<typename T1, typename T2>
bool mini(T1 &a, T2 b)
{if (a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2>
bool maxi(T1 &a, T2 b)
{if (a < b) a = b; else return 0; return 1;}
/*-----------------------------*/
const int maxn = 1e5 + 7;
const int maxval = 1e7 + 7;
int n, a[maxn], pos[maxval];
vector<pair<int, int>> candidate[maxval];
vector<pair<int, int>> g[maxn];
void prim() {
vector<int> dis(maxn, intmax);
dis[1] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.push({0, 1});
int ans = 0;
while(!q.empty()) {
pair<int, int> top = q.top(); q.pop();
int u = top.se, cost = top.fi;
if (cost != dis[u]) continue;
ans += cost;
dis[u] = intmin;
for(const pair<int, int> &val : g[u]) {
int v = val.fi, w = val.se;
if (dis[v] > w) {
dis[v] = w;
q.push({dis[v], v});
}
}
}
cout << ans;
}
void process() {
cin >> n;
FOR(i, 1, n) cin >> a[i];
sort(a + 1, a + 1 + n);
FOR(i, 1, n) pos[a[i]] = i;
FORD(i, a[n], 1) if (pos[i] == 0) pos[i] = pos[i + 1];
FOR(i, 2, n) {
int val = a[i] % a[i - 1];
g[i].pb({i - 1, val});
g[i - 1].pb({i, val});
}
FOR(i, 1, n) {
int cur = a[i] + a[i];
while(cur <= a[n]) {
int posi = pos[cur];
if (posi == 0) continue;
int val = a[posi] % a[i];
g[i].pb({posi, val});
g[posi].pb({i, val});
cur = a[posi] / a[i] * a[i];
cur += a[i];
}
}
prim();
}
/*-----------------------------*/
______________TgX______________ {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen("temp.inp", "r")) {
freopen("temp.inp", "r", stdin);
freopen("temp.out", "w", stdout);
}
process();
}
/*==============================+
|INPUT |
--------------------------------|
================================+
|OUTPUT |
--------------------------------|
===============================*/
Compilation message
sirni.cpp:21:41: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
21 | #define ______________TgX______________ main()
| ^~~~
sirni.cpp:113:1: note: in expansion of macro '______________TgX______________'
113 | ______________TgX______________ {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp: In function 'int main()':
sirni.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | freopen("temp.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen("temp.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
277300 KB |
Output is correct |
2 |
Correct |
131 ms |
281416 KB |
Output is correct |
3 |
Correct |
123 ms |
277368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
238164 KB |
Output is correct |
2 |
Correct |
130 ms |
279004 KB |
Output is correct |
3 |
Correct |
125 ms |
277436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
277192 KB |
Output is correct |
2 |
Correct |
119 ms |
277072 KB |
Output is correct |
3 |
Correct |
137 ms |
277332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
269388 KB |
Output is correct |
2 |
Correct |
525 ms |
367428 KB |
Output is correct |
3 |
Correct |
239 ms |
291196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
245876 KB |
Output is correct |
2 |
Runtime error |
856 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
292 ms |
307784 KB |
Output is correct |
2 |
Correct |
483 ms |
431180 KB |
Output is correct |
3 |
Correct |
201 ms |
279036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
249068 KB |
Output is correct |
2 |
Correct |
402 ms |
396884 KB |
Output is correct |
3 |
Correct |
214 ms |
286040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
246 ms |
309784 KB |
Output is correct |
2 |
Runtime error |
1044 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
234 ms |
308424 KB |
Output is correct |
2 |
Runtime error |
945 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
282316 KB |
Output is correct |
2 |
Runtime error |
1064 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |