我们发现对于一段ai,ai/x是相同的,我们用差分数组记录个数,乘上贡献就可以了
复杂度有一个比较重要的东西
#include<bits/stdc++.h>
#define N 1000050
#define LL long long
#define inf 1000000000000000
using namespace std;
int a[N],Max; LL s,ans;
int n,d[N];
int read(){
int cnt=0,f=1; char ch=0;
while(!isdigit(ch)){ch=getchar(); if(ch=='-') f=-1;}
while(isdigit(ch))cnt=cnt*10+(ch-'0'),ch=getchar();
return cnt * f;
}
int main(){
n = read();
for(int i=1;i<=n;i++){
a[i] = read();
s += (LL)a[i];
Max = max(Max, a[i]);
d[a[i]]++;
}
for(int i=3;i<=Max;i++) d[i] += d[i-1];
for(int x=2;x<=Max;x++){
LL tmp = 0;
for(int l=x;l<=Max;l+=x){
int r = l + x - 1; if(r > Max) r = Max;
int val = l/x;
tmp += (LL)(d[r]-d[l-1]) * (LL)val;
}
ans = max(ans, tmp * (LL)(x-1));
} printf("%lld",s-ans); return 0;
}