!> This file was processed by `fypp`. !> Today's fortune: "Lucky:)", really OK? !> ランダムウォーク猿「'bit DP' で はっぴー.」 !> ギャンブラー猿「AtCoder はゲームだ.」 program f902938 use, intrinsic :: iso_fortran_env !> auto use module implicit none integer(int32), parameter :: max_a = 500 integer(int32) :: n integer(int32), allocatable :: arr(:) integer(int64) :: idx_sums(max_a), cnts(max_a) integer(int64) :: ans integer(int32) :: i, v read(input_unit, *) n allocate(arr(n)) read(input_unit, *) arr(:) idx_sums(:) = 0_int64 cnts(:) = 0_int64 ans = 0_int64 !> 2 * Σ_{i = 1}^{n}Σ_{j = i + 1}^{n}(...) == Σ_{i = 1}^{n}Σ_{j = 1}^{n}(...) do i = 1, n associate(idx => n - i) do v = 1, max_a ans = ans + (idx_sums(v) - cnts(v) * idx) * abs(v - arr(i)) end do idx_sums(arr(i)) = idx_sums(arr(i)) + idx cnts(arr(i)) = cnts(arr(i)) + 1 end associate end do write(output_unit, '(i0)') 2 * ans end program f902938