Garden
Time limit1sMemory limit128 MB
Place two non-overlapping rectangles, each holding exactly k roses, and minimize the sum of their perimeters over an l by w grid with n roses.
- Level
Hard8 of 10
- Topics
- Array, Prefix sum, Brute force, Implementation
- Solved
- No attempts yet
Problem
Jaehyeon owns the most beautiful garden, and has planted roses in it. On a summer day when every flower is in full bloom, Jaehyeon looks at the beautiful roses and suddenly begins to worry about the national economy, so he hires two gardeners, Park Seungwon and Shin Seungwon, to lower unemployment and stimulate the economy.
The garden is a rectangle meters wide and meters tall, divided into unit square cells of side meter. The garden's sides are parallel to the - and -axes, and every cell inside the garden can be described by integer coordinates with and .
A rectangular region with the four corners , , , contains every cell with and (that is, and ), and its perimeter is .
Jaehyeon wants to build two non-overlapping rectangular fences in the garden so that each fenced region contains exactly the same number of roses, . He plans to assign the two regions to Park Seungwon and Shin Seungwon.
Unfortunately, the fences are imported, so they do not help the economy at all. Therefore Jaehyeon wants to minimize the total fence length (the sum of the two perimeters) to boost domestic demand.
The two regions must not share any cell, and each region must contain exactly roses. Where the two fences touch, the fence is built twice, so that shared length is counted twice in the total.
Given the size of the garden, the positions of the roses, and the number of roses each region must contain, write a program that finds two regions satisfying the conditions with the minimum total fence length. A single cell may contain several roses.
Input
The first line contains the garden's width and height and . ()
The second line contains the total number of roses and the number of roses each region must contain . (, )
Each of the next lines contains two integers and , the cell of the -th rose. (, ) A single cell may contain multiple roses.
Output
Print, on one line, the minimum possible sum of the two regions' perimeters. If no two regions can satisfy the conditions, print NO.
Hint
