Codeforces Round #376 (Div. 2) 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,枚舉1~200000作爲基數,j作爲i的倍數對200000進行分段求和,最後求出最大值。

值得注意的是,複雜度爲∑1/i≈O(nlogn),而通過前綴和可以得出在某一數段內有多少個數,直接用j乘數的個數,即可得出該段最大和。

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
const long long N=200005;
const long long mod=1e9+7;
const double PI=acos(-1.0);

int a[N];
int s[N];

int main() {
    int n,m=2e5;
    while (cin>>n) {
        memset(s, 0, sizeof(s));
        for (int i=0; i<n; i++) {
            scanf("%d",&a[i]);
            s[a[i]]++;
        }
        if (n==1) {
            cout<<a[0]<<endl;
            continue;
        }
        for (int i=1; i<=m; i++) {
            s[i]+=s[i-1];
        }
        long long ans,temp;
        ans=0;
        for (int i=1; i<=m; i++) {
            if (s[i]-s[i-1]) {
                temp=0;
                
                for (int j=0; j<=m; j+=i) {
                    temp+=(long long)j*(s[min(j+i-1, m)]-s[j-1]);
                }
                
                ans=max(ans, temp);
            }
            
        }
        cout<<ans<<endl;
    }
    return 0;
}


代碼如下:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章