King of Pie, Kim Pie
Time limit1sMemory limit512 MB
Choose one box length x in [L,R] to minimize x times the number of boxes needed to pack pies of given lengths into consecutive groups, where a length-0 pie must sit alone.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Binary search, Prefix sum, Greedy
- Solved
- No attempts yet
Problem
We like pie. Our favorite number is pi, our favorite browser is Firefox, our favorite Pokemon is Charmander, our favorite philosopher is Paul Karl Feyerabend, our favorite solitaire game is Spider, our favorite RPG is Final Fantasy, our favorite fighting games are far too many to list, our favorite card game is Slay the Spire, our favorite LoL support is Pyke, our favorite missile is the Spike, our favorite Harry Potter spell is Stupefy, our favorite demon is Paimon, our favorite comedy is Monty Python, and our favorite instrument is the pipe organ. Our favorite programming language, of course, is Delphi. Unfortunately Delphi is not available on this site, so we use our next favorite, Pike.
We want to pack N long rectangular apple pies into boxes. (These boxes are made of pine.) The width and height of an apple pie match the box, so when several apple pies go into a box, the sum of their lengths must be at most the length of the box. Each apple pie has a number, and the pies placed in one box must have consecutive numbers.
Some apple pies have length 1 but unusual ingredients, so they spoil if packed in the same box as another apple pie. Therefore, if such a pie is put in a box, no other apple pie may be in that box.
We are going to buy boxes that all have the same length. The length of a box we can buy is an integer between L and R inclusive, and one box of length x costs x won. We have no idea what length to choose to minimize the total cost.
Help poor Kim Woo-ri.
Input
First, N, L, and R are given. (1 ≤ N ≤ 10,000, 1 ≤ L ≤ R ≤ 10,000)
Then the information for the first few apple pies is given in order. For an unusual apple pie the value is 0; otherwise the length of the pie is given. Every apple pie not given in the input is unusual. (1 ≤ length ≤ L)
All numbers in the input are integers.
Output
Print the minimum cost of buying the boxes.