#include #include #include #include #include using namespace std; struct POS { int x, y; bool operator<(const POS &right)const { return x + y > right.x + right.y; } }; int main() { int Gx, Gy, N, F;cin >> Gx >> Gy >> N >> F; vector>xyc(N); for (int i = 0;i < N;i++) { cin >> xyc[i].first.x >> xyc[i].first.y >> xyc[i].second; } sort(xyc.begin(), xyc.end()); mapM; POS p = { 0,0 }; M[p] = 0; for (int i = 0;i < N;i++) { for (auto itr = M.begin();itr != M.end();itr++) { POS pos = itr->first; pos.x += xyc[i].first.x;pos.y += xyc[i].first.y; if (pos.x > Gx || pos.y > Gy)continue; if (M.find(pos) == M.end()) { M[pos] = itr->second + xyc[i].second; } else M[pos] = min(M[pos], itr->second + xyc[i].second); } } int ans = 1e8; for (auto itr = M.begin();itr != M.end();itr++) { ans = min(ans, itr->second + F*abs(itr->first.x - Gx) + F*abs(itr->first.y - Gy)); } cout << ans << endl; return 0; }