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 a1, a2, ..., 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
  1. 4
    3 2 15 9
output
  1. 27
input
  1. 4
    8 2 2 7
output
  1. 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 3should 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 power3 + 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 be8 + 2 + 2 + 6 = 18.

题意:一串数字,选择一个作为leading,再找多个Secondary,要求Secondary要能被leading整除,Secondary可以任意减小,leading不能减小,将leading和Secondary加和。问最大的和为多少。

思路:一眼看去没有思路,看了别人的题解。枚举leading,找p*leading—(p+1)*leading之间数字的个数;所以使用前缀和,cnt[num-1]-cnt[num-x-1]为[num-x,num)之间数字的个数。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. #define LL long long
  7. #define N 200005
  8.  
  9. int num[N];
  10. int cnt[N];
  11. bool vis[N];
  12. int main()
  13. {
  14. int n;
  15. while(scanf("%d",&n)!=EOF)
  16. {
  17. memset(vis,,sizeof(vis));
  18. memset(cnt,,sizeof(cnt));
  19. int maxn=;
  20. for(int i=; i<n; i++)
  21. {
  22. scanf("%d",&num[i]);
  23. maxn=max(maxn,num[i]);
  24. cnt[num[i]]++;
  25. }
  26. sort(num,num+n);
  27. for(int i=; i<=maxn; i++)
  28. cnt[i]+=cnt[i-];
  29. LL res=;
  30. for(int i=; i<n; i++)
  31. {
  32. LL tmp=;
  33. if(!vis[num[i]])
  34. {
  35. vis[num[i]]=;
  36. for(int j=num[i]; j<=maxn; j+=num[i])
  37. {
  38. tmp+=(cnt[j-]-cnt[j-num[i]-])*(LL)(j-num[i]);
  39. if(j+num[i]>maxn)
  40. tmp+=(cnt[maxn]-cnt[j-])*(LL)j;
  41. }
  42. res=max(tmp,res);
  43. }
  44. }
  45. if(n==)
  46. printf("%d\n",num[]);
  47. else
  48. printf("%I64d\n",res);
  49. }
  50. return ;
  51. }

Codeforces_731F_(前缀和)的更多相关文章

  1. HDU1671——前缀树的一点感触

    题目http://acm.hdu.edu.cn/showproblem.php?pid=1671 题目本身不难,一棵前缀树OK,但是前两次提交都没有成功. 第一次Memory Limit Exceed ...

  2. 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed

    之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...

  3. ASP.NET Core MVC 配置全局路由前缀

    前言 大家好,今天给大家介绍一个 ASP.NET Core MVC 的一个新特性,给全局路由添加统一前缀.严格说其实不算是新特性,不过是Core MVC特有的. 应用背景 不知道大家在做 Web Ap ...

  4. 如何处理CSS3属性前缀

    今天闲来无聊,重新来说说CSS3前缀的问题.在春节前和@一丝姐姐说起Sass中有关于gradient的mixins.姐姐说: 为什么还要用mixin呢?为什么不使用Autoprefixer?使用Aut ...

  5. context:component-scan" 的前缀 "context" 未绑定。

    SpElUtilTest.testSpELLiteralExpressiontestSpELLiteralExpression(cn.zr.spring.spel.SpElUtilTest)org.s ...

  6. 解决adobe air sdk打包 apk后自动在包名前面加上air. (有个点)前缀的问题

    早就找到了这个方法,但是一直忙没心思写博客. 默认情况下,所有 AIR Android 应用程序的包名称都带 air 前缀.若不想使用此默认行为,可将计算机环境变量 AIR_NOANDROIDFLAI ...

  7. adobe air类app 接入腾讯开放平台移动游戏使用带tencent包名前缀的问题

    作者:Panda Fang 出处:http://www.cnblogs.com/lonkiss/p/4209159.html 原创文章,转载请注明作者和出处,未经允许不可用于商业营利活动 ------ ...

  8. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  9. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

随机推荐

  1. jq仿ps颜色拾取功能-js颜色拾取

    1.效果展示 2.html代码:index.html <!DOCTYPE html> <html lang="en"> <head> <m ...

  2. ubuntu16.04 卸载 php7并安装php5.6记录

    ubuntu16.04版本从默认源安装的php版本为7.x版本,我们都知道php7.0已经舍弃了很多旧版本的函数等内容,这对旧系统来说是致命的,那么,我们就有了安装旧版php的需求,而同一主机安装两个 ...

  3. vim 快速搜索的快捷键

    当光标在某个单词上面的时候 按 shift + #键(或 shift + * )就可以了!!! ----------------------------------- If you are worki ...

  4. shell apt install 按tab键自动补全

    insert if [ -f /etc/bash_completion ]; then . /etc/bash_completion fi to ~/.bashrc

  5. Handling bundles in activities and fragments

     Bundle is a useful data holder, which maps String values to various Parcelable types. So basicall ...

  6. 使用Tomcat的一些经验和心得

    如今将使用Tomcat的一些经验和心得写到这里.作为记录和备忘.假设有朋友看到,也请指教. 1.首先是Tomcat的获取和安装. 获取当然得上Apache的官方站点下载,开源免费.并且带宽也足够.下载 ...

  7. Apache 配置学习占位

    http://www.cnblogs.com/yeer/archive/2011/01/18/1938024.html http://www.cnblogs.com/zgx/archive/2011/ ...

  8. hdu 2094 产生冠军(拓扑排序)

    产生冠军 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. POJ 3683 Priest John&#39;s Busiest Day (2-SAT+输出可行解)

    题目地址:POJ 3683 第一次做须要输出可行解的题目. . .大体思路是先用强连通来推断是否有可行解,然后用逆序建图.用拓扑排序来进行染色.然后输出可行解. 详细思路见传送门 由于推断的时候少写了 ...

  10. P1830 轰炸III

    P1830 轰炸III 84通过 145提交 题目提供者wanglichao1121 标签模拟矩阵洛谷原创 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目背景 一个大小为N ...