#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll MOD = 1e9+7;
const int nax = 1e5+5;
const int MAX_VAL = 1e6;
double PI=3.14159265359;
int arx[8]={1,1,0,-1,-1,-1, 0, 1};
int ary[8]={0,1,1, 1, 0,-1,-1,-1};
typedef complex<int> Point;
#define X real()
#define Y imag()
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
vi parent(nax);
int find(int x)
{
if (x==parent[x]) return x;
return parent[x]=find(parent[x]);
}
void joinset(int x,int y)
{
x=find(x);
y=find(y);
parent[x]=y;
}
bool sameset(int x,int y)
{
return find(x)==find(y);
}
int main(){
optimise;
// setIO("walk");
int n;
cin>>n;
for (int i = 0; i < n; ++i)
{
parent[i]=i;
}
set<int> check;
for (int i = 0; i < n; ++i)
{
int x;
cin>>x;
check.insert(x);
}
vi tab;
for(auto u:check)
{
tab.pb(u);
}
int mx=tab[tab.size()-1];
vector<pair<ll,pair<int,int>>> mst;
for (int i = 0; i < n; ++i)
{
int cur=tab[i];
while(cur<=mx)
{
int r= 0;
if(cur == tab[i]) r = upper_bound(tab.begin(),tab.end(),cur)-tab.begin();
else r = lower_bound(tab.begin(),tab.end(),cur)-tab.begin();
if(r < n){
mst.pb({tab[r]%tab[i],{i,r}});
cur+=tab[i];
} else break;
}
}
sort(mst.begin(),mst.end());
ll ans=0;
for (int i = 0; i < mst.size(); ++i)
{
int x=mst[i].se.fi;
int y=mst[i].se.se;
int z=mst[i].fi;
if (sameset(x,y)) continue;
joinset(x,y);
ans+=z;
}cout << ans<<endl;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int i = 0; i < mst.size(); ++i)
| ~~^~~~~~~~~~~~
sirni.cpp: In function 'void setIO(std::string)':
sirni.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
980 KB |
Output is correct |
2 |
Runtime error |
125 ms |
89392 KB |
Execution killed with signal 8 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
1876 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
980 KB |
Output is correct |
2 |
Runtime error |
2 ms |
1488 KB |
Execution killed with signal 8 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
159 ms |
44852 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
21 ms |
8720 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
326 ms |
85120 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
57 ms |
16868 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
180 ms |
54192 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
200 ms |
70512 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
27 ms |
9504 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |