답안 #856151

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
856151 2023-10-02T17:51:37 Z vjudge1 Sirni (COCI17_sirni) C++17
0 / 140
5000 ms 49316 KB
#ifndef Local
    #pragma GCC optimize("O3,unroll-loops")
    #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define lim 100001
#define till 1000001
using namespace std;
const int mod=1000000007ll;

int parent[lim];
int find(int i){
    if(parent[i]==i)return i;
    return parent[i]=find(parent[i]);
}
void unite(int i,int j){
    int x=parent[i],y=parent[j];
    parent[x]=y;
}

vector<int>all[till];

int mody(int i,int j){
    return max(i,j)%min(i,j);
}

void solve(){
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++){
        parent[i]=i;
        cin>>a[i];
    }
    sort(a,a+n);
    int ans=0;
    int uni[n];
    memset(uni,0,sizeof(uni));
    vector<int>opp;
    for(int i=n-1;0<=i;i--){
        if(uni[i]==2)continue;
        int to1=-1,to2=-1;
        //cerr<<i<<" "<<find(i)<<"::\n";
        for(int j=i-1;0<=j;j--){
            if(find(i)!=find(j)&&uni[j]!=2){
                //cerr<<j<<" "<<find(j)<<"\n";
                if(to1==-1){
                    to1=j;
                }else if(to2==-1||mody(a[i],a[j])<mody(a[i],a[to2])){
                    to2=j;
                    if(mody(a[i],a[to2])<mody(a[i],a[to1])){
                        swap(to1,to2);
                    }
                }
            }
        }
        if(to1!=-1){
            ans+=mody(a[i],a[to1]);
            opp.pb(mody(a[i],a[to1]));
            uni[i]++;
            uni[to1]++;
            //cerr<<"unite "<<i<<" "<<find(i)<<" "<<to1<<" "<<find(to1)<<"\n";
            unite(i,to1);
        }
        if(to2!=-1&&uni[i]!=2&&find(i)!=find(to2)){
            ans+=mody(a[i],a[to2]);
            opp.pb(mody(a[i],a[to2]));
            uni[i]++;
            uni[to2]++;
            //cerr<<"unite "<<i<<" "<<find(i)<<" "<<to2<<" "<<find(to2)<<"\n";
            unite(i,to2);
        }
        /*
        cerr<<to1<<" "<<to2<<"\n";
        for(int i:uni){
            cerr<<i<<" ";
        }cerr<<"\n";
        */
    }
    sort(opp.begin(),opp.end());
    int k=0,didnt[2];
    for(int i=0;i<n;i++){
        if(uni[i]==1)didnt[k++]=i;
    }
        //cerr<<didnt[0]<<" "<<didnt[1]<<"\n";
    if(mody(a[didnt[0]],a[didnt[1]])<opp.back()){
        ans-=opp.back();
        ans+=mody(a[didnt[0]],a[didnt[1]]);
    }
    cout<<ans<<"\n";
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
#ifdef Local  
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    int t=1;
    //cin>>t;
    while (t--)
    {
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 24156 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 25 ms 48208 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 23900 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5041 ms 26720 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 720 ms 48940 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5092 ms 26472 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5019 ms 25416 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5043 ms 26640 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5036 ms 26688 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1391 ms 49316 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -