#include #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) FOR(i,0,n) #define repr(i,n) for(int i=(n)-1;0<=i;--i) #define each(e,v) for(auto&& e:(v)) #define all(v) begin(v),end(v) #define DUMP(x) cerr<<#x<<": "<<(x)<; using vdouble = vector; using vstring = vector; using ll = long long; template void chmin(T& a, const T& b) { a = min(a, b); } template void chmax(T& a, const T& b) { a = max(a, b); } const int dx[] = {0, 1}; const int dy[] = {1, 0}; struct Crystal { int x, y, c; Crystal() = default; Crystal(int x, int y, int c): x(x), y(y), c(c) {} }; bool operator > (const Crystal& a, const Crystal& b) { return ((a.x + a.y) / a.c) > ((b.x + b.y) / b.c); } int main() { int gx, gy, n, f; cin >> gx >> gy >> n >> f; vector crs; rep(i, n) { int x, y, c; cin >> x >> y >> c; if((x + y) * f <= c) continue; crs.push_back(Crystal(x, y, c)); } int x = 0, y = 0, cost = 0; sort(all(crs), greater()); const int cn = crs.size(); rep(i, cn) { if(x + crs[i].x <= gx && y + crs[i].y <= gy) { x += crs[i].x; y += crs[i].y; cost += crs[i].c; } } cost += (gx - x + gy - y) * f; cout << cost << endl; return 0; }