Maximum Value(Codeforces_484B)

来源:互联网 发布:java android pdf 编辑:程序博客网 时间:2024/05/29 09:48

You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj.

Input

The first line contains integer n — the length of the sequence (1 ≤ n ≤ 2·105).

The second line contains n space-separated integers ai (1 ≤ ai ≤ 106).

Output

Print the answer to the problem.

Sample test(s)

Input

3
3 4 5

Output

2

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;int main(){    int n;    int a,num[2000100];    scanf("%d",&n);    int maxx=-99999;    for(int i=1;i<=n;i++)    {        scanf("%d",&a);        num[a]=a;        maxx=max(maxx,a);    }    for(int i=1;i<=maxx*2+1;i++)    {        if(num[i]!=i) num[i]=num[i-1];//这样处理一下更高效    }    int ans=0;    for(int i=2;i<maxx;i++)    {        if(num[i]==i)        {            for(int j=i*2;j<=maxx*2+1;j+=i)//枚举i的倍数            {                if(num[j-1]>i)//与i取余所能得到的最大值只会在比i的倍数小的数中的最大值中出现                    ans=max(ans,num[j-1]%i);//(有点绕,譬如有四个数3,4,5,6,i=3,比i的2倍小的数有4,5,最大的是5,所以5%3是当前所得到的最大余数            }        }    }    printf("%d\n",ans);    return 0;}
0 0
原创粉丝点击