package main import ( "bufio" "fmt" "os" "sort" "strconv" ) func configure(scanner *bufio.Scanner) { scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 1000005), 1000005) } func getNextString(scanner *bufio.Scanner) string { scanned := scanner.Scan() if !scanned { panic("scan failed") } return scanner.Text() } func getNextInt(scanner *bufio.Scanner) int { i, _ := strconv.Atoi(getNextString(scanner)) return i } func getNextInt64(scanner *bufio.Scanner) int64 { i, _ := strconv.ParseInt(getNextString(scanner), 10, 64) return i } func getNextFloat64(scanner *bufio.Scanner) float64 { i, _ := strconv.ParseFloat(getNextString(scanner), 64) return i } func main() { fp := os.Stdin wfp := os.Stdout cnt := 1 if os.Getenv("I") == "IronMan" { fp, _ = os.Open(os.Getenv("END_GAME")) cnt = 100 } scanner := bufio.NewScanner(fp) configure(scanner) writer := bufio.NewWriter(wfp) defer func() { r := recover() if r != nil { fmt.Fprintln(writer, r) } writer.Flush() }() for i := 0; i < cnt; i++ { if i > 0 { fmt.Fprintln(writer, "-----------------------------------") } solve(scanner, writer) } } func solve(scanner *bufio.Scanner, writer *bufio.Writer) { n := getNextInt(scanner) k := getNextInt64(scanner) x := getNextInt64(scanner) y := getNextInt64(scanner) aa := make([]int64, n) for i := 0; i < n; i++ { aa[i] = getNextInt64(scanner) - 1 } sort.SliceStable(aa, func(i, j int) bool { return aa[i] < aa[j] }) if x >= y { fmt.Fprintln(writer, count(aa[n-1], k)*y) return } var l, r, s1, s2 int64 l = -1 r = int64(1e9 + 1) for 2 < r-l { m1 := l + (r-l)/3 m2 := r - (r-l)/3 s1 = m1 * y s2 = m2 * y for i := 0; i < n; i++ { s1 += heal(aa[i], k, x, y, m1) s2 += heal(aa[i], k, x, y, m2) } if s1 >= s2 { l = m1 continue } r = m2 } var ans, temp int64 ans = total(aa, k, x, y, l) temp = total(aa, k, x, y, l+1) if ans > temp { ans = temp } ans = total(aa, k, x, y, r) if ans > temp { ans = temp } fmt.Fprintln(writer, ans) } func count(a, k int64) int64 { return (a + k - 1) / k } func heal(a, k, x, y, c int64) int64 { if a-k*c <= 0 { return 0 } return count(a-k*c, k) * x } func total(aa []int64, k, x, y, c int64) int64 { var ans int64 for i := 0; i < len(aa); i++ { ans += heal(aa[i], k, x, y, c) } return ans + y*c }