题目链接:http://codeforces.com/contest/731/problem/F

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

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个数,在其中选择1个数,使得其他数经过自减后能够被它整除,求经过处理后,这n个数之和所能达到的最大值?

题解:

1.sum[]数组记录a[i]的个数,即为sum[a[i]]。

2.对sum[]数组求后缀和。则sum[i]为:在这n个数之中,大小能够达到i(>=i)的个数。

3.可以形象地把这n个数想象成n条长度分别为a[i]的木棍,然后把这n条木棍左对齐,而sum[len]即为长度>=len的木棍条数。

4.当被选中的数为a[i]时,就依次在长度为a[i]*k的位置上切一刀,得到sum[a[i]*k]条木棍,每条木棍长度为a[i], 那么总和 tmp += sum[a[i]*k]*a[i]  (k=1,2,3,4……,a[i]<=a[i]*k<=最长的那条木棍)。然后枚举a[i],取ans = max(tmp);

注意:如果a[i]是a[j]的倍数,那么a[i]就没有枚举的必要了,因为a[i]的情况已经包含于a[j]里面了。即:a[i]情况下的tmp<=a[j]情况下的tmp。所以加个vis[]数组。

学习之处:

1.以后看到数据范围在 200,000 之内的,可以考虑开大小为 200,000 的数组,实现y=x的映射。

2.前缀和的逆向求和,即后缀和sum[val]。其表示为:大小能够达到val的数的个数。

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 2e5+10; int n, N, a[maxn], sum[maxn];
bool vis[maxn]; void init()
{
memset(sum,0,sizeof(sum));
memset(vis,0,sizeof(vis)); scanf("%d",&n);
for(int i = 1; i<=n; i++)
{
scanf("%d",&a[i]);
sum[a[i]]++;
}
sort(a+1, a+1+n);
for(int i = a[n]-1; i>0; i--)
sum[i] += sum[i+1];
} int solve()
{
LL ans = 0;
for(int i = 1; i<=n; i++)
{
int len = a[i];
if(vis[len]) continue; LL tmp = 0;
for(int pos = len; pos<=a[n]; pos += len)
{
tmp += 1LL*sum[pos]*len;
vis[pos] = 1;
}
ans = max(ans, tmp);
}
cout<<ans<<endl;
} int main()
{
init();
solve();
}

最新文章

  1. 【Java EE 学习 74 上】【数据采集系统第六天】【使用Jfreechart的统计图实现】【Jfreechart的基本使用方法】
  2. markdown编辑器
  3. 【mysql】关于硬件方面的一些优化
  4. 浅析C# new和override的区别
  5. Core Data 多线程时多个context使用
  6. 【leetcode】Remove Element (easy)
  7. ZOJ 3913 Bob wants to pour water ZOJ Monthly, October 2015 - H
  8. NSString
  9. asp.net 查询,导出
  10. [转]C++中引用(&amp;)的用法和应用实例
  11. NFC应用实例
  12. CST和GMT时间的区别
  13. windows下安装和配置Weka
  14. 实现TableLayout布局下循环取出TableRow控件中的文字内容到list集合
  15. SQL Server中的CLR编程——用.NET为SQL Server编写存储过程和函数
  16. Linux设备驱动编程---miscdevice杂类设备的使用方法
  17. Java实现发送手机验证码功能(短信+语音)
  18. 【Docker江湖】之docker部署与理解
  19. 〖Android〗从Android Studio转为Eclipse开发项目运行程序闪退的解决方法
  20. 在推送提交之后阻止Azure DevOps (TFS)持续集成

热门文章

  1. 补充==的使用和equals的区别
  2. Linux下批量替换文件内容和文件名(转)
  3. 用AntRun插件测试Maven的生命周期
  4. springboot配置filter
  5. Android gradle 相关配置
  6. android -- 存储byte
  7. 【转载】使用事件模型 &amp; libev学习
  8. AngularJS的表单验证提交示例
  9. SolidEdge 工程图中如何给零件添加纹理或贴图
  10. 关于CUDA两种API:Runtime API 和 Driver API