No.1470 Mex Sum
Problem Statement
You are given a sequence $A_1 , A_2 , \cdots , A_N$ of $N$ integers, each at least $1$.
Also, let $\mathrm{mex} (i,j)$ be the smallest integer that is at least $1$ and is equal to neither $i$ nor $j$.
Find the sum of $\mathrm{mex} (A_i,A_j)$ over all integer pairs $(i,j)$ satisfying $1 \leq i < j \leq N$.
Input
$N$
$A_1\ \ A_2\ \ \cdots\ \ A_N$
- $2 \leq N \leq 2 \times 10^5$
- $1 \leq A_i \leq 10^9(1 \leq i \leq N)$
- All inputs are integers.
Output
Output the sum of $\mathrm{mex} (A_i,A_j)$ over all integer pairs $(i,j)$ satisfying $1 \leq i < j \leq N$, followed by a newline.
Sample
Sample 1
Input
3
2 1 1
Output
8
There are $3$ integer pairs satisfying $1 \leq i < j \leq N$: $(1,2),(1,3),(2,3)$.
- $\mathrm{mex} (A_1,A_2)= \mathrm{mex} (2,1)=3$
- $\mathrm{mex} (A_1,A_3)= \mathrm{mex} (2,1)=3$
- $\mathrm{mex} (A_2,A_3)= \mathrm{mex} (1,1)=2$
Therefore, the answer is $3+3+2=8$.
Sample 2
Input
5
1 1 1 1 1
Output
20
For all integer pairs satisfying $1 \leq i < j \leq N$, $\mathrm{mex} (A_i,A_j)=2$.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
蜜蜂
nok0