#define TEST_MODE true #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i, a, b) for (int i = (a); i < (b); ++i) #define rrep(i, n) for (int i = (n)-1; i >= 0; --i) #define rrep2(i, a, b) for (int i = (a)-1; i >= b; --i) #define range(i, a, b, c) for (int i = a; \ c > 0 ? i < b : i > b; \ i += c) #define chmax(a, b) (a = (a) < (b) ? (b) : (a)) #define chmin(a, b) (a = (a) > (b) ? (b) : (a)) using ll = long long; using ull = unsigned long long; using ld = long double; #define all(a) begin(a), end(a) #define ifnot(a) if (not(a)) #define int long long #ifdef LOCAL_ENV #if TEST_MODE == true const bool test = true; #define dump(x) cerr << #x << " = " << (x) << endl #else const bool test = false; #define dump(x) #endif #else const bool test = false; #define dump(x) #endif int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; const int INF = (int)1 << 60; const ll INFL = (ll)1 << 60; ll mod_n = (int)1e9 + 7; const double eps = 1e-10; typedef long double Real; // return -1, 0, 1 int sgn(const Real &r) { return (r > eps) - (r < -eps); } int sgn(const Real &a, const Real &b) { return sgn(a - b); } //..................... const int MAX = (int)2e5 + 5; vector split(const string &str, char sep) { vector v; stringstream ss(str); string buffer; while (getline(ss, buffer, sep)) { v.push_back(buffer); } return v; } template int sum(InputIterator begin, InputIterator end) { return accumulate(begin, end, 0ll); } void solve(); signed main() { cout << fixed << setprecision(20); solve(); while (true) { char s[MAX]; if (scanf("%s", s) == EOF) break; int n = strlen(s); for (int i = n - 1; i > -1; i--) { ungetc(s[i], stdin); } solve(); } return 0; } class Mycin { bool flag = true; public: Mycin& operator >> (int& a) {flag = scanf("%lld", &a) != EOF; return *this;} Mycin& operator >> (char& a) {flag = scanf("%c", &a) != EOF; return *this;} Mycin& operator >> (string& s) {flag = (bool)(cin >> s); return *this;} operator bool() {return flag;} } mycin; class Mycout { public: Mycout& operator << (const int& a) {printf("%lld", a); return *this;} Mycout& operator << (const char c) {printf("%c", c); return *this;} Mycout& operator << (const string& s) {printf("%s", s.c_str()); return *this;} } mycout; #define cin mycin #define cout mycout #define endl '\n' int Gx, Gy, N, F; int x[55]; int y[55]; int c[55]; int dp[105][105]; void solve() { cin >> Gx >> Gy >> N >> F; rep(i, N) cin >> x[i] >> y[i] >> c[i]; rep(y, 105) rep(x, 105) dp[y][x] = INF; dp[0][0] = 0; rep(i, N) { rrep(cur_y, 105 - y[i]) rrep(cur_x, 105 - x[i]) { int next_y = cur_y + y[i]; int next_x = cur_x + x[i]; dp[next_y][next_x] = min(dp[next_y][next_x], dp[cur_y][cur_x] + c[i]); } } int res = INF; rep(y, Gy + 1) rep(x, Gx + 1) { res = min(res, dp[y][x] + (Gy - y + Gx - x) * F); } cout << res << endl; }