package main import ( "container/heap" "fmt" ) const offset = 16384 // An IntHeap is a min-heap of ints. type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *IntHeap) Push(x interface{}) { // Push and Pop use pointer receivers because they modify the slice's length, // not just its contents. *h = append(*h, x.(int)) } func (h *IntHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } func main() { var N, tmp int fmt.Scan(&N) A := make(IntHeap, N) B := make([]int, N) for i := 0; i < N; i++ { fmt.Scan(&tmp) A[i] = tmp * offset } heap.Init(&A) for i := 0; i < N; i++ { fmt.Scan(&B[i]) } min := N + 1 for i := 0; i < N; i++ { tmp = calc(i, A, B) if tmp < min { min = tmp } } fmt.Println(min) } func calc(start int, A IntHeap, B []int) int { N := len(B) h := make(IntHeap, len(A)) copy(h, A) for i := 0; i < N; i++ { j := start + i if j >= len(B) { j -= len(B) } l := heap.Pop(&h) nl := l.(int) + ((B[j] / 2) * offset) + 1 heap.Push(&h, nl) } var tmp, count int for h.Len() > 0 { tmp = heap.Pop(&h).(int) % offset if tmp > count { count = tmp } } return count }