Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "container/heap"
- "fmt"
- "os"
- // "github.com/pkg/profile"
- )
- // An Item is something we manage in a priority queue.
- type Item struct {
- value int // The value of the item; arbitrary.
- priority int // The priority of the item in the queue.
- // The index is needed by update and is maintained by the heap.Interface methods.
- index int // The index of the item in the heap.
- }
- type PriorityQueue []*Item
- func (pq PriorityQueue) Len() int { return len(pq) }
- func (pq PriorityQueue) Less(i, j int) bool {
- // We want Pop to give us the highest, not lowest, priority so we use greater than here.
- return pq[i].priority < pq[j].priority
- }
- func (pq PriorityQueue) Swap(i, j int) {
- pq[i], pq[j] = pq[j], pq[i]
- pq[i].index = i
- pq[j].index = j
- }
- func (pq *PriorityQueue) Push(x any) {
- n := len(*pq)
- item := x.(*Item)
- item.index = n
- *pq = append(*pq, item)
- }
- func (pq *PriorityQueue) Pop() any {
- old := *pq
- n := len(old)
- item := old[n-1]
- old[n-1] = nil // avoid memory leak
- item.index = -1 // for safety
- *pq = old[0 : n-1]
- return item
- }
- // update modifies the priority and value of an Item in the queue.
- func (pq *PriorityQueue) update(item *Item, value int, priority int) {
- item.value = value
- item.priority = priority
- heap.Fix(pq, item.index)
- }
- var t int
- func main() {
- // defer profile.Start().Stop()
- in := bufio.NewReader(os.Stdin)
- var (
- n, m, l, ld, a int
- )
- fmt.Fscan(in, &n, &m)
- pq := make(PriorityQueue, n)
- pq2 := make(PriorityQueue, 0)
- for i := 0; i < n; i++ {
- fmt.Fscan(in, &a)
- pq[i] = &Item{
- value: a,
- priority: a,
- index: i,
- }
- }
- heap.Init(&pq)
- heap.Init(&pq2)
- for j := 0; j < m; j++ {
- fmt.Fscan(in, &t, &l)
- for pq2.Len() > 0 {
- it := heap.Pop(&pq2).(*Item)
- if it.priority <= t {
- it.priority = it.value
- heap.Push(&pq, it)
- } else {
- heap.Push(&pq2, it)
- break
- }
- }
- if pq.Len() == 0 {
- continue
- }
- it := heap.Pop(&pq).(*Item)
- ld += it.value * l
- it.priority = t + l
- heap.Push(&pq2, it)
- }
- fmt.Printf("%d", ld)
- }
Advertisement
Add Comment
Please, Sign In to add comment