This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
constexpr ll big = 1e18 + 18;
constexpr int maxn = 10e5 + 5;
int n;
int p[maxn];
ll dist[maxn];
unordered_set<int> notdone;
ll weight(int a, int b) { return min(p[a] % p[b], p[b] % p[a]); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> p[i];
dist[i] = weight(0, i);
notdone.emplace(i);
}
notdone.erase(0);
ll COST = 0;
dist[0] = 0;
while (!notdone.empty()) {
ll mindist = big;
int minode = -1;
for (int node : notdone) {
if (dist[node] < mindist) {
mindist = node;
minode = node;
}
}
notdone.erase(minode);
COST += dist[minode];
dist[minode] = 0;
for (int nbr : notdone) {
dist[nbr] = min(dist[nbr], weight(minode, nbr));
}
}
cout << COST;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |