結果

問題 No.1917 LCMST
ユーザー suzuken_w
提出日時 2022-04-29 23:02:14
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 361 ms / 4,000 ms
コード長 2,212 bytes
コンパイル時間 2,881 ms
コンパイル使用メモリ 228,492 KB
最終ジャッジ日時 2025-01-28 23:45:08
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
// const ll mod=998244353;
const ll mod=1000000007;
const vector<int> dy={-1,0,1,0},dx={0,-1,0,1};
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
class UF{
public:
	vector<int> par,num;
	int find(int v){
		return (par[v]==v)? v: (par[v]=find(par[v]));
	}
  explicit UF(){}
	explicit UF(int N):par(N),num(N,1){
		iota(all(par),0);
	}
  void init(int N){
     par.assign(N,0);
     num.assign(N,1);
     iota(all(par),0);
  }
	void unite(int u,int v){
		u=find(u),v=find(v);
		if(u==v)return;
		if(num[u]<num[v])swap(u,v);
		num[u]+=num[v];
		par[v]=u;
	}
	bool same(int u,int v){
		return find(u)==find(v);
	}
	bool ispar(int v){
		return v==find(v);
	}
	int size(int v){
		return num[find(v)];
	}
};
int main(){
    int n;
    cin>>n;
    V<int> a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    sort(all(a));
    ll ans=0;
    UF uf(n);
    for(int i=1;i<n;i++){
        if(a[i-1]==a[i]){
            uf.unite(i-1,i);
            ans+=a[i];
        }
    }
    ll sz=a.back();
    V<int> num(sz+1,-1);
    for(int i=0;i<n;i++){
        if(num[a[i]]!=-1)continue;
        num[a[i]]=i;
    }
    V<tuple<ll,int,int>> e;
    for(ll i=1;i<=sz;i++){
        ll v=-1;
        for(ll j=i;j<=sz;j+=i){
            if(num[j]!=-1){
                if(v==-1){
                    v=j;
                }else{
                    e.emplace_back(v*j/i,num[v],num[j]);
                }
            }
        }
    }
    sort(all(e));
    for(auto &[v,l,r]:e){
        if(uf.same(l,r))continue;
        ans+=v;
        uf.unite(l,r);
    }
    cout<<ans<<"\n";
}
0