이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <vector>
#include <algorithm>
#include <iostream>
#include <utility>
#include <queue>
#include <set>
using namespace std;
int main(){
int N;
cin >> N;
set<int> s;
int a;
for(int i = 0; i < N; ++i){
cin >> a;
s.insert(-a);
}
N = s.size();
vector<int> v;
for(auto x: s)v.emplace_back(-x);
int m = 1e7 + 2;
vector<vector<int>> p(m);
for(int j = 0; j < N; ++j){
for(int i = v[j]; i < m; i += v[j]){
p[i].push_back(j);
}
}
int c = v[0];
long long ans = 0;
vector<vector<pair<int,int>>> adj(N);
for(int i = 0; i < N; ++i){
auto x = v[i];
while(c > x){
--c;
for(auto j: p[c]){
adj[j].emplace_back(make_pair(i-1, v[i-1]-c));
adj[i-1].emplace_back(make_pair(j, v[i-1]-c));
}
}
if(c == x && p[x].size() == 1)--c;
while(c > 0 && p[c].empty())--c;
for(auto j: p[c]){
adj[j].emplace_back(make_pair(i, x-c));
adj[i].emplace_back(make_pair(j, x-c));
}
}
vector<int> vis(N, 0);
vis[N-1] = 1;
priority_queue<pair<int,int>> pq;
for(auto [n, d]: adj[N-1])pq.push(make_pair(-d, n));
while(!pq.empty()){
auto [d, n] = pq.top();
pq.pop();
if(vis[n])continue;
vis[n] = 1;
ans -= d;
for(auto [nn, dd]: adj[n]){
if(vis[nn])continue;
pq.push(make_pair(-dd, nn));
}
}
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |