【codeforces 731F】【前缀和 分块求和 好题】F. Video Cards

传送门:F. Video Cards

描述:

F. Video Cards
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated.

There are n video cards in the shop, the power of the i-th video card is equal to integer value ai. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced.

Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of video cards in the shop.

The second line contains n integers a1a2, ..., an (1 ≤ ai ≤ 200 000) — powers of video cards.

Output

The only line of the output should contain one integer value — the maximum possible total power of video cards working together.

Examples
input
4
3 2 15 9
output
27
input
4
8 2 2 7
output
18
Note

In the first sample, it would be optimal to buy video cards with powers 315 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3 + 1 + 15 + 9 = 28.

In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8 + 2 + 2 + 6 = 18.


题意:

给一个n长度的序列,问从序列中找一个数,把不小于它的数变成它或者它的倍数,并使得这些数的和最大


思路:

序列中的数最大为200000 ,我们可以枚举i(1<=i<=200000)作为base,然后枚举i的倍数j分段去累加就好了,类似于素数筛的思想,再加上前缀和处理一下

复杂度:
O(nlogn)

代码:
#include 
#define ll __int64
#define rep(i,k,n) for(int i=k;i<=n;i++)
using  namespace  std;

template void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('\n');
}

const int N=2e5+10;
int cnt[N],n, m=2e5;
ll ans, tmp;

int  main(){
  read(n);
  int x;
  rep(i, 1, n)read(x), cnt[x]++;
  rep(i, 1, m)cnt[i] += cnt[i-1];
  rep(i, 1, m){
    if(cnt[i]-cnt[i-1]){
      tmp=0;
      for(int j=0; j<=m; j+=i)tmp += (ll)j*(cnt[min(j+i-1, m)] - cnt[j-1]);
      ans = max(ans, tmp);
    }
  }
  print(ans);
  return 0;
}



你可能感兴趣的:(online,judge,Codeforces,------思维题,------数学基础与思维题)