分析
对于一个确定的 ,答案一定是可以取到前
个最大的值,所以只需要排序,再维护一个前缀和。最后时间复杂度为
。
代码
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 1e5 +10;
LL A[N],sum[N],n,X,Y,ans;
bool cmp(LL x,LL y) {return x > y;}
int main()
{
scanf("%lld",&n);
for(int i = 1;i <= n;i++) scanf("%lld",&A[i]);
sort(A+1,A+1+n,cmp);
scanf("%lld%lld",&X,&Y);
for(int i = 1;i <= n;i++) sum[i] = sum[i-1] + A[i];
for(int i = 1;i <= X;i++)
{
for(int j = 1;j <= Y;j++) {
ans += sum[i*j];
}
}
cout << ans << endl;
}

京公网安备 11010502036488号