Submission #956269

#TimeUsernameProblemLanguageResultExecution timeMemory
956269Trisanu_DasSirni (COCI17_sirni)C++17
42 / 140
1012 ms786432 KiB
#include "bits/stdc++.h" using namespace std; int main(){ ios::sync_with_stdio(0);cin.tie(0); int n; cin>>n; vector<int> a(n),parent(n); for(auto &i:a)cin>>i; for(int i=0;i<n;i++){ parent[i] =i; } auto find = [&](int x){ while(x != parent[x])x = parent[x]; return x; }; auto unite = [&](int a,int b){ a = find(a); b = find(b); if(a != b){ parent[b] = a; } }; vector<pair<int,pair<int,int>>> edges; for(int i=0;i<n;i++){ for(int j=0;j<i;j++){ edges.push_back({min(a[i]%a[j],a[j]%a[i]),{i,j}}); } } long long res = 0; sort(edges.begin(),edges.end()); for(auto edge:edges){ int x = edge.second.first,b = edge.second.second; if(find(x) != find(b)){ res += edge.first; unite(x,b); } } cout << res <<'\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...