Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "sort"
- )
- func binSearch(arr []int, target int) int {
- low, high := 0, len(arr)-1
- for low <= high {
- mid := (low + high) / 2
- if arr[mid] == target && mid+1 < len(arr) && arr[mid+1] != target {
- return mid
- } else if arr[mid] > target {
- high = mid - 1
- } else {
- low = mid + 1
- }
- }
- return -2
- }
- func main() {
- var n int
- fmt.Scan(&n)
- for i := 0; i < n; i++ {
- var size int
- fmt.Scan(&size)
- arr := make([]int, size)
- for j := range arr {
- fmt.Scan(&arr[j])
- }
- sort.Ints(arr)
- sum := 0
- start := 0
- end := binSearch(arr, arr[start]) + 1
- for end != -1 {
- sum += arr[start] * (end - start - (end-start)/3)
- start = end
- end = binSearch(arr, arr[start]) + 1
- }
- sum += arr[start] * (len(arr) - start - (len(arr)-start)/3)
- fmt.Println(sum)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement