#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(a==inf||b==inf)break;
if(merg(a,b))ans+=(v[b]%v[a]);
t[a].update();
if(t[a].get()!=inf)pq.push({v[t[a].get()]%v[a],a});
}
cout<<ans;
return 0;
}
/*
if we can just connect same value together
*/
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++){
| ~^~~~~~~~~
sirni.cpp:94:6: warning: unused variable 'cnt' [-Wunused-variable]
94 | int cnt=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
9816 KB |
Output is correct |
2 |
Correct |
130 ms |
16316 KB |
Output is correct |
3 |
Correct |
107 ms |
10340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
10072 KB |
Output is correct |
2 |
Correct |
127 ms |
12304 KB |
Output is correct |
3 |
Correct |
107 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
10028 KB |
Output is correct |
2 |
Correct |
74 ms |
9816 KB |
Output is correct |
3 |
Correct |
107 ms |
10136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5047 ms |
61712 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5036 ms |
17004 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5098 ms |
113868 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5047 ms |
28372 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5012 ms |
69616 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5047 ms |
68544 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5027 ms |
19860 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |