Submission #371237

#TimeUsernameProblemLanguageResultExecution timeMemory
371237FatihSolakSirni (COCI17_sirni)C++17
140 / 140
2659 ms634100 KiB
#include <bits/stdc++.h> #define N 100005 #define M 10000005 using namespace std; int par[N]; int nxt[M]; int pos[M]; vector<pair<int,int>> v[M]; int find(int a){ if(a == par[a])return a; return par[a] = find(par[a]); } bool unite(int a,int b){ a = find(a); b = find(b); if(a == b)return 0; par[b] = a; return 1; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n; cin >> n; int maxi = 0; for(int i=1;i<=n;i++){ int a; cin >> a; pos[a] = i; maxi = max(maxi,a); } vector<int> nums; for(int i=maxi;i>=0;i--){ if(pos[i]){ nxt[i] = i; nums.push_back(i); } else nxt[i] = nxt[i+1]; } for(auto u:nums){ int before = -1; for(int i=u;i<=maxi;i+=u){ int p = nxt[i+(i==u)]; if(p && p != before) v[p%u].push_back({pos[u],pos[p]}); before = p; } } for(int i=0;i<n;i++){ par[i] = i; } long long ans = 0; for(int i=0;i<=maxi;i++){ for(auto u:v[i]){ if(unite(u.first,u.second))ans+=i; } } cout << ans; }
#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...