package main import ( "bufio" "fmt" "math" "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) pp := make([]pair, n) for i := 0; i < n; i++ { pp[i][0] = cint(getNextInt(scanner)) pp[i][1] = cint(getNextInt(scanner)) } sort.SliceStable(pp, func(i, j int) bool { return pp[i][1] > pp[j][1] }) dp := makeGrid(n+1, n+1) for j := 0; j < n+1; j++ { for i := 0; i < n+1; i++ { dp[j][i] = math.MaxInt64 } } dp[0][0] = 0 for i := 0; i < n; i++ { for j := cint(i + 1); j > 0; j-- { if dp[j-1][j-1] == math.MaxInt32 { continue } dp[j][j].minAs(dp[j-1][j-1] + pp[i][0] + pp[i][1]*(j-1)) } } fmt.Fprintln(writer, dp[n-n/3][n-n/3]) } func makeGrid(h, w int) [][]cint { index := make([][]cint, h, h) data := make([]cint, h*w, h*w) for i := 0; i < h; i++ { index[i] = data[i*w : (i+1)*w] } return index } type pair [2]cint type cint int64 func (x *cint) minAs(y cint) *cint { *x = x.min(y) return x } func (x *cint) maxAs(y cint) *cint { *x = x.max(y) return x } func (x *cint) min(y cint) cint { if *x > y { return y } return *x } func (x *cint) max(y cint) cint { if *x < y { return y } return *x }