#include #define rep(x, to) for (int x = 0; x < (to); x++) #define REP(x, a, to) for (int x = (a); x < (to); x++) #define EPS (1e-14) #define _PA(x,N) rep(i,N){cout< PII; typedef pair PLL; typedef complex Complex; typedef vector< vector > Mat; const int INF = 1e+9 + 7; int Gx, Gy; int N, F; int x[55], y[55], c[55]; int dp[55][105][105]; void solve() { for (int i = 0; i < 105; i++) { for (int j = 0; j < 105; j++) { dp[0][i][j] = F * i + F * j; } } for (int i = 0; i < N; i++) { for (int j = 0; j < 105; j++) { // y for (int k = 0; k < 105; k++) { // x dp[i+1][j][k] = dp[i][j][k]; if (j-y[i] >= 0 && k-x[i] >= 0) { dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j-y[i]][k-x[i]] + c[i]); } } } } cout << dp[N][Gy][Gx] << endl; #if 0 rep(i, 5) { rep(j, 5) { rep(k, 5) { cout << dp[i][j][k] << " "; } cout << endl; } cout << endl; } #endif } int main() { cin >> Gx >> Gy >> N >> F; rep(i, N) { cin >> x[i] >> y[i] >> c[i]; } solve(); return 0; }