結果

問題 No.1917 LCMST
ユーザー momoharamomohara
提出日時 2022-04-29 23:04:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,196 bytes
コンパイル時間 2,389 ms
コンパイル使用メモリ 215,224 KB
実行使用メモリ 258,096 KB
最終ジャッジ日時 2023-09-11 14:32:16
合計ジャッジ時間 60,213 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 144 ms
108,316 KB
testcase_01 AC 220 ms
129,360 KB
testcase_02 AC 27 ms
67,076 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 73 ms
82,916 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 3,761 ms
258,096 KB
testcase_30 AC 3,628 ms
257,968 KB
testcase_31 AC 3,699 ms
258,012 KB
testcase_32 AC 3,743 ms
257,952 KB
testcase_33 AC 3,658 ms
257,960 KB
testcase_34 AC 133 ms
73,996 KB
testcase_35 AC 137 ms
74,064 KB
testcase_36 AC 133 ms
74,192 KB
testcase_37 AC 3,494 ms
242,624 KB
testcase_38 AC 3,451 ms
233,796 KB
testcase_39 AC 3,383 ms
228,188 KB
testcase_40 AC 3,437 ms
224,876 KB
testcase_41 AC 3,472 ms
222,048 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<ll, ll>;
using tp = tuple<ll, ll, ll>;

template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;

#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
#define rep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); ++i)
#define rep2(i, m, n) for(ll i = (ll)(n)-1; i >= (ll)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)

constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
// constexpr long long MOD = (ll)1e9 + 7;
constexpr long long MOD = 998244353LL;
static const ld pi = 3.141592653589793L;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

template <class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

//グラフ関連
struct Edge {
    int to, rev;
    ll cap;
    Edge(int _to, int _rev, ll _cap) : to(_to), rev(_rev), cap(_cap) {}
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, int from, int to, ll cap, bool revFlag, ll revCap) {
    G[from].push_back(Edge(to, (int)G[to].size(), cap));
    if(revFlag)
        G[to].push_back(Edge(from, (int)G[from].size() - 1, revCap));
}

class UnionFind {
    vector<int> data;
    int num;

  public:
    UnionFind(int size) : data(size, -1), num(size) {}
    bool unionSet(int x, int y) {
        x = root(x);
        y = root(y);
        if(x != y) {
            if(data[y] < data[x])
                swap(x, y);
            data[x] += data[y];
            data[y] = x;
            num--;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
    int numSet() {
        return num;
    }
};

void solve() {
    ll n;
    cin >> n;
    vec<ll> a(n);
    REP(i, n) {
        cin >> a[i];
    }

    ll ma = 2020202;
    vec<ll> num(ma, 0);

    vvec<int> vs(ma);
    ll ans = 0;
    sort(all(a));

    vec<ll> b;
    REP(i, n) {
        num[a[i]]++;
        if(num[a[i]] >= 2) {
            ans += a[i];
            continue;
        }

        int k = b.size();
        b.push_back(a[i]);
        for(int j = 1; j * a[i] < ma; j++) {
            vs[j * a[i]].push_back(k);
        }
    }

    UnionFind uni(b.size());
    REP(i, ma) {
        if(vs[i].size() <= 1)
            continue;
        rep(j, 1, vs[i].size()) {
            if(uni.unionSet(vs[i][0], vs[i][j])) {
                ans += i;
            }
        }
        if(uni.size(0) == b.size())
            break;
    }
    cout << ans << en;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // cout << fixed << setprecision(10);

    // ll t;
    // cin >> t;
    // REP(i, t - 1) {
    //     solve();
    // }

    solve();

    return 0;
}
0