I've been stuck on this problem for a while (Lazy Cows USACO 2005 US Open Gold) . I was able to get the recurrence correct . But my naive approach () TLEs .
First I compressed the positions by sorting them based on their column and breaking ties using the row number
Then I pre-processed 3 arrays 1. costH1[i][j] stores minimum area