package main import ( "bufio" "fmt" "math" "os" "strconv" ) const MaxInt = math.MaxInt64 var scanner *bufio.Scanner func init() { scanner = bufio.NewScanner(os.Stdin) scanner.Buffer([]byte{}, MaxInt) scanner.Split(bufio.ScanWords) } func Int() int { var w string if scanner.Scan() { w = scanner.Text() } ret, _ := strconv.ParseInt(w, 10, 0) return int(ret) } func Min(a, b int) int { if a < b { return a } return b } func main() { n := Int() m := Int() a := make([]int, m) dp := make([]int, m) for j := 0; j < m; j++ { a[j] = Int() } if n == 1 { fmt.Println(0) return } b := make([]int, m) for j := 0; j < m; j++ { b[j] = Int() dp[j] = a[j] + b[j] } a = b for i := 2; i < n; i++ { k := 0 minVal := dp[0] for j := 1; j < m; j++ { if dp[j] < minVal { k = j minVal = dp[j] } } nextA := make([]int, m) nextDP := make([]int, m) for j := 0; j < m; j++ { nextA[j] = Int() nextDP[j] = nextA[j] + Min(dp[j], dp[k]+a[j]) } a = nextA dp = nextDP } ans := dp[0] for j := 1; j < m; j++ { if dp[j] < ans { ans = dp[j] } } fmt.Println(ans) }