#P5682. [CSP-J 2019 江西] 次大值

[CSP-J 2019 江西] 次大值

Problem Description

Alice has nn positive integers, numbered from 1n1 \sim n, which are a1,a2,,ana_1, a_2, \dots, a_n.
Bob has just learned the modulo operation, so he uses these nn numbers to practice. He wrote down all values of

aimodaj(1i,jnij)a_i \bmod a_j (1 \le i,j \le n \wedge i \neq j)

where mod\bmod denotes the modulo operation.

Alice wants to know the strict second largest value among all results. Deduplicate all values obtained after modulo, meaning that identical results are kept only once. The second largest value among the remaining numbers is called the strict second largest value.

Input Format

The first line contains a positive integer nn, representing the number of integers.
The second line contains nn positive integers aia_i.

Output Format

Output one integer in a single line, representing the answer.
If there are fewer than two numbers left after deduplicating the modulo results, output 1-1.

4
4 5 5 6
4
4
1 1 1 1
-1
7
12 3 8 5 7 20 15
12

Hint

【Constraints】
For 40%40\% of the testdata, 1n,ai1001 \le n, a_i \le 100;
For 70%70\% of the testdata, 1n30001 \le n \le 3000, 1ai1051 \le a_i \le 10^5;
For 100%100\% of the testdata, 3n2×1053 \le n \le 2 \times 10^5, 1ai1091 \le a_i \le 10^9.

【Sample 11 Explanation】
All modulo results are {4,4,4,1,0,5,1,0,5,2,1,1}\{4,4,4,1,0,5,1,0,5,2,1,1\}.
After deduplication, we have {0,1,2,4,5}\{0,1,2,4,5\}, so the result is 44.

Translated by ChatGPT 5