結果

問題 No.3270 No Coprime Cycles
ユーザー Today03
提出日時 2025-09-12 22:51:22
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,833 bytes
コンパイル時間 3,678 ms
コンパイル使用メモリ 291,328 KB
実行使用メモリ 52,608 KB
最終ジャッジ日時 2025-09-12 23:43:45
合計ジャッジ時間 22,104 ms
ジャッジサーバーID
(参考情報)
judge6 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 8 WA * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for(ll i=0; i<(ll)(n); i++)

int lob(const auto& v, auto x) { return lower_bound(ALL(v),x)-v.begin(); }
bool chmax(auto& a, auto b) { return a<b ? a=b, true : false; }
bool chmin(auto& a, auto b) { return a>b ? a=b, true : false; }
using ll=long long; const int INF=1e9+10; const ll INFL=4e18;
using VI=vector<int>; using VVI=vector<VI>; using VL=vector<ll>; using VVL=vector<VL>;
using PL=pair<ll,ll>; using VP=vector<PL>; using WG=vector<vector<pair<int,ll>>>;

#ifdef LOCAL
#include "./debug.hpp"
#else
#define debug(...)
#define print_line
#endif


//----------------------------------------------------------

void solve() {
    ll N; cin>>N;
    VL A(N); REP(i,N) cin>>A[i];

    int mx=1e6;
    VI lpf(mx+1,INF);
    for(int i=2; i<=mx; i++) {
        if(lpf[i]==INF) {
            for(int j=i; j<=mx; j+=i) lpf[j]=i;
        }
    }
    sort(ALL(A));

    VVL muls(mx+1);
    REP(i,N) {
        ll a=A[i];
        while(a>1) {
            ll p=lpf[a];
            while(a%p==0) a/=p;
            muls[p].push_back(i);
        }
    }

    ll sum=reduce(ALL(A));
    ll ans=INFL;

    multiset<ll> ms(ALL(A));
    for(int p=2; p<=mx; p++) if(lpf[p]==p) {
        ll res=sum;
        ll mx=0;

        for(int i: muls[p]) {
            res-=A[i];

            ms.erase(ms.find(A[i]));
            ll a=A[i];
            while(a%p==0) a/=p;

            if(a>1) res+=a;
            if(a>1) chmax(mx,a);
        }
        if(ms.size()) chmax(mx,*ms.rbegin());
        for(int i: muls[p]) ms.insert(A[i]);

        res-=mx;
        chmin(ans,res);
    }

    cout<<ans<<endl;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    //cout<<fixed<<setprecision(15);
    int T=1; //cin>>T;
    while(T--) solve();
}
0