Apple Tree
InterviewTime limit1sMemory limit512 MB
Given an N by N grid of profits, find the K by K square subgrid with the largest sum.
- Level
Medium4 of 10
- Topics
- Prefix sum, Array, Brute force, Implementation
- Solved
- No attempts yet
Problem
There is a square orchard of size , and apple trees are planted in every cell on a grid.
Farmer Hyeonggon wants to harvest apples in the fall, but the landowner Sinyoung informed him: "You may harvest and take away the apple trees only in a square of size within the orchard. Here is an integer with ! I will eat the rest! Haha!"
Harvesting a single apple tree brings in profit from the apples and loss from labor cost at the same time.
So Hyeonggon recorded the total profit, the profit from the apples plus the labor cost, as a two-dimensional array indexed by the tree's coordinates.
You want to give poor cute Hyeonggon the maximum total profit, away from the cruel landowner Sinyoung. Help Hyeonggon!
Input
The first line gives the size of the orchard . ()
From the second line to the -th line, the total profit obtained by harvesting that tree is given.
The total profit is an integer with value .
Output
Print the maximum value on the first line.