답안 #1057693

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1057693 2024-08-14T03:33:07 Z vjudge1 Sirni (COCI17_sirni) C++17
0 / 140
2104 ms 54696 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];
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));
  for (int x : z) for (int y = 2 * x; y <= z.back(); y += x) {
    auto it = lower_bound(all(z), y);
    if (it == end(z)) continue;
    y = ((*it) - y) / x * x + y;
    e.pb({x, (*it), (*it) % x});
  }
  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;
    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();
}

Compilation message

sirni.cpp: In function 'int main()':
sirni.cpp:57:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |   if (fopen("A.inp", "r")) freopen("A.inp", "r", stdin);
      |                            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1067 ms 18228 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 92 ms 2560 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2104 ms 54696 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 290 ms 8004 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1322 ms 29232 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1121 ms 29580 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 162 ms 4880 KB Output isn't correct
2 Halted 0 ms 0 KB -