#include using namespace std; #define rep(i,j,n) for(int i=j;i pi; template using vt = vector; template using vvt = vector>; i64 gcd(i64 n, i64 m) {return (m == 0? n : gcd(m, n % m));} i64 lcd(i64 n, i64 m) {return (n / gcd(n, m) * m);} int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { cin.tie(0); ios::sync_with_stdio(false); int gx, gy, n, f; cin >> gx >> gy >> n >> f; vt x(n), y(n), c(n); rep(i, 0, n) cin >> x[i] >> y[i] >> c[i]; vvt dp(110, vt(110, INF)); dp[0][0] = 0; rep(i, 0, gx + 1) { rep(j, 0, gy + 1) { dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + f); dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + f); } } rep(k, 0, n) { auto vec = dp; rep(i, 0, gx + 1) { rep(j, 0, gy + 1) { vec[i][j] = dp[i][j]; if(i >= x[k] && j >= y[k]) vec[i][j] = min(vec[i][j], dp[i - x[k]][j - y[k]] + c[k]); } } swap(vec, dp); } cout << dp[gx][gy] << endl; }