forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
13C.go
35 lines (30 loc) · 752 Bytes
/
13C.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"bufio"
"container/heap"
. "fmt"
"io"
"sort"
)
type hp13 struct{ sort.IntSlice }
func (h hp13) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
func (h *hp13) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp13) Pop() interface{} { a := h.IntSlice; v := a[len(a)-1]; h.IntSlice = a[:len(a)-1]; return v }
// github.com/EndlessCheng/codeforces-go
func CF13C(_r io.Reader, out io.Writer) {
in := bufio.NewReader(_r)
var n, v, ans int
Fscan(in, &n)
h := hp13{}
for ; n > 0; n-- {
Fscan(in, &v)
heap.Push(&h, v)
if d := h.IntSlice[0] - v; d > 0 {
ans += d
h.IntSlice[0] = v
heap.Fix(&h, 0)
}
}
Fprint(out, ans)
}
//func main() { CF13C(os.Stdin, os.Stdout) }