← Home
```go
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	scanInt := func() int {
		scanner.Scan()
		val, _ := strconv.Atoi(scanner.Text())
		return val
	}

	if !scanner.Scan() {
		return
	}
	m, _ := strconv.Atoi(scanner.Text())

	minQ := 200000
	for i := 0; i < m; i++ {
		q := scanInt()
		if q < minQ {
			minQ = q
		}
	}

	n := scanInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt()
	}

	// Sort prices in descending order
	sort.Sort(sort.Reverse(sort.IntSlice(a)))

	var totalCost int64 = 0
	blockSize := minQ + 2

	for i, price := range a {
		// In every block of size minQ+2, we pay for the first minQ items
		// and get the next 2 items for free.
		if i%blockSize < minQ {
			totalCost += int64(price)
		}
	}

	fmt.Println(totalCost)
}
```