func main() var N: int :: cui@input().toInt(&) var k: int :: (N + 1) / 3 * 2 + (N % 3 = 1 ?(1, 0)) var A: []int :: #[N]int var B: []int :: #[N]int var C: []int :: #[N]int for i(0, N - 1) var _: [][]char :: cui@input().split(" ") do A[i] :: _[0].toInt(&) do B[i] :: _[1].toInt(&) do C[i] :: (A[i] + (B[i] * (k - 1))) * 10 ^ 5 + i end for do C.sort() var AA: []int :: #[k]int var BB: []int :: #[k]int for i(0, k - 1) var id: int :: C[i] % 10 ^ 5 do AA[i] :: A[id] do BB[i] :: B[id] end for do BB.sort() do BB.reverse() var ans: int for i(0, k - 1) do ans :+ AA[i] + BB[i] * i end for do cui@print("\{ans}\n") end func