Apple Market
Time limit2sMemory limit512 MB
Given a grid of apple inventories and rectangle requests with budgets, sell apples to maximize total money where each apple costs 1.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Prefix sum
- Solved
- No attempts yet
Problem
You manage a market made up of several stores. The stores are arranged in an grid, and every store sells apples. At every store, one apple costs exactly 1 Malaysian Ringgit.
Several customers walk through this market. Each customer visits only the stores inside one subrectangle of the market and has a fixed amount of money to spend. Each store has a limited inventory of apples, which differs from store to store and is not replenished between customers. If you can decide how many apples each store sells to each customer, what is the most money you can make?
Input
The input consists of a single test case. Your program may be run several times on different inputs. The first line contains three space-separated integers , , and : the market has rows and columns (), and there are customers ().
Each of the next lines contains integers (). This matrix, in row-major order, gives the number of apples in the inventory of each store. is the number of apples in the store in row , column . Rows are numbered to and columns to . The top left corner is and the bottom right corner is .
Each of the next lines describes one customer with five integers , (), , (), and (). The customer shops only in the subrectangle from to inclusive ( is top, is bottom, is left, is right). The customer has exactly Malaysian Ringgits to spend.
Output
Print a single integer: the maximum amount of money you can make by deciding how many apples each store sells to each customer.