#include #include using namespace std; using namespace atcoder; #define rep(i,n) for(int i=0;i<(n);i++) typedef long long ll; //using mint = modint; using mint = modint998244353; using P = pair; const ll INF=1LL<<60, dx[]={0,1,0,-1},dy[]={1,0,-1,0}; bool chmin(ll& a,ll b){if(a>b){a=b; return 1;} return 0;} bool chmax(ll& a,ll b){if(a> n >> m; vector> a(n,vector (m)); rep(i,n)rep(j,m) cin >> a[i][j]; vector> dp(n+1,vector (m)); if(n==1) { cout << 0 << endl; return 0; } ll lowcost = INF; rep(j,m){ dp[2][j] = a[0][j]+a[1][j]; chmin(lowcost,dp[2][j]); } for(int i=3;i<=n;i++){ ll tmp=INF; rep(j,m){ dp[i][j] = min(lowcost+a[i-2][j],dp[i-1][j]) +a[i-1][j]; chmin(tmp,dp[i][j]); } lowcost = tmp; } ll ans = INF; rep(i,m) chmin(ans,dp[n][i]); cout << ans << endl; return 0; }