#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
#include <cstdlib>
#include <cstdint>
using namespace std;
#define ll long long
#define f first
//#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
#define int long long
#define double long double
const int mxn=1e5,inf=1e9,minf=-1e9;
vector<int>v;
struct num{
priority_queue<pii,vector<pii>,greater<pii>>pq;
int x;
vector<int>wall,o;
void init(){
int cur=0,g=x;
while(cur<v.size()){
if(wall.empty()||cur!=wall.back())wall.pb(cur),o.pb(cur);
while(cur<v.size()&&v[cur]<g)cur++;
g+=x;
}
for(int i=0;i<wall.size();i++)pq.push({v[wall[i]]%x,i});
o.pb(v.size());
}
void update(){
if(pq.empty())return;
pii ans=pq.top();
wall[ans.s]++;
pq.pop();
if(wall[ans.s]<o[ans.s+1])pq.push({v[wall[ans.s]]%x,ans.s});
}
int get(){
if(pq.empty())return inf;
pii ans=pq.top();
return wall[ans.s];
}
}t[mxn+10];
int p[mxn+10];
int find(int u){return (p[u]==u)?u:p[u]=find(p[u]);}
bool merg(int a,int b){
a=find(a),b=find(b);
if(a==b)return 0;
p[a]=b;
return 1;
}
int32_t main(){
fastio
int n;cin>>n;
for(int i=0;i<n;i++){
int a;cin>>a;
v.pb(a);
}
for(int i=0;i<n;i++)p[i]=i;
sort(all(v));
int ans=0;
v.erase(unique(all(v)),v.end());
priority_queue<pii,vector<pii>,greater<pii>>pq;
for(int i=0;i<v.size();i++){
t[i].x=v[i];
t[i].init();
t[i].update();
pq.push({v[t[i].get()]%v[i],i});
}
int cnt=0;
while(!pq.empty()){
int a=pq.top().s,b=t[a].get();
pq.pop();
if(merg(a,b))ans+=(v[b]%v[a]),cnt++;
if(cnt==n-1)break;
t[a].update();
if(t[a].get()!=inf)pq.push({v[t[a].get()]%v[a],a});
}
cout<<ans;
return 0;
}
/*
*/
Compilation message
sirni.cpp: In member function 'void num::init()':
sirni.cpp:47:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | while(cur<v.size()){
| ~~~^~~~~~~~~
sirni.cpp:49:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | while(cur<v.size()&&v[cur]<g)cur++;
| ~~~^~~~~~~~~
sirni.cpp:52:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i=0;i<wall.size();i++)pq.push({v[wall[i]]%x,i});
| ~^~~~~~~~~~~~
sirni.cpp: In function 'int32_t main()':
sirni.cpp:88:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for(int i=0;i<v.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
122 ms |
16308 KB |
Output is correct |
3 |
Correct |
5 ms |
10328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
10088 KB |
Output is correct |
2 |
Correct |
126 ms |
12116 KB |
Output is correct |
3 |
Correct |
108 ms |
10320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
73 ms |
9828 KB |
Output is correct |
3 |
Correct |
4 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5060 ms |
61280 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5047 ms |
16976 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5060 ms |
113048 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5052 ms |
28112 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5048 ms |
68616 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5033 ms |
68032 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5015 ms |
19656 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |