#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const int INF = INT_MAX / 2; int main() { int w, h, n, f; cin >> w >> h >> n >> f; vector > dp(h+1, vector(w+1, INF)); dp[0][0] = 0; for(int i=0; i> dx >> dy >> c; for(int y=h-dy; y>=0; --y){ for(int x=w-dx; x>=0; --x){ int y2 = y + dy; int x2 = x + dx; dp[y2][x2] = min(dp[y2][x2], dp[y][x] + c); } } } int ans = INF; for(int y=0; y<=h; ++y){ for(int x=0; x<=w; ++x){ int dist = abs(h - y) + abs(w - x); ans = min(ans, dp[y][x] + f * dist); } } cout << ans << endl; return 0; }