//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <bitset>
#include <set>
#include <queue>
#include <stack>
#include <assert.h>
#include <cmath>
#include <iomanip>
#include <random>
using namespace std;
void debug(){cout << endl;};
template<class T, class ...U> void debug(T a, U ... b){cout << a << " ", debug(b ...);};
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
};
#define ll long long
#define maxn 100005
#define maxc 10000005
#define mod 1000000007
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
bool rig[maxc], lef[maxc];
int dis[maxn], ind[maxc];
bool found[maxn];
priority_queue<pii, vector<pii>, greater<pii> > pq;
vector<int> a;
vector<int> from[maxn];
set<int> se;
int n;
void upd(int x, int p) {
auto checkbig = [&] (int i) {
auto it = se.lower_bound(i);
if (it != se.end()) {
int id = ind[*it];
if (a[id] - i < dis[id]) {
dis[id] = a[id] - i;
pq.push({dis[id], id});
}
}
};
for (int i = (maxc-1) - ((maxc-1) % x);i >= x;i -= x) {
if (!rig[i]) {
rig[i] = 1;
checkbig(i);
}
}
checkbig(x);
for (auto id:from[p]) {
if (x % a[id] < dis[id]) {
dis[id] = x % a[id];
pq.push({dis[id], id});
}
}
}
int main() {
io
cin >> n;
a.resize(n, 0);
for (int i = 0;i < n;i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
a.resize(int(unique(a.begin(), a.end()) - a.begin()));
n = a.size();
dis[0] = 0;
found[0] = 1;
for (int i = 1;i < n;i++) {
dis[i] = 1<<30, se.insert(a[i]);
ind[a[i]] = i;
int prv = 0;
for (int j = a[i];j < maxc;j += a[i]) {
int id = lower_bound(a.begin(), a.end(), j) - a.begin();
if (id < n && id != prv) {
from[id].push_back(i);
}
prv = id;
}
}
upd(a[0], 0);
int ans = 0;
while (pq.size()) {
pii cur = pq.top();pq.pop();
if (cur.ff != dis[cur.ss] || found[cur.ss]) continue;
ans += cur.ff;
found[cur.ss] = 1;
se.erase(a[cur.ss]);
upd(a[cur.ss], cur.ss);
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
14924 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
179 ms |
12552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
15564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
846 ms |
30008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
136 ms |
18500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1428 ms |
37948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
813 ms |
18248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
629 ms |
67460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
586 ms |
66292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
94 ms |
49716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |