This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// https://oj.uz/problem/view/COCI17_sirni
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define vb vector<bool>
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
vi cards(n);
for (int i = 0; i < n; i++) {
cin >> cards[i];
}
sort(cards.begin(), cards.end());
int next = 0, curr = 0;
vb visited(n, false);
ll total = 0;
vi dists(n, INT_MAX);
for (int i = 0; i < n-1; i++) {
curr = next;
next = 0;
visited[curr] = true;
int current_value = cards[curr];
for (int v = 0; v < curr; v++) {
if (visited[v]) continue;
dists[v] = min(dists[v], current_value % cards[v]);
if (dists[v] < dists[next]) next = v;
}
for (int v = curr+1; v < n; v++) {
if (visited[v]) continue;
dists[v] = min(dists[v], cards[v] % current_value);
if (dists[v] < dists[next]) next = v;
}
total += (ll) dists[next];
}
cout << total << "\n";
}
# | 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... |