// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;}
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#pragma GCC optimize("Ofast")
#define debug(...) ((void)0)
#endif
struct EEK {
int w, u, v;
EEK (int w, int u, int v) : w(w), u(u), v(v) {}
const bool operator < (const EEK &oth) const {
return w < oth.w;
}
};
struct DSU {
int n; vector<int> f;
DSU (int nn) : n(nn), f(nn) { iota(AI(f), 0); }
int F(int u) { return u == f[u] ? u : f[u] = F(f[u]); }
bool M(int u, int v) {
u = F(u); v = F(v);
if(u == v) return false;
f[v] = u;
return true;
}
};
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int N; cin >> N;
vector<int> P(N); for(int &i: P) cin >> i;
sort(AI(P));
P.erase(unique(AI(P)), end(P));
N = P.size();
vector<EEK> e;
for(int i = 0; i < N-1; ++i){
for(int j = P[i]; j <= P.back(); j += P[i]){
int p = lower_bound(begin(P)+i+1, end(P), j) - begin(P);
int r = P[p] / P[i];
e.pb(P[p] - P[i] * r, i, p);
j = P[i] * r;
}
}
sort(AI(e));
DSU dsu(N);
int ans = 0;
int cnt = N;
for(auto [w, u, v]: e){
if(dsu.M(u, v)) ans += w, --cnt;
if(cnt == 1) break;
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
25 ms |
3468 KB |
Output is correct |
3 |
Correct |
3 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
9 ms |
1224 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
13096 KB |
Output is correct |
2 |
Correct |
628 ms |
50016 KB |
Output is correct |
3 |
Correct |
235 ms |
25368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
3520 KB |
Output is correct |
2 |
Correct |
346 ms |
49980 KB |
Output is correct |
3 |
Correct |
215 ms |
13164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
416 ms |
50020 KB |
Output is correct |
2 |
Correct |
810 ms |
99308 KB |
Output is correct |
3 |
Correct |
220 ms |
25424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
6716 KB |
Output is correct |
2 |
Correct |
764 ms |
99400 KB |
Output is correct |
3 |
Correct |
209 ms |
25368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
245 ms |
25388 KB |
Output is correct |
2 |
Correct |
3631 ms |
394792 KB |
Output is correct |
3 |
Correct |
261 ms |
25356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
203 ms |
25336 KB |
Output is correct |
2 |
Execution timed out |
5119 ms |
394844 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
3516 KB |
Output is correct |
2 |
Execution timed out |
5124 ms |
394712 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |