forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1539D.go
46 lines (41 loc) · 746 Bytes
/
1539D.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
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"bufio"
. "fmt"
"io"
"sort"
)
// github.com/EndlessCheng/codeforces-go
func CF1539D(_r io.Reader, out io.Writer) {
in := bufio.NewReader(_r)
min := func(a, b int64) int64 {
if a > b {
return b
}
return a
}
var n int
Fscan(in, &n)
a := make([]struct{ c, lim int64 }, n)
for i := range a {
Fscan(in, &a[i].c, &a[i].lim)
}
sort.Slice(a, func(i, j int) bool { return a[i].lim < a[j].lim })
ans := int64(0)
for l, r, c := 0, n-1, int64(0); l <= r; {
if a[l].lim > c {
v := min(a[l].lim-c, a[r].c)
ans += v * 2
c += v
if a[r].c -= v; a[r].c == 0 {
r--
}
} else {
ans += a[l].c
c += a[l].c
l++
}
}
Fprint(out, ans)
}
//func main() { CF1539D(os.Stdin, os.Stdout) }