Submission #139796

#TimeUsernameProblemLanguageResultExecution timeMemory
139796MinnakhmetovSirni (COCI17_sirni)C++14
42 / 140
1348 ms786436 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define all(aaa) aaa.begin(), aaa.end() const int N = 1e6 + 5; int a[N], q[N]; struct E { int a, b, w; }; int findSet(int v) { return q[v] < 0 ? v : q[v] = findSet(q[v]); } bool connect(int a, int b) { a = findSet(a); b = findSet(b); if (a == b) return false; if (q[a] > q[b]) swap(a, b); q[a] += q[b]; q[b] = a; return true; } signed main() { #ifdef HOME freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; fill(q, q + n, -1); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); vector<E> edges; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { edges.push_back({i, j, a[j] % a[i] }); } } sort(all(edges), [](E x, E y) { return x.w < y.w; }); ll ans = 0; for (E e : edges) { if (connect(e.a, e.b)) { ans += e.w; } } cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...