-
Notifications
You must be signed in to change notification settings - Fork 0
/
371c.go
109 lines (104 loc) · 1.5 KB
/
371c.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 00
package main
import (
"bufio"
"fmt"
"os"
)
func write(f *bufio.Writer, a ...interface{}) {
f.Write([]byte(fmt.Sprint(a...)))
}
type Number interface {
int64 | float64 | int
}
func min[K Number](a K, b K) K {
if a < b {
return a
}
return b
}
func max[K Number](a K, b K) K {
if a > b {
return a
}
return b
}
func main() {
var s string
var nb, ns, nc, pb, ps, pc, r int64
reader := bufio.NewReader(os.Stdin)
f := bufio.NewWriter(os.Stdout)
defer f.Flush()
fmt.Fscan(reader, &s, &nb, &ns, &nc, &pb, &ps, &pc, &r)
bc := int64(0)
sc := int64(0)
cc := int64(0)
for i := 0; i < len(s); i++ {
if s[i] == 'B' {
bc++
} else if s[i] == 'S' {
sc++
} else {
cc++
}
}
ans := int64(0)
cost := bc*pb + sc*ps + cc*pc
for r > 0 {
if bc <= nb && sc <= ns && cc <= nc {
ans++
nb -= bc
ns -= sc
nc -= cc
} else if nb > 0 || ns > 0 || nc > 0 {
req := int64(0)
if nb < bc {
req += (bc - nb) * pb
}
if ns < sc {
req += (sc - ns) * ps
}
if nc < cc {
req += (cc - nc) * pc
}
if req <= r {
r -= req
ans++
used := 0
if nb < bc {
nb = 0
} else {
if bc > 0 {
used++
}
nb -= bc
}
if ns < sc {
ns = 0
} else {
if sc > 0 {
used++
}
ns -= sc
}
if nc < cc {
nc = 0
} else {
if cc > 0 {
used++
}
nc -= cc
}
if used == 0 {
break
}
} else {
break
}
} else {
break
}
}
ans += r / cost
write(f, ans, "\n")
}