#include <bits/stdc++.h> using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; typedef unsigned long long ull; #define INF 10000000 struct ST { int x, y, c; }; int main(){ int Gx, Gy, N, F; cin >> Gx >> Gy >> N >> F; vector<ST> v; rep(i,N){ int x, y, c; cin >> x >> y >> c; v.push_back((ST){x,y,c}); } vector<vector<int>> cost(Gy+1, vector<int>(Gx+1,INF)); cost[0][0] = 0; rep(i,N){ vector<vector<int>> new_cost = cost; rep(y,cost.size()){ rep(x,cost[y].size()){ if(cost[y][x] != INF){ new_cost[y][x] = min(new_cost[y][x], cost[y][x]); if(x+v[i].x<cost[y].size() && y+v[i].y<cost.size()){ new_cost[y+v[i].y][x+v[i].x] = min(new_cost[y+v[i].y][x+v[i].x], cost[y][x] + v[i].c); } } } } rep(y,cost.size()){ rep(x,cost[y].size()){ if(y+1<cost.size()){ new_cost[y+1][x] = min(new_cost[y+1][x], new_cost[y][x]+F); } if(x+1<cost[y].size()){ new_cost[y][x+1] = min(new_cost[y][x+1], new_cost[y][x]+F); } } } cost = new_cost; } cout << cost[Gy][Gx] << endl; return 0; }