# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1275151 | vk3601h | Sirni (COCI17_sirni) | C++20 | 0 ms | 0 KiB |
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
vector<int> P(N);
for (int i = 0; i < N; ++i) {
cin >> P[i];
}
sort(P.begin(), P.end());
set<int> visited;
visited.insert(P[0]);
long long total = 0;
for (int i = 1; i < N; ++i) {
int x = P[i];
bool found = false;
// Check for divisors in visited
int limit = sqrt(x);
for (int d = 1; d <= limit; ++d) {
if (x % d == 0) {
if (visited.find(d) != visited.end()) {
found = true;
break;
}
if (d != x / d && visited.find(x / d) != visited.end()) {
found = true;
break;
}
}
}
if (found) {
visited.insert(x);
continue;
}
// Find the largest element in visited less than x
auto it = visited.upper_bound(x);
int min_weight = INT_MAX;
if (it != visited.begin()) {
--it;
int candidate = *it;
min_weight = x % candidate;
}
// Check intervals [x/(k+1), x/k]
for (int k = 1; k <= limit; ++k) {
int L = x / (k + 1);
int R = x / k;
if (L > R) continue;
auto it = visited.upper_bound(R);
if (it != visited.begin()) {
--it;
if (*it >= L) {
int candidate = *it;
int weight = x % candidate;
if (weight < min_weight) {
min_weight = weight;
}
}
}
}
total += min_weight;
visited.insert(x);
}
cout << total << endl;
return 0;
}