Submission #1057701

# Submission time Handle Problem Language Result Execution time Memory
1057701 2024-08-14T03:46:25 Z vjudge1 Sirni (COCI17_sirni) C++17
56 / 140
3426 ms 107044 KB
// 027 072 207 270 702 720
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define fo(i, a, b) for (int i = (a), b_ = (b); i <= b_; ++i)
#define fod(i, a, b) for (int i = (a), b_ = (b); i >= b_; --i)
#define rep(i, n) fo(i, 1, n)
#define per(i, n) fod(i, n, 1)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
const int N = 1e6+5, M = 1e7+5;

int n, a[N], nxt[N];
map<int, int> root;

struct edge {
  int u, v, c;
  bool operator < (const edge &o) const {
    return c < o.c;
  }
};

vector<edge> e;
vector<int> z;

inline int find(int u) { return (u == root[u] ? u : root[u] = find(root[u])); }

void sol() {
  cin >> n;
  rep(i, n) cin >> a[i], z.pb(a[i]);
  sort(a+1, a+n+1);
  sort(all(z)); z.resize(unique(all(z))-begin(z));
  
  int j = 0;
  rep(i, z.back()) {
    while (z[j] < i) j++;
    nxt[i] = z[j];
  }
  
  for (int x : z) for (int y = x; y <= z.back(); y += x) {
    int p = nxt[(y==x?y+1:y)];
    if (!p) break;
    if (y + x < p) y = (p - y) / x * x + y;
    e.pb({x, p, p - y});
  }
  // dbg(z);
  for (int x : z) root[x] = x;
  sort(all(e));
  ll ans = 0;
  for (edge ee : e) {
    int u = ee.u, v = ee.v, c = ee.c;
    // dbg(u, v, c);
    u = find(u), v = find(v);
    if (u != v) {
      root[u] = v;
      ans += c*1LL;
    }
  }
  cout << ans;
}

signed main() {
  cin.tie(0) -> sync_with_stdio(0);
  // if (fopen("A.inp", "r")) freopen("A.inp", "r", stdin);
  int tc = 1, test = 0; // cin >> tc;
  while (++test <= tc) sol();
}
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 16472 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2648 KB Output is correct
2 Runtime error 7 ms 16304 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 7 ms 16472 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1183 ms 23488 KB Output is correct
2 Correct 2907 ms 57912 KB Output is correct
3 Correct 1096 ms 33112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 96 ms 8704 KB Output is correct
2 Correct 913 ms 56888 KB Output is correct
3 Correct 1014 ms 23096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2146 ms 58172 KB Output is correct
2 Correct 3426 ms 106036 KB Output is correct
3 Correct 1112 ms 33092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 320 ms 12112 KB Output is correct
2 Correct 3351 ms 107044 KB Output is correct
3 Correct 1033 ms 33852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 22 ms 17360 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 22 ms 17372 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 16732 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -