【19.05%】【codeforces 731F】 Video Cards

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.markdown

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.less

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.ide

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

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

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

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 3, 15 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.code

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.排序

【題解】

用前綴和來搞。
先把這個序列排序一下;
枚舉每個數X。
而後獲取
(j-1)*x和j*x
j是一個整數;
若是(j-1)*x..j*x-1 這個區間範圍裏面有數字。那麼確定都只能變成(j-1)*x,由於只能減少。而再多減不知足最大。
那麼貢獻的答案就是(j-1)*x..j*x-1這個數字範圍裏面的數字個數cnt;
(用前綴和獲取);
temp+=cnt*(j-1)*x
取最大的temp;
可能會出現大量重複數據。
加個flag判重。否則會T。(第38個點);
long long 什麼的都開吧。
ci

#include <cstdio>
#include <algorithm>

using namespace std;

int pre[6000000] = { 0 };
int a[400000];
int n;
long long ans = 0;
bool flag[290000] = { 0 };

bool cmp(int a, int b)
{
    return a < b;
}   

int main()
{
    //freopen("F:\\rush.txt", "r", stdin);
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]),pre[a[i]]++;
    sort(a + 1, a + 1 + n,cmp);
    for (int i = 1; i <= 550000; i++)
        pre[i] = pre[i]+pre[i - 1];
    for (int i = 1; i <= n; i++)
        if (!flag[a[i]])
    {
            flag[a[i]] = true;
        int now = a[i];
        long long temp2 = 0;
        for (int j = 2; j*a[i] <= a[n] + 200000; j++)
        {
            int prenow = (j - 1)*a[i];
            if (prenow > a[n])
                break;
            long long nextnow = j*a[i];
            long long k = pre[nextnow-1] - pre[prenow - 1];
            long long temp =k * prenow;
            temp2 += temp;
        }
        if (temp2 > ans)
            ans = temp2;
    }
    printf("%I64d\n", ans);
    return 0;
}
相關文章
相關標籤/搜索