/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define ui unsigned int
#define f first
#define s second
#define ins insert
#define pb push_back
#define mp make_pair
#define ln '\n'
#define int ll
#define pii pair<int , int>
#define INF LLONG_MAX
#define vv(a) vector<a>
#define pp(a, b) pair<a, b>
#define pq(a) priority_queue<a>
#define qq(a) queue<a>
#define ss(a) set<a>
#define mss(a) multiset<a>
#define mm(a, b) map<a, b>
#define mmm(a , b) multimap<a , b>
#define sz(x) (x).size()
#define all(x) (x).begin() , (x).end()
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vector<int> color(1005 , 0);
vector<vector<int>> g(1005);
int n;
bool DFS(int src){
color[src] = 1;
for(auto to : g[src]){
if(color[to] == 0){
DFS(to);
}
}
for(int i=0 ; i<n ; i++){
if(color[i] == 0){
return false;
}
}
return true;
}
void init(){
for(int i=0 ; i<n ; i++){
color[i] = 0;
}
}
void solve(){
cin >> n;
vv(int) a(n);
for(int i=0 ; i<n ; i++){
cin >> a[i];
}
if(n == 1){
cout << 0;
return;
}
vector<pair<int , pii>> maybe;
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<n ; j++){
if(i != j){
maybe.pb({min(a[i]%a[j] , a[j]%a[i]) , {i , j}});
}
}
}
sort(all(maybe));
int ans = 0;
for(int i=0 ; i<maybe.size() ; i++){
init();
bool check = DFS(0);
if(check == true){
cout << ans;
return;
}
else{
init();
DFS(maybe[i].s.f);
if(color[maybe[i].s.s] == 0){
ans += maybe[i].f;
g[maybe[i].s.f].pb(maybe[i].s.s);
g[maybe[i].s.s].pb(maybe[i].s.f);
}
}
}
}
signed main(){
fastio
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
Compilation message
sirni.cpp: In function 'void solve()':
sirni.cpp:85:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int i=0 ; i<maybe.size() ; i++){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
341 ms |
25228 KB |
Output is correct |
2 |
Correct |
726 ms |
25072 KB |
Output is correct |
3 |
Correct |
244 ms |
25148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
295 ms |
25224 KB |
Output is correct |
2 |
Correct |
447 ms |
25148 KB |
Output is correct |
3 |
Correct |
153 ms |
25100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
373 ms |
25124 KB |
Output is correct |
2 |
Correct |
408 ms |
25148 KB |
Output is correct |
3 |
Correct |
284 ms |
25024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
580 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
594 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
768 ms |
786436 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
591 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
622 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
630 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
597 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |