// Package main provides // // File: main.go // Author: ymiyamoto // // Created on Sun Sep 29 02:05:32 2019 // package main import ( "container/heap" "fmt" ) type pq []int func (p pq) Len() int { return len(p) } func (p pq) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p pq) Less(i, j int) bool { return p[i] > p[j] } func (p *pq) Pop() interface{} { n := len(*p) x := (*p)[n-1] *p = (*p)[:n-1] return x } func (p *pq) Push(x interface{}) { v, _ := x.(int) *p = append(*p, v) } func main() { var n int fmt.Scan(&n) p := make(pq, n) sum := 0 for i := 0; i < n; i++ { fmt.Scan(&p[i]) sum += p[i] } heap.Init(&p) var q int fmt.Scan(&q) for i := 0; i < q; i++ { var x int fmt.Scan(&x) n, _ := heap.Pop(&p).(int) for x <= n { heap.Push(&p, n%x) sum -= n sum += n % x n, _ = heap.Pop(&p).(int) } heap.Push(&p, n) fmt.Println(sum) } }