package main import "fmt" func min(a, b int) int { if a < b { return a } return b } func main() { var n, m int fmt.Scanln(&n, &m) a := make([]int, m) dp := make([]int, m) for j := 0; j < m; j++ { fmt.Scan(&a[j]) } if n == 1 { fmt.Println(0) return } b := make([]int, m) for j := 0; j < m; j++ { fmt.Scan(&b[j]) 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++ { fmt.Scan(&nextA[j]) 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) }