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 extra := 0 if os.Getenv("I") == "IronMan" { fp, _ = os.Open(os.Getenv("END_GAME")) extra = 100 } scanner := bufio.NewScanner(fp) configure(scanner) writer := bufio.NewWriter(wfp) defer func() { r := recover() if r != nil { fmt.Fprintln(writer, r) } writer.Flush() }() solve(scanner, writer) for i := 0; i < extra; i++ { fmt.Fprintln(writer, "-----------------------------------") solve(scanner, writer) } } func solve(scanner *bufio.Scanner, writer *bufio.Writer) { n := getNextInt(scanner) type pair [2]int pp := make([]pair, n) for i := 0; i < n; i++ { pp[i][0] = getNextInt(scanner) } for i := 0; i < n; i++ { pp[i][1] = getNextInt(scanner) } sort.Slice(pp, func(i, j int) bool { return pp[i][0] < pp[j][0] }) ss := make([]int64, n+1) for i := 0; i < n; i++ { ss[i+1] = ss[i] + int64(pp[i][1]) } l := -1 r := n + 1 for l+1 < r { m := (l + r) >> 1 if ss[m] < ss[n]-ss[m] { l = m continue } r = m } var ans int64 if r == 0 { x := pp[0][0] for i := 0; i < n; i++ { ans += int64(pp[i][1]) * int64(abs(x-pp[i][0])) } fmt.Fprintln(writer, fmt.Sprintf("%d %d", x, ans)) return } if r == n { x := pp[n-1][0] for i := 0; i < n; i++ { ans += int64(pp[i][1]) * int64(abs(x-pp[i][0])) } fmt.Fprintln(writer, fmt.Sprintf("%d %d", x, ans)) return } x := pp[r][0] for i := 0; i < n; i++ { ans += int64(pp[i][1]) * int64(abs(x-pp[i][0])) } fmt.Fprintln(writer, fmt.Sprintf("%d %d", x, ans)) } func abs(a int) int { if a < 0 { return -a } return a }