package main

import (
	"bufio"
	"os"
	"strconv"
)

var in *bufio.Scanner
var out *bufio.Writer

func rev(s string) string {
	r := []byte(s)
	i := 0
	j := len(r) - 1
	for i < j {
		r[i], r[j] = r[j], r[i]
		i++
		j--
	}
	return string(r)
}

func smaller(s string, t string) bool {
	ns := len(s)
	nt := len(t)
	i := 0
	for (i < ns) && (i < nt) && (s[i] == t[i]) {
		i++
	}
	if i == ns {
		return i <= nt
	}
	if i == nt {
		return false
	}
	return s[i] < t[i]
}

func min(x int64, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

const maxn = 100005
const inf = int64(20000000000000000)

var n int
var c [maxn]int64
var s, r [maxn]string
var f [maxn][2]int64

func main() {
	in = bufio.NewScanner(os.Stdin)
	out = bufio.NewWriter(os.Stdout)

	in.Split(bufio.ScanWords)
	defer out.Flush()

	in.Scan()
	n, _ = strconv.Atoi(in.Text())

	for i := 1; i <= n; i++ {
		in.Scan()
		c[i], _ = strconv.ParseInt(in.Text(), 10, 64)
	}

	for i := 1; i <= n; i++ {
		in.Scan()
		s[i] = in.Text()
		r[i] = rev(s[i])
	}

	f[1][0] = 0
	f[1][1] = c[1]
	for i := 1; i < n; i++ {
		f[i+1][0] = inf
		f[i+1][1] = inf
		if f[i][0] < inf {
			if smaller(s[i], s[i+1]) {
				f[i+1][0] = min(f[i+1][0], f[i][0])
			}
			if smaller(s[i], r[i+1]) {
				f[i+1][1] = min(f[i+1][1], f[i][0]+c[i+1])
			}
		}
		if f[i][1] < inf {
			if smaller(r[i], s[i+1]) {
				f[i+1][0] = min(f[i+1][0], f[i][1])
			}
			if smaller(r[i], r[i+1]) {
				f[i+1][1] = min(f[i+1][1], f[i][1]+c[i+1])
			}
		}
	}

	res := min(f[n][0], f[n][1])
	if res < inf {
		out.WriteString(strconv.FormatInt(res, 10))

	} else {
		out.WriteString("-1")
	}
	out.WriteByte('\n')

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const (
	MAX_N = 100100
)

var INF int64 = 1 << 50
var N int
var C [MAX_N]int64
var words [MAX_N]string
var rev_words [MAX_N]string

func Reverse(value string) string {
	data := []rune(value)
	result := []rune{}
	for i := len(data) - 1; i >= 0; i-- {
		result = append(result, data[i])
	}
	return string(result)
}

var dp [MAX_N][2]int64

func Compare(s, t string) bool {
	n := len(s)
	m := len(t)
	k := IntMin(n, m)
	for i := 0; i < k; i++ {
		if s[i] == t[i] {
			continue
		}
		return s[i] < t[i]
	}
	if n == m {
		return true
	}
	if n < m {
		return true
	}
	return false
}
func memo(pos, rev int) int64 {

	if pos == N {
		return 0
	}
	if dp[pos][rev] >= 0 {
		return dp[pos][rev]
	}
	var res int64
	res = INF
	// fmt.Println(res)
	if rev == 1 {
		if Compare(rev_words[pos-1], words[pos]) {
			res = Int64Min(res, memo(pos+1, 0))
		}
		if Compare(rev_words[pos-1], rev_words[pos]) {
			res = Int64Min(res, memo(pos+1, 1)+C[pos])
		}
	} else {
		if Compare(words[pos-1], words[pos]) {
			res = Int64Min(res, memo(pos+1, 0))
		}
		if Compare(words[pos-1], rev_words[pos]) {
			res = Int64Min(res, memo(pos+1, 1)+C[pos])
		}
	}
	// fmt.Println(pos, rev, res)
	dp[pos][rev] = res
	return res
}
func init() {
	for i := 0; i < MAX_N; i++ {
		for j := 0; j < len(dp[i]); j++ {
			dp[i][j] = -1
		}
	}
}
func main() {
	fsc := NewFastScanner()
	N = fsc.NextInt()
	for i := 0; i < N; i++ {
		C[i] = fsc.NextInt64()
	}
	for i := 0; i < N; i++ {
		words[i] = fsc.Next()
		rev_words[i] = Reverse(words[i])
	}
	var result int64
	result = INF
	result = Int64Min(result, memo(1, 0))
	result = Int64Min(result, memo(1, 1)+C[0])
	if result >= INF {
		result = -1
	}
	fmt.Println(result)
}

//template
type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1024)
	return &FastScanner{r: rdr}
}
func (s *FastScanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *FastScanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *FastScanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *FastScanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *FastScanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *FastScanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

//Max,Min
func IntMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func Int64Max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func Float64Max(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

func IntMin(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func Int64Min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func Float64Min(a, b float64) float64 {
	if a > b {
		return b
	}
	return a
}

//Gcd
func IntGcd(a, b int) int {
	if a < b {
		b, a = a, b
	}
	if b == 0 {
		return a
	}
	return IntGcd(b, a%b)
}
func Int64Gcd(a, b int64) int64 {
	if a < b {
		b, a = a, b
	}
	if b == 0 {
		return a
	}
	return Int64Gcd(b, a%b)
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/706/C

package main

import (
	"bufio"
	"fmt"
	"os"
)

const MaxCost = 1e14 + 1

func min(x int64, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

func reverse(s string) string {
	n := len(s)
	reversed := make([]byte, n)
	for i := 0; i < n; i++ {
		reversed[n-1-i] = s[i]
	}
	return string(reversed)
}

func Solve(n int, c []int64, s []string) int64 {
	dp := make([][2]int64, n)
	dp[0][0] = 0
	dp[0][1] = int64(c[0])
	for i := 1; i < n; i++ {
		dp[i][0] = MaxCost + 1
		dp[i][1] = MaxCost + 1
		if s[i] >= s[i-1] {
			dp[i][0] = min(dp[i][0], dp[i-1][0])
		}
		if s[i] >= reverse(s[i-1]) {
			dp[i][0] = min(dp[i][0], dp[i-1][1])
		}
		if reverse(s[i]) >= s[i-1] {
			dp[i][1] = min(dp[i][1], c[i]+dp[i-1][0])
		}
		if reverse(s[i]) >= reverse(s[i-1]) {
			dp[i][1] = min(dp[i][1], c[i]+dp[i-1][1])
		}
	}
	if dp[n-1][0] == MaxCost+1 && dp[n-1][1] == MaxCost+1 {
		return -1
	}
	return min(dp[n-1][0], dp[n-1][1])
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	c := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &c[i])
	}
	fmt.Fscanf(reader, "\n")
	s := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%s\n", &s[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, c, s))
}

package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
	"math"
)

func main() {
	var n int
	fmt.Scan(&n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	costs := make([]int64, n+1)
	for i:=0;i<n;i++ {
		costs[i+1] = readInt64(scanner)
	}
	words := make([]string, n+1)
	reverseWords := make([]string, n+1)
	words[0] = ""
	reverseWords[0] = ""
	for i:=1;i<=n;i++ {
		words[i] = readString(scanner)
		reverseWords[i] = reverse(words[i])
	}
	DP := make([][]int64, n+1)
	DP[0] = make([]int64, 2)
	for i:=1;i<=n;i++ {
		DP[i] = make([]int64, 2)
		DP[i][0] = math.MaxInt64
		DP[i][1] = math.MaxInt64
	}
	for i:=1;i<=n;i++ {
		was := false
		if words[i] >= words[i-1] && DP[i-1][0] != math.MaxInt64 {
			DP[i][0] = DP[i-1][0]
			was = true
		}
		if words[i] >= reverseWords[i-1] && DP[i-1][1] != math.MaxInt64 {
			DP[i][0] = min(DP[i][0], DP[i-1][1])
			was = true
		}
		if reverseWords[i] >= words[i-1] && DP[i-1][0] != math.MaxInt64 {
			DP[i][1] = DP[i-1][0] + costs[i]
			was = true
		}
		if reverseWords[i] >= reverseWords[i-1] && DP[i-1][1] != math.MaxInt64 {
			DP[i][1] = min(DP[i][1], DP[i-1][1] + costs[i])
			was = true
		}
		if !was {
			fmt.Println("-1")
			return
		}
	}
	fmt.Println(min(DP[n][0], DP[n][1]))
}

func min(a, b int64) int64 {
	if a<b {
		return a
	}
	return b
}

func reverse(s string) string {
	r := []byte(s)
	i:=0
	j:= len(s) - 1
	for i<j {
		r[i], r[j] = r[j], r[i]
		i++
		j--
	}
	return string(r)
}

func readString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	value, _ := strconv.Atoi(scanner.Text())
	return value
}

func readInt64(scanner *bufio.Scanner) int64 {
	scanner.Scan()
	value, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return value
}

// http://codeforces.com/problemset/problem/706/C

package main

import (
	"fmt"
	"bufio"
	"os"
	"strings"
	"math"
)

func min (a, b int64) int64 {
	if a < b {
		return a
	}

	return b
}

func min3 (a, b, c int64) int64 {
	return min(min(a, b), c)
}

func InvStr(str string) string {
	data := []byte(str)
	n := len(data)
	for i := 0; i <= (n - 1)/ 2; i++ {
		data[i], data[n - i - 1] = data[n - i - 1], data[i]
	}

	return string(data)
}

func main() {
	var n int
	var tmp int64

	input := bufio.NewReaderSize(os.Stdin, 1 << 20)

	fmt.Fscanln(input, &n)
	c := make([]int64, n)
	strs := make([]string, n)
	strinv := make([]string, n)
	dp := make([][]int64, n)

	for i := 0; i < n; i++ {
		if i == n - 1 {
			fmt.Fscanln(input, &tmp)
		} else {
			fmt.Fscan(input, &tmp)
		}
		c[i] = tmp
		dp[i] = make([]int64, 2)
		dp[i][0], dp[i][1] = math.MaxInt64, math.MaxInt64
	}

	for i := 0; i < n; i++ {
		var str string
		fmt.Fscanln(input, &str)

		strs[i] = str
		strinv[i] = InvStr(str)
	}

	// Solve
	if strings.Compare(strs[0], strs[1]) <= 0 {
		dp[1][0] = 0
	}
	if strings.Compare(strinv[0], strs[1]) <= 0 {
		dp[1][0] = min(c[0], dp[1][0])
	}

	if strings.Compare(strs[0], strinv[1]) <= 0 {
		dp[1][1] = c[1]
	}

	if strings.Compare(strinv[0], strinv[1]) <= 0 {
		dp[1][1] = min(c[1] + c[0], dp[1][1])
	}

	for i := 2; i < n; i++ {

		if strings.Compare(strs[i - 1], strs[i]) <= 0 && dp[i - 1][0] != math.MaxInt64 {
			dp[i][0] = dp[i - 1][0] + 0
		}
		if strings.Compare(strinv[i - 1], strs[i]) <= 0 && dp[i - 1][1] != math.MaxInt64 {
			dp[i][0] = min(dp[i - 1][1] + 0, dp[i][0])
		}

		if strings.Compare(strs[i - 1], strinv[i]) <= 0 && dp[i - 1][0] != math.MaxInt64 {
			dp[i][1] = dp[i - 1][0] + c[i]
		}

		if strings.Compare(strinv[i - 1], strinv[i]) <= 0 && dp[i - 1][1] != math.MaxInt64 {
			dp[i][1] = min(dp[i - 1][1] + c[i], dp[i][1])
		}

		if dp[i][0] == math.MaxInt64 && dp[i][1] == math.MaxInt64 {
			fmt.Println(-1)
			return
		}
	}

	if ans := min(dp[n - 1][0], dp[n - 1][1]); ans == math.MaxInt64 {
		fmt.Println(-1)
	} else {
		fmt.Println(ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

const EMPTY = -2

var (
	n  int
	c  []int64
	s  []string
	dp []int64
)

func reverseStr(s string) string {
	runes := []rune(s)
	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return string(runes)
}

func calcEnergy(start int, reverse bool) int64 {
	if start+1 >= len(c) {
		// For the last element the amount of energy needed is just that (or zero if not reversing)
		if reverse {
			return c[start]
		} else {
			return 0
		}
	}
	dpIdx := start
	if reverse {
		dpIdx += n
	}
	if dp[dpIdx] != EMPTY {
		return dp[dpIdx]
	}
	var res1 int64 = -1
	var res2 int64 = -1
	if reverse {
		if strings.Compare(reverseStr(s[start]), s[start+1]) <= 0 {
			res1 = calcEnergy(start+1, false)
			if res1 != -1 {
				res1 += c[start]
			}
		}
		if strings.Compare(reverseStr(s[start]), reverseStr(s[start+1])) <= 0 {
			res2 = calcEnergy(start+1, true)
			if res2 != -1 {
				res2 += c[start]
			}
		}
	} else {
		if strings.Compare(s[start], s[start+1]) <= 0 {
			res1 = calcEnergy(start+1, false)
		}
		if strings.Compare(s[start], reverseStr(s[start+1])) <= 0 {
			res2 = calcEnergy(start+1, true)
		}
	}
	dp[dpIdx] = minEnergy(res1, res2)
	return dp[dpIdx]
}

func minEnergy(en1 int64, en2 int64) int64 {
	if en2 != -1 && (en1 == -1 || en2 < en1) {
		return en2
	} else {
		return en1
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)

	fmt.Fscanf(reader, "%d ", &n)
	c = make([]int64, n)
	for i := range c {
		fmt.Fscanf(reader, "%d ", &c[i])
	}
	s = make([]string, n)
	for i := range s {
		fmt.Fscanf(reader, "%s ", &s[i])
	}
	dp = make([]int64, n*2)
	for i := range dp {
		dp[i] = EMPTY
	}

	res1 := calcEnergy(0, false)
	res2 := calcEnergy(0, true)
	fmt.Println(minEnergy(res1, res2))
}

package main

import (
	"bufio"
	. "container/heap"
	. "fmt"
	"io"
	"os"
)

type hPair struct {
	x int64
	y int
}
type pairHeap []hPair

func (h pairHeap) Len() int              { return len(h) }
func (h pairHeap) Less(i, j int) bool    { return h[i].x < h[j].x || h[i].x == h[j].x && h[i].y < h[j].y }
func (h pairHeap) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }
func (h *pairHeap) Push(v interface{})   { *h = append(*h, v.(hPair)) }
func (h *pairHeap) Pop() (v interface{}) { n := len(*h); *h, v = (*h)[:n-1], (*h)[n-1]; return }

// github.com/EndlessCheng/codeforces-go
func Sol706C(reader io.Reader, writer io.Writer) {
	const inf int64 = 1e18
	reverse := func(ss string) string {
		n := len(ss)
		s := make([]byte, n)
		for i := range s {
			s[i] = ss[n-1-i]
		}
		return string(s)
	}
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	costs := make([]int, n)
	for i := range costs {
		Fscan(in, &costs[i])
	}
	type neighbor struct {
		vertex int
		weight int
	}
	g := make([][]neighbor, 2*n+2)
	var prev, rPrev, cur, rCur string
	for i, c := range costs {
		Fscan(in, &cur)
		rCur = reverse(cur)
		if prev == "" {
			g[0] = []neighbor{{1, 0}, {2, c}}
		} else {
			if prev <= cur {
				g[2*i-1] = append(g[2*i-1], neighbor{2*i + 1, 0})
			}
			if prev <= rCur {
				g[2*i-1] = append(g[2*i-1], neighbor{2*i + 2, c})
			}
			if rPrev <= cur {
				g[2*i] = append(g[2*i], neighbor{2*i + 1, 0})
			}
			if rPrev <= rCur {
				g[2*i] = append(g[2*i], neighbor{2*i + 2, c})
			}
		}
		prev, rPrev = cur, rCur
	}
	g[2*n-1] = []neighbor{{2*n + 1, 0}}
	g[2*n] = []neighbor{{2*n + 1, 0}}

	dist := make([]int64, 2*n+2)
	for i := range dist {
		dist[i] = inf
	}
	dist[0] = 0
	visited := make([]bool, 2*n+2)
	h := &pairHeap{}
	Push(h, hPair{0, 0})
	for h.Len() > 0 {
		p := Pop(h).(hPair)
		v := p.y
		if visited[v] {
			continue
		}
		visited[v] = true
		for _, e := range g[v] {
			w := e.vertex
			if newDist := dist[v] + int64(e.weight); newDist < dist[w] {
				dist[w] = newDist
				Push(h, hPair{newDist, w})
			}
		}
	}
	ans := dist[2*n+1]
	if ans == inf {
		ans = -1
	}
	Fprintln(out, ans)
}

func main() {
	Sol706C(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"math"
	"strings"
)

/*
	2
	1 2
	ba
	ac
 */

 /**

  */

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanln(reader, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}
	fmt.Fscanln(reader)
	str := make([]string, n)
	reverse := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanln(reader, &str[i])
		reverse[i] = reverseString(str[i])
	}

	var ret [][]int64
	ret = make([][]int64, n)
	for i := 0; i < n; i++ {
		ret[i] = make([]int64, 2) // 0 表示没有reverse 1 表示reverse
	}

	ret[0][0] = 0
	ret[0][1] = int64(a[0])
	for i := 1; i < n; i++ {
		var ret0, ret1 int64 = math.MaxInt64, math.MaxInt64
		if ret[i-1][0] != math.MaxInt64 {
			if strings.Compare(str[i], str[i-1]) >= 0 {
				ret0 = int64(ret[i-1][0])
			}
			if strings.Compare(reverse[i], str[i-1]) >= 0{
				ret1 = int64(ret[i-1][0]) + int64(a[i])
			}
		}

		if ret[i-1][1] != math.MaxInt64 {
			if strings.Compare(str[i], reverse[i-1]) >= 0 {
				if ret[i-1][1] < ret0 {
					ret0 = ret[i-1][1]
				}
			}
			if strings.Compare(reverse[i], reverse[i-1]) >= 0{
				if ret[i-1][1] + int64(a[i]) < ret1 {
					ret1 = ret[i-1][1] + int64(a[i])
				}
			}
		}
		ret[i][0]=ret0
		ret[i][1]=ret1
	}
	if ret[n-1][0] == math.MaxInt64 && ret[n-1][1] == math.MaxInt64 {
		fmt.Println(-1)
	} else {
		if ret[n-1][0] < ret[n-1][1] {
			fmt.Println(ret[n-1][0])
		} else {
			fmt.Println(ret[n-1][1])
		}
	}
	//fmt.Println(ret)
}

func reverseString(str string) string {
	ret := make([]byte, len(str))
	count := len(str)
	for i := 0; i < len(str); i++ {
		ret[i] = str[count - i - 1]
	}
	return string(ret)
}

package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
	"math"
)

func main() {
	var n int
	fmt.Scan(&n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	costs := make([]int64, n+1)
	for i:=0;i<n;i++ {
		costs[i+1] = readInt64(scanner)
	}
	words := make([]string, n+1)
	reverseWords := make([]string, n+1)
	words[0] = ""
	reverseWords[0] = ""
	for i:=1;i<=n;i++ {
		words[i] = readString(scanner)
		reverseWords[i] = reverse(words[i])
	}
	DP := make([][]int64, n+1)
	DP[0] = make([]int64, 2)
	for i:=1;i<=n;i++ {
		DP[i] = make([]int64, 2)
		DP[i][0] = math.MaxInt64
		DP[i][1] = math.MaxInt64
	}
	for i:=1;i<=n;i++ {
		was := false
		if words[i] >= words[i-1] {
			DP[i][0] = DP[i-1][0]
			was = true
		}
		if words[i] >= reverseWords[i-1] {
			DP[i][0] = min(DP[i][0], DP[i-1][1])
			was = true
		}
		if reverseWords[i] >= words[i-1] {
			DP[i][1] = DP[i-1][0] + costs[i]
			was = true
		}
		if reverseWords[i] >= reverseWords[i-1] {
			DP[i][1] = min(DP[i][1], DP[i-1][1] + costs[i])
			was = true
		}
		if !was {
			fmt.Println("-1")
			return
		}
	}
	fmt.Println(min(DP[n][0], DP[n][1]))
}

func min(a, b int64) int64 {
	if a<b {
		return a
	}
	return b
}

func reverse(s string) string {
	r := []byte(s)
	i:=0
	j:= len(s) - 1
	for i<j {
		r[i], r[j] = r[j], r[i]
		i++
		j--
	}
	return string(r)
}

func readString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func readInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	value, _ := strconv.Atoi(scanner.Text())
	return value
}

func readInt64(scanner *bufio.Scanner) int64 {
	scanner.Scan()
	value, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return value
}

// http://codeforces.com/problemset/problem/706/C

package main

import (
	"fmt"
	"bufio"
	"os"
	"strings"
	"math"
)

func min (a, b int64) int64 {
	if a < b {
		return a
	}

	return b
}

func min3 (a, b, c int64) int64 {
	return min(min(a, b), c)
}

func InvStr(str string) string {
	data := []byte(str)
	n := len(data)
	for i := 0; i <= (n - 1)/ 2; i++ {
		data[i], data[n - i - 1] = data[n - i - 1], data[i]
	}

	return string(data)
}

func main() {
	var n int
	var tmp int64

	input := bufio.NewReaderSize(os.Stdin, 1 << 20)

	fmt.Fscanln(input, &n)
	c := make([]int64, n)
	strs := make([]string, n)
	strinv := make([]string, n)
	dp := make([][]int64, n)

	for i := 0; i < n; i++ {
		if i == n - 1 {
			fmt.Fscanln(input, &tmp)
		} else {
			fmt.Fscan(input, &tmp)
		}
		c[i] = tmp
		dp[i] = make([]int64, 2)
		dp[i][0], dp[i][1] = math.MaxInt64, math.MaxInt64
	}

	for i := 0; i < n; i++ {
		var str string
		fmt.Fscanln(input, &str)

		strs[i] = str
		strinv[i] = InvStr(str)
	}

	// Solve
	if strings.Compare(strs[0], strs[1]) < 0 {
		dp[1][0] = 0
	}
	if strings.Compare(strinv[0], strs[1]) < 0 {
		dp[1][0] = min(c[0], dp[1][0])
	}

	if strings.Compare(strs[0], strinv[1]) < 0 {
		dp[1][1] = c[1]
	}

	if strings.Compare(strinv[0], strinv[1]) < 0 {
		dp[1][1] = min(c[1] + c[0], dp[1][1])
	}

	for i := 2; i < n; i++ {

		if strings.Compare(strs[i - 1], strs[i]) < 0 && dp[i - 1][0] != math.MaxInt64 {
			dp[i][0] = dp[i - 1][0] + 0
		}
		if strings.Compare(strinv[i - 1], strs[i]) < 0 && dp[i - 1][1] != math.MaxInt64 {
			dp[i][0] = min(dp[i - 1][1] + 0, dp[i][0])
		}

		if strings.Compare(strs[i - 1], strinv[i]) < 0 && dp[i - 1][0] != math.MaxInt64 {
			dp[i][1] = dp[i - 1][0] + c[i]
		}

		if strings.Compare(strinv[i - 1], strinv[i]) < 0 && dp[i - 1][1] != math.MaxInt64 {
			dp[i][1] = min(dp[i - 1][1] + c[i], dp[i][1])
		}

		if dp[i][0] == math.MaxInt64 && dp[i][1] == math.MaxInt64 {
			fmt.Println(-1)
			return
		}
	}

	if ans := min(dp[n - 1][0], dp[n - 1][1]); ans == math.MaxInt64 {
		fmt.Println(-1)
	} else {
		fmt.Println(ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

const EMPTY = -2

var (
	n  int
	c  []int64
	s  []string
	dp []int64
)

func reverseStr(s string) string {
	runes := []rune(s)
	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return string(runes)
}

func calcEnergy(start int, reverse bool) int64 {
	if start+1 >= len(c) {
		// For the last element the amount of energy needed is just that (or zero if not reversing)
		if reverse {
			return c[start]
		} else {
			return 0
		}
	}
	dpIdx := start
	if reverse {
		dpIdx += n
	}
	if dp[dpIdx] != EMPTY {
		return dp[dpIdx]
	}
	var res1 int64 = -1
	var res2 int64 = -1
	if reverse {
		if strings.Compare(reverseStr(s[start]), s[start+1]) <= 0 {
			res1 = c[start] + calcEnergy(start+1, false)
		}
		if strings.Compare(reverseStr(s[start]), reverseStr(s[start+1])) <= 0 {
			res2 = c[start] + calcEnergy(start+1, true)
		}
	} else {
		if strings.Compare(s[start], s[start+1]) <= 0 {
			res1 = calcEnergy(start+1, false)
		}
		if strings.Compare(s[start], reverseStr(s[start+1])) <= 0 {
			res2 = calcEnergy(start+1, true)
		}
	}
	dp[dpIdx] = minEnergy(res1, res2)
	return dp[dpIdx]
}

func minEnergy(en1 int64, en2 int64) int64 {
	if en2 != -1 && (en1 == -1 || en2 < en1) {
		return en2
	} else {
		return en1
	}
}

func main() {
	reader := bufio.NewReader(os.Stdin)

	fmt.Fscanf(reader, "%d ", &n)
	c = make([]int64, n)
	for i := range c {
		fmt.Fscanf(reader, "%d ", &c[i])
	}
	s = make([]string, n)
	for i := range s {
		fmt.Fscanf(reader, "%s ", &s[i])
	}
	dp = make([]int64, n*2)
	for i := range dp {
		dp[i] = EMPTY
	}

	res1 := calcEnergy(0, false)
	res2 := calcEnergy(0, true)
	fmt.Println(minEnergy(res1, res2))
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"math"
	"strings"
)

/*
	2
	1 2
	ba
	ac
 */

 /**

  */

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanln(reader, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}
	fmt.Fscanln(reader)
	str := make([]string, n)
	reverse := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanln(reader, &str[i])
		reverse[i] = reverseString(str[i])
	}

	var ret [][]int
	ret = make([][]int, n)
	for i := 0; i < n; i++ {
		ret[i] = make([]int, 2) // 0 表示没有reverse 1 表示reverse
	}

	ret[0][0] = 0
	ret[0][1] = a[0]
	for i := 1; i < n; i++ {
		var ret0, ret1 = math.MaxInt32, math.MaxInt32
		if ret[i-1][0] != math.MaxInt32 {
			if strings.Compare(str[i], str[i-1]) >= 0 {
				ret0 = ret[i-1][0]
			}
			if strings.Compare(reverse[i], str[i-1]) >= 0{
				ret1 = ret[i-1][0] + a[i]
			}
		}

		if ret[i-1][1] != math.MaxInt32 {
			if strings.Compare(str[i], reverse[i-1]) >= 0 {
				if ret[i-1][1] < ret0 {
					ret0 = ret[i-1][1]
				}
			}
			if strings.Compare(reverse[i], reverse[i-1]) >= 0{
				if ret[i-1][1] + a[i] < ret1 {
					ret1 = ret[i-1][1] + a[i]
				}
			}
		}
		ret[i][0]=ret0
		ret[i][1]=ret1
	}
	if ret[n-1][0] == math.MaxInt32 && ret[n-1][1] == math.MaxInt32 {
		fmt.Println(-1)
	} else {
		if ret[n-1][0] < ret[n-1][1] {
			fmt.Println(ret[n-1][0])
		} else {
			fmt.Println(ret[n-1][1])
		}
	}
	//fmt.Println(ret)
}

func reverseString(str string) string {
	ret := make([]byte, len(str))
	count := len(str)
	for i := 0; i < len(str); i++ {
		ret[i] = str[count - i - 1]
	}
	return string(ret)
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"math"
	"strings"
)

/*
	2
	1 2
	ba
	ac
 */

 /**

  */

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanln(reader, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}
	fmt.Fscanln(reader)
	str := make([]string, n)
	reverse := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanln(reader, &str[i])
		reverse[i] = reverseString(str[i])
	}

	var ret [][]int
	ret = make([][]int, n)
	for i := 0; i < n; i++ {
		ret[i] = make([]int, 2) // 0 表示没有reverse 1 表示reverse
	}

	ret[0][0] = 0
	ret[0][1] = a[0]
	for i := 1; i < n; i++ {
		var ret0, ret1 = math.MaxInt32, math.MaxInt32
		if ret[i-1][0] != math.MaxInt32 {
			if strings.Compare(str[i], str[i-1]) > 0 {
				ret0 = ret[i-1][0]
			} else if strings.Compare(reverse[i], str[i-1]) > 0{
				ret1 = ret[i-1][0] + a[i]
			}
		}

		if ret[i-1][1] != math.MaxInt32 {
			if strings.Compare(str[i], reverse[i-1]) > 0 {
				if ret[i-1][1] < ret0 {
					ret0 = ret[i-1][1]
				}
			} else if strings.Compare(reverse[i], reverse[i-1]) > 0{
				if ret[i-1][1] + a[i] < ret1 {
					ret1 = ret[i-1][1] + a[i]
				}
			}
		}
		ret[i][0]=ret0
		ret[i][1]=ret1
	}
	if ret[n-1][0] == math.MaxInt32 && ret[n-1][1] == math.MaxInt32 {
		fmt.Println(-1)
	} else {
		if ret[n-1][0] < ret[n-1][1] {
			fmt.Println(ret[n-1][0])
		} else {
			fmt.Println(ret[n-1][1])
		}
	}
	//fmt.Println(ret)
}

func reverseString(str string) string {
	ret := make([]byte, len(str))
	count := len(str)
	for i := 0; i < len(str); i++ {
		ret[i] = str[count - i - 1]
	}
	return string(ret)
}

package main

import (
	"bufio"
	"os"
	"fmt"
	"math"
	"strings"
)

/*
	2
	1 2
	ba
	ac
 */

 /**

  */

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanln(reader, &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}
	fmt.Fscanln(reader)
	str := make([]string, n)
	reverse := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanln(reader, &str[i])
		reverse[i] = reverseString(str[i])
	}

	var ret [][]int64
	ret = make([][]int64, n)
	for i := 0; i < n; i++ {
		ret[i] = make([]int64, 2) // 0 表示没有reverse 1 表示reverse
	}

	ret[0][0] = 0
	ret[0][1] = int64(a[0])
	for i := 1; i < n; i++ {
		var ret0, ret1 int64 = math.MaxInt64, math.MaxInt64
		if ret[i-1][0] != math.MaxInt32 {
			if strings.Compare(str[i], str[i-1]) >= 0 {
				ret0 = int64(ret[i-1][0])
			}
			if strings.Compare(reverse[i], str[i-1]) >= 0{
				ret1 = int64(ret[i-1][0]) + int64(a[i])
			}
		}

		if ret[i-1][1] != math.MaxInt32 {
			if strings.Compare(str[i], reverse[i-1]) >= 0 {
				if ret[i-1][1] < ret0 {
					ret0 = ret[i-1][1]
				}
			}
			if strings.Compare(reverse[i], reverse[i-1]) >= 0{
				if ret[i-1][1] + int64(a[i]) < ret1 {
					ret1 = ret[i-1][1] + int64(a[i])
				}
			}
		}
		ret[i][0]=ret0
		ret[i][1]=ret1
	}
	if ret[n-1][0] == math.MaxInt32 && ret[n-1][1] == math.MaxInt32 {
		fmt.Println(-1)
	} else {
		if ret[n-1][0] < ret[n-1][1] {
			fmt.Println(ret[n-1][0])
		} else {
			fmt.Println(ret[n-1][1])
		}
	}
	//fmt.Println(ret)
}

func reverseString(str string) string {
	ret := make([]byte, len(str))
	count := len(str)
	for i := 0; i < len(str); i++ {
		ret[i] = str[count - i - 1]
	}
	return string(ret)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const (
	MAX_N = 100100
)

var INF int64 = 1 << 50
var N int
var C [MAX_N]int64
var words [MAX_N]string
var rev_words [MAX_N]string

func Reverse(value string) string {
	data := []rune(value)
	result := []rune{}
	for i := len(data) - 1; i >= 0; i-- {
		result = append(result, data[i])
	}
	return string(result)
}

var dp [MAX_N][2]int64

func Compare(s, t string) bool {
	n := len(s)
	m := len(t)
	k := IntMin(n, m)
	for i := 0; i < k; i++ {
		if s[i] == t[i] {
			continue
		}
		return s[i] < t[i]
	}
	if n < m {
		return true
	}
	return false
}
func memo(pos, rev int) int64 {

	if pos == N {
		return 0
	}
	if dp[pos][rev] >= 0 {
		return dp[pos][rev]
	}
	var res int64
	res = INF
	// fmt.Println(res)
	if rev == 1 {
		if Compare(rev_words[pos-1], words[pos]) {
			res = Int64Min(res, memo(pos+1, 0))
		}
		if Compare(rev_words[pos-1], rev_words[pos]) {
			res = Int64Min(res, memo(pos+1, 1)+C[pos])
		}
	} else {
		if Compare(words[pos-1], words[pos]) {
			res = Int64Min(res, memo(pos+1, 0))
		}
		if Compare(words[pos-1], rev_words[pos]) {
			res = Int64Min(res, memo(pos+1, 1)+C[pos])
		}
	}
	// fmt.Println(pos, rev, res)
	dp[pos][rev] = res
	return res
}
func init() {
	for i := 0; i < MAX_N; i++ {
		for j := 0; j < len(dp[i]); j++ {
			dp[i][j] = -1
		}
	}
}
func main() {
	fsc := NewFastScanner()
	N = fsc.NextInt()
	for i := 0; i < N; i++ {
		C[i] = fsc.NextInt64()
	}
	for i := 0; i < N; i++ {
		words[i] = fsc.Next()
		rev_words[i] = Reverse(words[i])
	}
	var result int64
	result = INF
	result = Int64Min(result, memo(1, 0))
	result = Int64Min(result, memo(1, 1)+C[0])
	if result >= INF {
		result = -1
	}
	fmt.Println(result)
}

//template
type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1024)
	return &FastScanner{r: rdr}
}
func (s *FastScanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *FastScanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *FastScanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *FastScanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *FastScanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *FastScanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

//Max,Min
func IntMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func Int64Max(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func Float64Max(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

func IntMin(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func Int64Min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}
func Float64Min(a, b float64) float64 {
	if a > b {
		return b
	}
	return a
}

//Gcd
func IntGcd(a, b int) int {
	if a < b {
		b, a = a, b
	}
	if b == 0 {
		return a
	}
	return IntGcd(b, a%b)
}
func Int64Gcd(a, b int64) int64 {
	if a < b {
		b, a = a, b
	}
	if b == 0 {
		return a
	}
	return Int64Gcd(b, a%b)
}


package main

import (
	"container/heap"
	"fmt"
	"bufio"
	"strconv"
	"os"
	"bytes"
)

var sc *bufio.Scanner

func I() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}

type Flight struct {
	cost, time int
}

type FHeap []Flight

func (h FHeap) Len() int { return len(h) }
func (h FHeap) Less(i, j int) bool {
	return h[i].cost > h[j].cost
}
func (h FHeap) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}
func (h *FHeap) Push(f interface{}) {
	*h = append(*h, f.(Flight))
}

func (h *FHeap) Pop() interface{} {
	old := *h
	n := len(old) - 1
	*h = old[0: n]
	return old[n]
}

func main() {
	sc = bufio.NewScanner(os.Stdin)
	sc.Buffer(make([]byte, 0, 1<<20), 1<<20)
	sc.Split(bufio.ScanWords)
	n, k := I(), I()
	m := k
	if m > n {
		m = n
	}
	h, t, res := make(FHeap, m), 0, int64(0)
	for ; t < m; t++ {
		h[t] = Flight{I(), t}
	}
	heap.Init(&h)
	timing := make([]int, n)
	for ; t < n; t++ {
		heap.Push(&h, Flight{I(), t})
		f := heap.Pop(&h).(Flight)
		timing[f.time] = t
		res += int64(t-f.time) * int64(f.cost)
	}
	k += n
	for ; t < k; t++ {
		f := heap.Pop(&h).(Flight)
		timing[f.time] = t
		res += int64(t-f.time) * int64(f.cost)
	}
	fmt.Println(res)
	var buffer bytes.Buffer
	for _, t := range timing {
		buffer.WriteString(strconv.Itoa(t + 1))
		buffer.WriteString(" ")
	}
	fmt.Println(buffer.String())
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"math"
	"os"
	"strconv"
)

func solve() {
	n := readInt()
	k := readInt()
	heap := NewMinHeap(n)
	cost := int64(0)
	a := make([]int, n)
	for i := 0; i < n+k; i++ {
		if i < n {
			heap.Add(i, -readInt())
		}
		if i >= k {
			index := heap.ArgMin()
			value := -heap.Min()
			a[index] = i + 1
			heap.Remove(index)
			cost += int64(i-index) * int64(value)
		}
	}
	fmt.Println(cost)
	var buffer bytes.Buffer
	for i := 0; i < n; i++ {
		if i > 0 {
			buffer.WriteString(" ")
		}
		buffer.WriteString(strconv.Itoa(a[i]))
	}
	fmt.Println(buffer.String())
}

type MinHeap struct {
	a    []int
	vmap []int
	imap []int
	n    int
	pos  int
}

const INF = math.MaxInt32

func NewMinHeap(m int) *MinHeap {
	heap := &MinHeap{}
	m += 2
	heap.n = m
	heap.a = make([]int, heap.n)
	heap.vmap = make([]int, heap.n)
	heap.imap = make([]int, heap.n)
	heap.pos = 1
	for i := 0; i < heap.n; i++ {
		heap.a[i] = INF
		heap.vmap[i] = -1
		heap.imap[i] = -1
	}
	return heap
}

func (h *MinHeap) Add(ind, x int) int {
	ret := h.imap[ind]
	if h.imap[ind] < 0 {
		h.a[h.pos] = x
		h.vmap[h.pos] = ind
		h.imap[ind] = h.pos
		h.pos++
		h.up(h.pos - 1)
	}
	if ret != -1 {
		return h.a[ret]
	}
	return x
}

func (h *MinHeap) Update(ind, x int) int {
	ret := h.imap[ind]
	if h.imap[ind] < 0 {
		h.a[h.pos] = x
		h.vmap[h.pos] = ind
		h.imap[ind] = h.pos
		h.pos++
		h.up(h.pos - 1)
	} else {
		h.a[ret] = x
		h.up(ret)
		h.down(ret)
	}
	return x
}

func (h *MinHeap) Remove(ind int) int {
	if h.pos == 1 {
		return INF
	}
	if h.imap[ind] == -1 {
		return INF
	}

	h.pos--
	rem := h.imap[ind]
	ret := h.a[rem]
	h.vmap[rem] = h.vmap[h.pos]
	h.imap[h.vmap[h.pos]] = rem
	h.imap[ind] = -1
	h.a[rem] = h.a[h.pos]
	h.a[h.pos] = INF
	h.vmap[h.pos] = -1

	h.up(rem)
	h.down(rem)
	return ret
}

func (h *MinHeap) Min() int {
	return h.a[1]
}

func (h *MinHeap) ArgMin() int {
	return h.vmap[1]
}

func (h *MinHeap) Size() int {
	return h.pos - 1
}

func (h *MinHeap) up(cur int) {
	for c, p := cur, cur>>1; p >= 1 && h.a[p] > h.a[c]; c, p = c>>1, p>>1 {
		h.a[p], h.a[c] = h.a[c], h.a[p]
		h.imap[h.vmap[p]], h.imap[h.vmap[c]] = h.imap[h.vmap[c]], h.imap[h.vmap[p]]
		h.vmap[p], h.vmap[c] = h.vmap[c], h.vmap[p]
	}
}

func (h *MinHeap) down(cur int) {
	for c := cur; 2*c < h.pos; {
		b := 0
		if h.a[2*c] < h.a[2*c+1] {
			b = 2 * c
		} else {
			b = 2*c + 1
		}
		if h.a[b] < h.a[c] {
			h.a[c], h.a[b] = h.a[b], h.a[c]
			h.imap[h.vmap[c]], h.imap[h.vmap[b]] = h.imap[h.vmap[b]], h.imap[h.vmap[c]]
			h.vmap[c], h.vmap[b] = h.vmap[b], h.vmap[c]
			c = b
		} else {
			break
		}
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"sort"
	"strconv"
)

type flight struct {
	t, c int64
}

var k int64

func solve() {
	n := readInt()
	k = readInt64()
	flights := make([]flight, n)
	ft := make([]int64, n)
	for i := 0; i < n; i++ {
		flights[i] = flight{int64(i + 1), readInt64()}
	}
	sort.Sort(ByCost(flights))
	cost := int64(0)
	for i := 0; i < n; i++ {
		c := (int64(i) + 1 - flights[i].t + k) * flights[i].c
		cost += c
		ft[flights[i].t-1] = (int64(i) + 1 + k)
	}
	fmt.Println(cost)
	var buffer bytes.Buffer
	for i := 0; i < n; i++ {
		if i > 0 {
			buffer.WriteString(" ")
		}
		buffer.WriteString(strconv.FormatInt(ft[i], 10))
	}
	fmt.Println(buffer.String())
}

type ByCost []flight

func (a ByCost) Len() int      { return len(a) }
func (a ByCost) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByCost) Less(i, j int) bool {
	c0 := (int64(i) + 1 - a[i].t + k) * a[i].c
	c1 := (int64(j) + 1 - a[j].t + k) * a[j].c
	return c0 > c1
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}


package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

// 321E. Ciel and Gondolas
// Using opt[i - 1][j] <= opt[i][j] <= opt[i][j + 1]
// O(n^2)

const inf = int(1e9)

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n, k := io.nextInt(), io.nextInt()
	u := make([][]int, n+1)
	u[0] = make([]int, n+1)
	for i := 1; i <= n; i++ {
		u[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			u[i][j] = io.nextInt()
			if i > j {
				u[i][j] = 0
			}
			u[i][j] += u[i-1][j] + u[i][j-1] - u[i-1][j-1]
		}
	}

	sum := func(l, r int) int { return u[r][r] - u[r][l-1] - u[l-1][r] + u[l-1][l-1] }

	dp := make([][]int, k+1)
	opt := make([][]int, k+1)
	for i := 0; i <= k; i++ {
		dp[i] = make([]int, n+1)
		opt[i] = make([]int, n+1)
	}

	for j := 1; j <= n; j++ {
		dp[1][j] = sum(1, j)
	}

	for i := 2; i <= k; i++ {
		lim := n - 1
		for j := n; j > 0; j-- {
			dp[i][j] = inf
			for x := opt[i-1][j]; x <= lim && x < j; x++ {
				if dp[i][j] > dp[i-1][x]+sum(x+1, j) {
					dp[i][j] = dp[i-1][x] + sum(x+1, j)
					opt[i][j] = x
				}
			}
			lim = opt[i][j]
		}
	}

	io.println(dp[k][n])
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p []byte
}

func newFastIO(r io.Reader, w io.Writer) fastIO {
	return fastIO{bufio.NewReader(r), bufio.NewWriter(w), make([]byte, 1)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p)
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p)
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p)
	}
	return n
}

func (f *fastIO) next() string {
	f.Read(f.p)
	for f.p[0] <= ' ' {
		f.Read(f.p)
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p)
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

// 321E. Ciel and Gondolas
// Using opt[i - 1][j] <= opt[i][j] <= opt[i][j + 1]
// O(n^2)

const inf = int(1e9)

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n, k := io.NextInt(), io.NextInt()
	u := make([][]int, n+1)
	u[0] = make([]int, n+1)
	for i := 1; i <= n; i++ {
		u[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			u[i][j] = io.NextInt()
			if i > j {
				u[i][j] = 0
			}
			u[i][j] += u[i-1][j] + u[i][j-1] - u[i-1][j-1]
		}
	}

	sum := func(l, r int) int { return u[r][r] - u[r][l-1] - u[l-1][r] + u[l-1][l-1] }

	dp := make([][]int, k+1)
	opt := make([][]int, k+1)
	for i := 0; i <= k; i++ {
		dp[i] = make([]int, n+1)
		opt[i] = make([]int, n+1)
	}

	for j := 1; j <= n; j++ {
		dp[1][j] = sum(1, j)
	}

	for i := 2; i <= k; i++ {
		lim := n - 1
		for j := n; j > 0; j-- {
			dp[i][j] = inf
			for x := opt[i-1][j]; x <= lim && x < j; x++ {
				if dp[i][j] > dp[i-1][x]+sum(x+1, j) {
					dp[i][j] = dp[i-1][x] + sum(x+1, j)
					opt[i][j] = x
				}
			}
			lim = opt[i][j]
		}
	}

	io.Println(dp[k][n])
}

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
}

func NewFastIO(r io.Reader, w io.Writer) FastIO {
	return FastIO{bufio.NewReader(r), bufio.NewWriter(w)}
}

func (f *FastIO) NextInt() int {
	p := make([]byte, 1)
	for {
		f.Read(p)
		if '0' <= p[0] && p[0] <= '9' {
			break
		}
	}
	n := 0
	for '0' <= p[0] && p[0] <= '9' {
		n = n*10 + int(p[0]) - '0'
		f.Read(p)
	}
	return n
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

// 321E. Ciel and Gondolas
// Using opt[i - 1][j] <= opt[i][j] <= opt[i][j + 1]
// O(n^2)

const inf = int(1e9)

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n, k := io.NextInt(), io.NextInt()
	u := make([][]int, n+1)
	u[0] = make([]int, n+1)
	for i := 1; i <= n; i++ {
		u[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			u[i][j] = io.NextInt()
			if i > j {
				u[i][j] = 0
			}
			u[i][j] += u[i-1][j] + u[i][j-1] - u[i-1][j-1]
		}
	}

	sum := func(l, r int) int { return u[r][r] - u[r][l-1] - u[l-1][r] + u[l-1][l-1] }

	dp := make([][]int, k+1)
	opt := make([][]int, k+1)
	for i := 0; i <= k; i++ {
		dp[i] = make([]int, n+1)
		opt[i] = make([]int, n+1)
	}

	for j := 1; j <= n; j++ {
		dp[1][j] = sum(1, j)
	}

	for i := 2; i <= k; i++ {
		lim := n - 1
		for j := n; j > 0; j-- {
			dp[i][j] = inf
			for x := opt[i-1][j]; x <= lim && x < j; x++ {
				if dp[i][j] > dp[i-1][x]+sum(x+1, j) {
					dp[i][j] = dp[i-1][x] + sum(x+1, j)
					opt[i][j] = x
				}
			}
			lim = opt[i][j]
		}
	}

	io.Println(dp[k][n])
}

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	c [1]byte
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	f.Read(f.c[:])
	return f.c[0]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, f.c[0])
	}
	return string(s)
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(' ', x)
		}
	}
}

func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

// 321E. Ciel and Gondolas
// Using opt[i - 1][j] <= opt[i][j] <= opt[i][j + 1]
// O(n^2)

const inf = int(1e9)

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n, k := io.NextInt(), io.NextInt()
	u := make([][]int, n+1)
	u[0] = make([]int, n+1)
	for i := 1; i <= n; i++ {
		u[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			u[i][j] = io.NextInt()
			if i > j {
				u[i][j] = 0
			}
			u[i][j] += u[i-1][j] + u[i][j-1] - u[i-1][j-1]
		}
	}

	sum := func(l, r int) int { return u[r][r] - u[r][l-1] - u[l-1][r] + u[l-1][l-1] }

	dp := make([][]int, k+1)
	opt := make([][]int, k+1)
	for i := 0; i <= k; i++ {
		dp[i] = make([]int, n+1)
		opt[i] = make([]int, n+1)
	}

	for j := 1; j <= n; j++ {
		dp[1][j] = sum(1, j)
	}

	for i := 2; i <= k; i++ {
		lim := n - 1
		for j := n; j > 0; j-- {
			dp[i][j] = inf
			for x := opt[i-1][j]; x <= lim && x < j; x++ {
				if dp[i][j] > dp[i-1][x]+sum(x+1, j) {
					dp[i][j] = dp[i-1][x] + sum(x+1, j)
					opt[i][j] = x
				}
			}
			lim = opt[i][j]
		}
	}

	io.Println(dp[k][n])
}

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
	p []byte
}

func NewFastIO(r io.Reader, w io.Writer) FastIO {
	return FastIO{bufio.NewReader(r), bufio.NewWriter(w), make([]byte, 1)}
}

func (f *FastIO) NextInt() int {
	f.Read(f.p)
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p)
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p)
	}
	return n
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

// 321E. Ciel and Gondolas
// Using opt[i - 1][j] <= opt[i][j] <= opt[i][j + 1]
// O(n^2)

const inf = int(1e9)

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n, k := io.nextInt(), io.nextInt()
	u := make([][]int, n+1)
	u[0] = make([]int, n+1)
	for i := 1; i <= n; i++ {
		u[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			u[i][j] = io.nextInt()
			if i > j {
				u[i][j] = 0
			}
			u[i][j] += u[i-1][j] + u[i][j-1] - u[i-1][j-1]
		}
	}

	sum := func(l, r int) int { return u[r][r] - u[r][l-1] - u[l-1][r] + u[l-1][l-1] }

	dp := make([][]int, k+1)
	opt := make([][]int, k+1)
	for i := 0; i <= k; i++ {
		dp[i] = make([]int, n+1)
		opt[i] = make([]int, n+1)
	}

	for j := 1; j <= n; j++ {
		dp[1][j] = sum(1, j)
	}

	for i := 2; i <= k; i++ {
		lim := n - 1
		for j := n; j > 0; j-- {
			dp[i][j] = inf
			for x := opt[i-1][j]; x <= lim && x < j; x++ {
				if dp[i][j] > dp[i-1][x]+sum(x+1, j) {
					dp[i][j] = dp[i-1][x] + sum(x+1, j)
					opt[i][j] = x
				}
			}
			lim = opt[i][j]
		}
	}

	io.println(dp[k][n])
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReaderSize(r, 1<<20), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	return n
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

package main

import (
	"os"
	"strconv"
)

// M ...
const M = 4010

var (
	n, m int
	a, w [M][M]int
	f, g [M][M]int
)

func readnumbers() (nums []int) {
	fileInfo, _ := os.Stdin.Stat()
	p := make([]byte, fileInfo.Size())
	os.Stdin.Read(p)

	num, neg := 0, 1
	for i := 0; i < len(p); i++ {
		if p[i] >= '0' {
			num = 10*num + int(p[i]-'0')
		} else if p[i] == '-' {
			neg = -1
		} else if p[i] != '\r' {
			nums = append(nums, neg*num)
			num, neg = 0, 1
		}
	}

	if len(p) > 0 && p[len(p)-1] >= '0' {
		nums = append(nums, neg*num)
	}

	return
}

func main() {
	nums := readnumbers()
	idx := 0

	n, m = nums[idx], nums[idx+1]
	idx += 2
	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			a[i][j] = nums[idx]
			idx++
			a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1]
		}
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			w[i][j] = a[j][j] - a[i-1][j] - a[j][i-1] + a[i-1][i-1]
		}
	}

	for j := 1; j <= n; j++ {
		f[1][j] = w[1][j]
		g[1][j] = 1
	}

	for i := 2; i <= m; i++ {
		g[i][n+1] = n
		for j := n; j >= i; j-- {
			f[i][j] = int(1e9)
			for k := g[i-1][j]; k <= g[i][j+1] && k <= j; k++ {
				if f[i-1][k-1]+w[k][j] < f[i][j] {
					f[i][j] = f[i-1][k-1] + w[k][j]
					g[i][j] = k
				}
			}
		}
	}

	os.Stdout.Write([]byte(strconv.Itoa(f[m][n] / 2)))
}

package main

import (
	"os"
	"strconv"
)

// M ...
const M = 4010

var (
	a, w [M][M]int
	f, g [M][M]int
)

func readnumbers() (nums []int) {
	fileInfo, _ := os.Stdin.Stat()
	p := make([]byte, fileInfo.Size())
	os.Stdin.Read(p)

	num := 0
	for i := 0; i < len(p); i++ {
		if p[i] >= '0' {
			num = 10*num + int(p[i]-'0')
		} else if p[i] != '\r' {
			nums = append(nums, num)
			num = 0
		}
	}

	if len(p) > 0 && p[len(p)-1] >= '0' {
		nums = append(nums, num)
	}

	return
}

func main() {
	fileInfo, _ := os.Stdin.Stat()
	p, idx := make([]byte, fileInfo.Size()), 0
	os.Stdin.Read(p)

	n, m := 0, 0
	for ; p[idx] >= '0'; idx++ {
		n *= 10
		n += int(p[idx] - '0')
	}
	idx++
	for ; p[idx] >= '0'; idx++ {
		m *= 10
		m += int(p[idx] - '0')
	}
	idx += 2

	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			a[i][j] = int(p[idx] - '0')
			idx += 2
			a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1]
		}
		idx++
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			w[i][j] = a[j][j] - a[i-1][j] - a[j][i-1] + a[i-1][i-1]
		}
	}

	for j := 1; j <= n; j++ {
		f[1][j] = w[1][j]
		g[1][j] = 1
	}

	for i := 2; i <= m; i++ {
		g[i][n+1] = n
		for j := n; j >= i; j-- {
			f[i][j] = int(1e9)
			for k := g[i-1][j]; k <= g[i][j+1] && k <= j; k++ {
				if f[i-1][k-1]+w[k][j] < f[i][j] {
					f[i][j] = f[i-1][k-1] + w[k][j]
					g[i][j] = k
				}
			}
		}
	}

	os.Stdout.Write([]byte(strconv.Itoa(f[m][n] / 2)))
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

// 321E. Ciel and Gondolas
// Using opt[i - 1][j] <= opt[i][j] <= opt[i][j + 1]
// O(n^2)

const inf = int(1e9)

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n, k := io.NextInt(), io.NextInt()
	u := make([][]int, n+1)
	u[0] = make([]int, n+1)
	for i := 1; i <= n; i++ {
		u[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			u[i][j] = io.NextInt()
			if i > j {
				u[i][j] = 0
			}
			u[i][j] += u[i-1][j] + u[i][j-1] - u[i-1][j-1]
		}
	}

	sum := func(l, r int) int { return u[r][r] - u[r][l-1] - u[l-1][r] + u[l-1][l-1] }

	dp := make([][]int, k+1)
	opt := make([][]int, k+1)
	for i := 0; i <= k; i++ {
		dp[i] = make([]int, n+1)
		opt[i] = make([]int, n+1)
	}

	for j := 1; j <= n; j++ {
		dp[1][j] = sum(1, j)
	}

	for i := 2; i <= k; i++ {
		lim := n - 1
		for j := n; j > 0; j-- {
			dp[i][j] = inf
			for x := opt[i-1][j]; x <= lim && x < j; x++ {
				if dp[i][j] > dp[i-1][x]+sum(x+1, j) {
					dp[i][j] = dp[i-1][x] + sum(x+1, j)
					opt[i][j] = x
				}
			}
			lim = opt[i][j]
		}
	}

	io.Println(dp[k][n])
}

type FastIO struct {
	*bufio.Reader
	*bufio.Writer
}

func NewFastIO(r io.Reader, w io.Writer) FastIO {
	return FastIO{bufio.NewReader(r), bufio.NewWriter(w)}
}

func (f *FastIO) NextInt() int {
	p := make([]byte, 1)
	f.Read(p)
	for !('0' <= p[0] && p[0] <= '9') {
		f.Read(p)
	}
	n := 0
	for '0' <= p[0] && p[0] <= '9' {
		n = n*10 + int(p[0]) - '0'
		f.Read(p)
	}
	return n
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

// 321E. Ciel and Gondolas
// Using opt[i - 1][j] <= opt[i][j] <= opt[i][j + 1]
// O(n^2)

const inf = int(1e9)

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n, k := io.nextInt(), io.nextInt()
	u := make([][]int, n+1)
	u[0] = make([]int, n+1)
	for i := 1; i <= n; i++ {
		u[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			u[i][j] = io.nextInt()
			if i > j {
				u[i][j] = 0
			}
			u[i][j] += u[i-1][j] + u[i][j-1] - u[i-1][j-1]
		}
	}

	sum := func(l, r int) int { return u[r][r] - u[r][l-1] - u[l-1][r] + u[l-1][l-1] }

	dp := make([][]int, k+1)
	opt := make([][]int, k+1)
	for i := 0; i <= k; i++ {
		dp[i] = make([]int, n+1)
		opt[i] = make([]int, n+1)
	}

	for j := 1; j <= n; j++ {
		dp[1][j] = sum(1, j)
	}

	for i := 2; i <= k; i++ {
		lim := n - 1
		for j := n; j > 0; j-- {
			dp[i][j] = inf
			for x := opt[i-1][j]; x <= lim && x < j; x++ {
				if dp[i][j] > dp[i-1][x]+sum(x+1, j) {
					dp[i][j] = dp[i-1][x] + sum(x+1, j)
					opt[i][j] = x
				}
			}
			lim = opt[i][j]
		}
	}

	io.println(dp[k][n])
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReaderSize(r, 16<<10), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	return n
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

// 321E. Ciel and Gondolas
// Using opt[i - 1][j] <= opt[i][j] <= opt[i][j + 1]
// O(n^2)

const inf = int(1e9)

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n, k := io.nextInt(), io.nextInt()
	u := make([][]int, n+1)
	u[0] = make([]int, n+1)
	for i := 1; i <= n; i++ {
		u[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			u[i][j] = io.nextInt()
			if i > j {
				u[i][j] = 0
			}
			u[i][j] += u[i-1][j] + u[i][j-1] - u[i-1][j-1]
		}
	}

	sum := func(l, r int) int { return u[r][r] - u[r][l-1] - u[l-1][r] + u[l-1][l-1] }

	dp := make([][]int, k+1)
	opt := make([][]int, k+1)
	for i := 0; i <= k; i++ {
		dp[i] = make([]int, n+1)
		opt[i] = make([]int, n+1)
	}

	for j := 1; j <= n; j++ {
		dp[1][j] = sum(1, j)
	}

	for i := 2; i <= k; i++ {
		lim := n - 1
		for j := n; j > 0; j-- {
			dp[i][j] = inf
			for x := opt[i-1][j]; x <= lim && x < j; x++ {
				if dp[i][j] > dp[i-1][x]+sum(x+1, j) {
					dp[i][j] = dp[i-1][x] + sum(x+1, j)
					opt[i][j] = x
				}
			}
			lim = opt[i][j]
		}
	}

	io.println(dp[k][n])
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) fastIO {
	return fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	return n
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

// 321E. Ciel and Gondolas
// Using opt[i - 1][j] <= opt[i][j] <= opt[i][j + 1]
// O(n^2)

const inf = int(1e9)

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n, k := io.nextInt(), io.nextInt()
	u := make([][]int, n+1)
	u[0] = make([]int, n+1)
	for i := 1; i <= n; i++ {
		u[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			u[i][j] = io.nextInt()
			if i > j {
				u[i][j] = 0
			}
			u[i][j] += u[i-1][j] + u[i][j-1] - u[i-1][j-1]
		}
	}

	sum := func(l, r int) int { return u[r][r] - u[r][l-1] - u[l-1][r] + u[l-1][l-1] }

	dp := make([][]int, k+1)
	opt := make([][]int, k+1)
	for i := 0; i <= k; i++ {
		dp[i] = make([]int, n+1)
		opt[i] = make([]int, n+1)
	}

	for j := 1; j <= n; j++ {
		dp[1][j] = sum(1, j)
	}

	for i := 2; i <= k; i++ {
		lim := n - 1
		for j := n; j > 0; j-- {
			dp[i][j] = inf
			for x := opt[i-1][j]; x <= lim && x < j; x++ {
				if dp[i][j] > dp[i-1][x]+sum(x+1, j) {
					dp[i][j] = dp[i-1][x] + sum(x+1, j)
					opt[i][j] = x
				}
			}
			lim = opt[i][j]
		}
	}

	io.println(dp[k][n])
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	return &fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for !('0' <= f.p[0] && f.p[0] <= '9') {
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= f.p[0] && f.p[0] <= '9' {
		n = n*10 + int(f.p[0]) - '0'
		f.Read(f.p[:])
	}
	return n
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for f.p[0] <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for f.p[0] > ' ' {
		s = append(s, f.p[0])
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

// 321E. Ciel and Gondolas
// Using opt[i - 1][j] <= opt[i][j] <= opt[i][j + 1]
// O(n^2)

const inf = int(1e9)

func main() {
	io := newFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n, k := io.nextInt(), io.nextInt()
	u := make([][]int, n+1)
	u[0] = make([]int, n+1)
	for i := 1; i <= n; i++ {
		u[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			u[i][j] = io.nextInt()
			if i > j {
				u[i][j] = 0
			}
			u[i][j] += u[i-1][j] + u[i][j-1] - u[i-1][j-1]
		}
	}

	sum := func(l, r int) int { return u[r][r] - u[r][l-1] - u[l-1][r] + u[l-1][l-1] }

	dp := make([][]int, k+1)
	opt := make([][]int, k+1)
	for i := 0; i <= k; i++ {
		dp[i] = make([]int, n+1)
		opt[i] = make([]int, n+1)
	}

	for j := 1; j <= n; j++ {
		dp[1][j] = sum(1, j)
	}

	for i := 2; i <= k; i++ {
		lim := n - 1
		for j := n; j > 0; j-- {
			dp[i][j] = inf
			for x := opt[i-1][j]; x <= lim && x < j; x++ {
				if dp[i][j] > dp[i-1][x]+sum(x+1, j) {
					dp[i][j] = dp[i-1][x] + sum(x+1, j)
					opt[i][j] = x
				}
			}
			lim = opt[i][j]
		}
	}

	io.println(dp[k][n])
}

type fastIO struct {
	*bufio.Reader
	*bufio.Writer
	p [1]byte
	q *byte
}

func newFastIO(r io.Reader, w io.Writer) *fastIO {
	f := fastIO{Reader: bufio.NewReader(r), Writer: bufio.NewWriter(w)}
	f.q = &f.p[0]
	return &f
}

func (f *fastIO) nextInt() int {
	f.Read(f.p[:])
	for !('0' <= *f.q && *f.q <= '9') {
		f.Read(f.p[:])
	}
	n := 0
	for '0' <= *f.q && *f.q <= '9' {
		n = n*10 + int(*f.q) - '0'
		f.Read(f.p[:])
	}
	return n
}

func (f *fastIO) next() string {
	f.Read(f.p[:])
	for *f.q <= ' ' {
		f.Read(f.p[:])
	}
	var s []byte
	for *f.q > ' ' {
		s = append(s, *f.q)
		f.Read(f.p[:])
	}
	return string(s)
}

func (f *fastIO) println(a ...interface{}) {
	fmt.Fprintln(f.Writer, a...)
}

func (f *fastIO) print(a ...interface{}) {
	fmt.Fprint(f.Writer, a...)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// Testing I/O from https://codeforces.com/contest/1380/submission/87050098

const inf = int(1e9)

func main() {
	fin := MakeFastIn()

	n, k := fin.ReadInt(), fin.ReadInt()
	u := make([][]int, n+1)
	u[0] = make([]int, n+1)
	for i := 1; i <= n; i++ {
		u[i] = make([]int, n+1)
		for j := 1; j <= n; j++ {
			u[i][j] = fin.ReadInt()
			if i > j {
				u[i][j] = 0
			}
			u[i][j] += u[i-1][j] + u[i][j-1] - u[i-1][j-1]
		}
	}

	sum := func(l, r int) int { return u[r][r] - u[r][l-1] - u[l-1][r] + u[l-1][l-1] }

	dp := make([][]int, k+1)
	opt := make([][]int, k+1)
	for i := 0; i <= k; i++ {
		dp[i] = make([]int, n+1)
		opt[i] = make([]int, n+1)
	}

	for j := 1; j <= n; j++ {
		dp[1][j] = sum(1, j)
	}

	for i := 2; i <= k; i++ {
		lim := n - 1
		for j := n; j > 0; j-- {
			dp[i][j] = inf
			for x := opt[i-1][j]; x <= lim && x < j; x++ {
				if dp[i][j] > dp[i-1][x]+sum(x+1, j) {
					dp[i][j] = dp[i-1][x] + sum(x+1, j)
					opt[i][j] = x
				}
			}
			lim = opt[i][j]
		}
	}

	fmt.Println(dp[k][n])
}

type FastIn struct {
	scanner *bufio.Scanner
}

func MakeFastIn() (obj FastIn) {
	obj = FastIn{}
	obj.scanner = bufio.NewScanner(os.Stdin)
	obj.scanner.Split(bufio.ScanWords)
	return
}

func (fi *FastIn) ReadText() string {
	fi.scanner.Scan()
	return fi.scanner.Text()
}

func (fi *FastIn) ReadInt() int {
	ret, err := strconv.Atoi(fi.ReadText())
	if err != nil {
		panic(err)
	}
	return ret
}

package main

import (
	"fmt"
	"os"
	"strconv"
)

// M ...
const M = 4010

var (
	a, w [M][M]int
	f, g [M][M]int
)

func readnumbers() (nums []int) {
	fileInfo, _ := os.Stdin.Stat()
	p := make([]byte, fileInfo.Size())
	os.Stdin.Read(p)

	num := 0
	for i := 0; i < len(p); i++ {
		if p[i] >= '0' {
			num = 10*num + int(p[i]-'0')
		} else if p[i] != '\r' {
			nums = append(nums, num)
			num = 0
		}
	}

	if len(p) > 0 && p[len(p)-1] >= '0' {
		nums = append(nums, num)
	}

	return
}

func main() {
	fileInfo, _ := os.Stdin.Stat()
	p, idx := make([]byte, fileInfo.Size()), 0
	os.Stdin.Read(p)

	n, m := 0, 0
	for ; p[idx] >= '0'; idx++ {
		n *= 10
		n += int(p[idx] - '0')
	}
	idx++
	for ; p[idx] >= '0'; idx++ {
		m *= 10
		m += int(p[idx] - '0')
	}
	idx += 2

	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			a[i][j] = int(p[idx] - '0')
			fmt.Println(a[i][j])
			idx += 2
			a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1]
		}
		idx++
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			w[i][j] = a[j][j] - a[i-1][j] - a[j][i-1] + a[i-1][i-1]
		}
	}

	for j := 1; j <= n; j++ {
		f[1][j] = w[1][j]
		g[1][j] = 1
	}

	for i := 2; i <= m; i++ {
		g[i][n+1] = n
		for j := n; j >= i; j-- {
			f[i][j] = int(1e9)
			for k := g[i-1][j]; k <= g[i][j+1] && k <= j; k++ {
				if f[i-1][k-1]+w[k][j] < f[i][j] {
					f[i][j] = f[i-1][k-1] + w[k][j]
					g[i][j] = k
				}
			}
		}
	}

	os.Stdout.Write([]byte(strconv.Itoa(f[m][n] / 2)))
}

package main

import (
	"os"
	"strconv"
)

// M ...
const M = 4010

var (
	n, m int
	a, w [M][M]int
	f, g [M][M]int
)

func readnumbers() (nums []int) {
	fileInfo, _ := os.Stdin.Stat()
	p := make([]byte, fileInfo.Size())
	os.Stdin.Read(p)

	num := 0
	for i := 0; i < len(p); i++ {
		if p[i] >= '0' {
			num = 10*num + int(p[i]-'0')
		} else if p[i] != '\r' {
			nums = append(nums, num)
			num = 0
		}
	}

	if len(p) > 0 && p[len(p)-1] >= '0' {
		nums = append(nums, num)
	}

	return
}

func main() {
	fileInfo, _ := os.Stdin.Stat()
	p, idx := make([]byte, fileInfo.Size()), 0
	os.Stdin.Read(p)

	n, k := 0, 0
	for ; p[idx] >= '0'; idx++ {
		n *= 10
		n += int(p[idx] - '0')
	}
	for ; p[idx] >= '0'; idx++ {
		k *= 10
		k += int(p[idx] - '0')
	}
	idx += 2

	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			a[i][j] = int(p[idx])
			idx++
			a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1]
		}
		idx += 2
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= n; j++ {
			w[i][j] = a[j][j] - a[i-1][j] - a[j][i-1] + a[i-1][i-1]
		}
	}

	for j := 1; j <= n; j++ {
		f[1][j] = w[1][j]
		g[1][j] = 1
	}

	for i := 2; i <= m; i++ {
		g[i][n+1] = n
		for j := n; j >= i; j-- {
			f[i][j] = int(1e9)
			for k := g[i-1][j]; k <= g[i][j+1] && k <= j; k++ {
				if f[i-1][k-1]+w[k][j] < f[i][j] {
					f[i][j] = f[i-1][k-1] + w[k][j]
					g[i][j] = k
				}
			}
		}
	}

	os.Stdout.Write([]byte(strconv.Itoa(f[m][n] / 2)))
}


package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)

func ReadInt32() int {
	scanner.Scan()
	return toInt(scanner.Bytes())
}

func toInt(buf []byte) (n int) {
	for _, v := range buf {
		n = n*10 + int(v-'0')
	}
	return n
}

func main() {
	scanner.Split(bufio.ScanWords)
	var n int
	n = ReadInt32()

	minPrice := math.MaxInt32
	totalCost := 0
	for i := 0; i < n; i++ {
		var ai, pi int
		ai = ReadInt32()
		pi = ReadInt32()
		if minPrice > pi {
			minPrice = pi

		}
		totalCost = totalCost + ai*minPrice

	}
	fmt.Print(totalCost)

}

package main

import (
    "fmt"
    "bufio"
    "os"
)

func main() {
    reader := bufio.NewReader(os.Stdin);
    var n int
    minVal := 101
    ans := 0

    fmt.Fscanln(reader, &n)

    for i:=0; i<n; i++ {
        var a, b int
        fmt.Fscanln(reader, &a, &b)
        if(minVal > b) {
            minVal = b
        }
        ans += minVal * a
    }
    fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var count, prePrice, sumPrice int
	fmt.Fscanln(reader, &count)
	for i := 0; i < count; i++ {
		var a, p int
		fmt.Fscanln(reader, &a, &p)
		if prePrice == 0 {
			prePrice = p
		}
		if prePrice < p {
			sumPrice += a * prePrice
		} else {
			sumPrice += a * p
			prePrice = p
		}
	}
	fmt.Println(sumPrice)
}

package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func ReadInt32() int {
	scanner.Scan()
	ans, _ := strconv.Atoi(scanner.Text())
	return ans
}
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)
	var n,b int
	fmt.Scan(&n)
	c := 100
	for i:=0;i<n;i++ {
		a,p := ReadInt32(),ReadInt32()
		if p < c {
			c = p
		}
		b += c*a
	}

	fmt.Println(b)
}

package main

import (
	"bufio"
	"os"
	"strconv"
	"math"
	"fmt"
)

var in *bufio.Scanner = bufio.NewScanner(os.Stdin)
var out *bufio.Writer = bufio.NewWriter(os.Stdout)

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)

	sum, cost := 0, math.MaxInt32
	for i := nextInt(); i > 0; i-- {
		a := nextInt()
		cost = min(cost, nextInt())
		sum += a * cost
	}
	fmt.Println(sum)
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func nextInt() int {
	in.Scan()
	res, _ := strconv.Atoi(in.Text())
	return res
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	reader  = bufio.NewReader(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	a, p := 0, 0
	totalMoney, minPrice := 0, 1<<31-1

	for n := nextInt(); n > 0; n-- {
		a, p = nextInt(), nextInt()
		if p < minPrice {
			minPrice = p
		}

		totalMoney += minPrice * a
	}

	println(totalMoney)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	var n, minp, a, p int
	fmt.Scanf("%d \n", &n)
	result := 0
	minp = 100
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < n; i++ {
		line, _ := reader.ReadString('\n')
		list := strings.Split(strings.TrimRight(line, "\r\n"), " ")
		temp, _ := strconv.ParseInt(list[0], 10, 64)
		a = int(temp)
		temp, _ = strconv.ParseInt(list[1], 10, 64)
		p = int(temp)

		if p < minp {
			minp = p
		}
		result += a * minp
	}
	fmt.Print(result)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	var n, minp, a, p int
	fmt.Scanf("%d \n", &n)
	result := 0
	minp = 100

	scanner := bufio.NewScanner(os.Stdin)

	for i := 0; i < n; i++ {
		scanner.Scan()
		line := scanner.Text()
		list := strings.Split(line, " ")
		temp, _ := strconv.ParseInt(list[0], 10, 64)
		a = int(temp)
		temp, _ = strconv.ParseInt(list[1], 10, 64)
		p = int(temp)

		if p < minp {
			minp = p
		}
		result += a * minp
	}
	fmt.Print(result)
}

package main

import "bufio"
import "os"
import "fmt"

func Read(s *bufio.Reader) string {
    line, _ := s.ReadString('\n')
    return line
}

func Write(s *bufio.Writer, a ...interface{}) {
    fmt.Fprintln(s, a...)
    s.Flush()
}

func main() {
    reader := bufio.NewReader(os.Stdin)

    var n int
    fmt.Sscanln(Read(reader), &n)

    a := make([][2]int, n)

    for i := 0; i < n; i++ {
        line, _ := reader.ReadString(' ')
        fmt.Sscanln(line, &a[i][0])
        fmt.Sscanln(Read(reader), &a[i][1])
    }

    var result int = 0
    var min int = 200
    for i := range a {
        if a[i][1] < min {
            min = a[i][1]
        }

        result += min * a[i][0]
    }

    writer := bufio.NewWriter(os.Stdout)
    Write(writer, result)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

/**
3
1 3
2 2
3 1
*/

type DuffMeat struct {
	Eat   int
	Price int
}

var Meats []DuffMeat

func main() {
	var n int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanln(reader, &n)
	Meats = make([]DuffMeat, n)
	for i := 0; i < n; i++ {
		fmt.Fscanln(reader, &Meats[i].Eat, &Meats[i].Price)
	}
	//fmt.Println(Meats)

	retMoney := 0
	x := 0
	y := len(Meats)
	for {
		if y == x {
			break
		}
		var tempMoney int
		y, tempMoney = eatMin(x, y)
		retMoney += tempMoney
	}
	fmt.Println(retMoney)
}

// 最小的index, money
func eatMin(x, y int) (int, int) {
	//fmt.Println(x, y)
	minIndex := x
	for i := x; i < y; i++ {
		if Meats[i].Price < Meats[minIndex].Price {
			minIndex = i
		}
	}
	count := 0
	for i := minIndex; i < y; i++ {
		count += Meats[i].Eat
	}
	return minIndex, count * Meats[minIndex].Price
}

package main

import (
	"fmt"
	"math"
)

func main() {

	var n int
	fmt.Scanf("%d\n",&n)
	minPrice := math.MaxInt32
	totalCost := 0
	for i := 0; i < n; i++ {
		var ai, pi int
		fmt.Scan("%d %d\n",&ai, &pi)
		if minPrice > pi {
			minPrice = pi

		}
		totalCost = totalCost + ai*minPrice

	}
	fmt.Print(totalCost)

}

package main

import (
	"fmt"
	"math"
)

func main() {

	var n int
	fmt.Scan(&n)
	minPrice := math.MaxInt32
	totalCost := 0
	for i := 0; i < n; i++ {
		var ai, pi int
		fmt.Scan(&ai, &pi)
		fmt.Print(ai, pi, "\n")
		if minPrice > pi {
			minPrice = pi

		}
		totalCost = totalCost + ai*minPrice

	}
	fmt.Print(totalCost)

}

package main

import (
	"fmt"
)

func main() {
	var count, prePrice, sumA, sumPrice int
	fmt.Scanf("%d", &count)
	for i := 0; i < count; i++ {
		var a, p int
		fmt.Scanf("%d %d", &a, &p)
		sumA += a
		if prePrice == 0 {
			prePrice = p
		}
		if prePrice < p {
			sumPrice += a * prePrice
		} else {
			sumPrice += a * p
		}
		prePrice = p
	}
	fmt.Println(sumPrice)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var count, prePrice, sumA, sumPrice int
	fmt.Fscanln(reader, &count)
	for i := 0; i < count; i++ {
		var a, p int
		fmt.Fscanln(reader, &a, &p)
		sumA += a
		if prePrice == 0 {
			prePrice = p
		}
		if prePrice < p {
			sumPrice += a * prePrice
		} else {
			sumPrice += a * p
		}
		prePrice = p
	}
	fmt.Println(sumPrice)
}