题目链接:
题意:
给你n个数,叫从中选一个数x,然后将其他比它大的数通过减去某个数变成x的倍数,然后将这所有数相加,求它的最大值为多少?
数据范围:
1≤n,ai≤2∗105
题解:
我们通过枚举所有x(相同的我只枚举了一次),然后去枚举x的倍数k*x,利用前缀和求出[k*x,(k+1)*x-1]有多少数?然后计算就可以了!
时间复杂度为
O(n∗log(n))
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
const int MOD = 1000000007;
const int N = 2E5+15;
const int maxn = 400+ 14;
const int mm=100000+15;
const int letter = 130;
const int INF = 1e9;
const double pi=acos(-1.0);
const double eps=1e-8;
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,a[N],num[N],vis[N];
int main(){
scanf("%d",&n);
int max1=0;
for(int i=0;i<n;i++) {
scanf("%d",a+i);
max1=max(max1,a[i]);
vis[a[i]]=1;
num[a[i]]++;
}
for(int i=1;i<N;i++) num[i]+=num[i-1];
sort(a,a+n);
LL ans=0;
for(int x=1;x<=max1;x++){
if(!vis[x]) continue;
int l=x,r=x+x;
LL ps=0;
while(l<=max1){
r=min(r,max1+1);
ps+=1ll*l*1ll*(num[r-1]-num[l-1]);
l+=x,r+=x;
}
ans=max(ans,ps);
}
printf("%I64d\n",ans);
return 0;
}