D. Maximum Value
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
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.
Examples
input
3
3 4 5
output
2
1 //2017-08-17 2 #include <cstdio> 3 #include <iostream> 4 #include <cstring> 5 #include <algorithm> 6 7 using namespace std; 8 9 const int N = 200010; 10 const int M = 3000010; 11 //book[i]记录i是否出现过,pre[i]表示比i小的出现过的最大的数 12 int a[N], n, book[M], pre[M]; 13 14 int main() 15 { 16 while (scanf("%d", &n) != EOF) 17 { 18 memset(book, 0, sizeof(book)); 19 for (int i = 0; i < n; i++){ 20 scanf("%d", &a[i]); 21 book[a[i]] = 1; 22 } 23 sort(a, a + n); 24 n = unique(a, a+n)-a; 25 int ans = 0, maxinum = (a[n-1]<<1), ptr = 0; 26 for(int i = 1; i <= maxinum; i++){ 27 pre[i] = ptr; 28 if(book[i])ptr = i; 29 } 30 for (int i = 0; i < n; i++){ 31 for (int j = a[i]<<1; j <= maxinum; j+=a[i]){ 32 ans = max(ans, a[i]-j+pre[j]); 33 } 34 } 35 printf("%d ", ans); 36 } 37 return 0; 38 }