import sequtils template times*(n:int,body) = (for _ in 0.." .} proc scan(): int = while true: let k = getchar_unlocked() if k < '0': return result = 10 * result + k.ord - '0'.ord const INF = 1e10.int let gx = scan() let gy = scan() let n = scan() let f = scan() var dp : array[101,array[101,int]] for x in 0..gx: for y in 0..gy: dp[x][y] = f * (x + y) n.times: let cx = scan() let cy = scan() let cost = scan() for x in (gx-cx).countdown(0): for y in (gy-cy).countdown(0): dp[x+cx][y+cy] .min= dp[x][y] + cost echo dp[gx][gy]