/* -*- coding: utf-8 -*- * * 2095.cc: No.2095 High Rise - yukicoder */ #include #include using namespace std; /* constant */ const int MAX_N = 1000; const int MAX_M = 1000; /* typedef */ typedef long long ll; /* global variables */ int as[MAX_N][MAX_M]; ll dp[MAX_N + 1][MAX_N]; /* subroutines */ inline void setmin(ll &a, ll b) { if (a > b) a = b; } /* main */ int main() { int n, m; scanf("%d%d", &n, &m); if (n == 1) { puts("0"); return 0; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf("%d", as[i] + j); ll mind = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { dp[i + 1][j] = dp[i][j] + as[i][j]; if (i > 0) setmin(dp[i + 1][j], mind + as[i - 1][j] + as[i][j]); } mind = *min_element(dp[i + 1], dp[i + 1] + m); } printf("%lld\n", mind); return 0; }