---
# System prepended metadata

title: Untitled

---

## コード
```cpp=
#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(int i = 0;i < n;i++)
typedef vector<int> VI;
typedef vector<VI> VVI;
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout<<fixed<<setprecision(15);
 
    int r,c,h,m; cin >> r >> c >> h >> m;
    int time[r][c]; 
    REP(i,r) REP(j,c) cin >> time[i][j];
    REP(i,r) REP(j,c)
    if(time[i][j]==1) time[i][j] = h;
    else time[i][j] = m; 

    int dp[r][c];
    dp[r-1][0]=0;

    for(int i=r-1; i>=0; i--){
        for(int j=0; j<c; j++){
            if(i==r-1&&j==0) continue;
            if(i==r-1) dp[i][j] = dp[i][j-1] + time[i][j];
            else if(j==0) dp[i][j] = dp[i+1][j] + time[i][j];
            else dp[i][j] = min(dp[i][j-1]+time[i][j], dp[i+1][j]+time[i][j]);
        }
    }

    cout << dp[0][c-1] << endl;

    return 0;
}
```