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.
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.
The only line of the output should contain one integer value — the maximum possible total power of video cards working together.
4
3 2 15 9
27
4
8 2 2 7
18
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.
————————————分割线————————————
分析:
枚举每个数字,让它成为主视频卡ak,然后枚举倍数 j ,将j介于 ( j - 1 ) * ak<= x < j * ak , 全部减少到 ( j - 1 ) * ak,为了快速的找到这些数,可以使用前缀和优化思想。
例如:
a1为现在的主视频卡,让介于[ a1 * 1 , a1 * 2 ) 减少到a1 , 让介于[ a1 * 2 , a1 * 3 ) 减少到2 * a1 , 依此类推... ...
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include "bits/stdc++.h" 2 3 using namespace std ; 4 const int maxN = 3e5 + 1e3 ; 5 const int INF = 2147483647 ; 6 typedef long long QAQ ; 7 8 QAQ Sum[ maxN ] , buc[ maxN ] ; 9 10 inline int INPUT ( ) { 11 int x = 0 , f = 1 ; char ch = getchar ( ) ; 12 while ( ch < '0' || '9' < ch ) { if ( ch == '-' ) f = -1 ; ch = getchar ( ) ; } 13 while ( '0' <= ch && ch <= '9' ) { x = ( x << 1 ) + ( x << 3 ) + ch - '0' ; ch = getchar ( ) ; } 14 return x * f ; 15 } 16 17 void Pre_Init ( int n ) { 18 for ( int i=1 ; i<=_max ; ++i ) 19 Sum[ i ] = Sum[ i - 1 ] + buc[ i ] ; 20 } 21 22 inline QAQ gmax ( QAQ x , QAQ y ) { return x > y ? x : y ; } 23 inline QAQ gmin ( QAQ x , QAQ y ) { return x > y ? y : x ; } 24 25 int main ( ) { 26 QAQ Ans = -INF , _max = -INF ; 27 int N = INPUT ( ) ; 28 for ( int i=1 ; i<=N ; ++i ) { 29 int tmp = INPUT ( ) ; 30 ++ buc [ tmp ] ; 31 _max = gmax ( _max , tmp ) ; 32 } 33 34 Pre_Init ( _max ) ; 35 36 for ( int i=1 ; i<=_max ; ++i ) { 37 if ( !buc[ i ] ) continue ; 38 QAQ rest = 0 ; 39 for ( int j=i ; j<=_max ; j+=i ) { 40 rest += ( Sum[ gmin ( i + j - 1 , _max ) ] - Sum [ j - 1 ] ) * j ; 41 } 42 Ans = gmax ( Ans , rest ) ; 43 } 44 cout << Ans << endl ; 45 return 0 ; 46 }
2016-10-18 10:45:39
(完)