Little Shop of Flowers
InterviewTime limit1sMemory limit128 MB
Place F ordered bunches into a row of V vases to maximize total aesthetic value, then output the lexicographically smallest optimal assignment.
- Level
Medium6 of 10
- Topics
- Dynamic programming
- Solved
- No attempts yet
Problem
You want to arrange the window of your flower shop as pleasantly as possible. You have bunches of flowers, each of a different kind, and a row of at least vases. The vases are fixed to the shelf and numbered through from left to right, so vase is the leftmost and vase is the rightmost. The bunches are movable and are identified by the integers through .
These id-numbers fix the required left-to-right order: whenever , bunch must stand in a vase to the left of the vase holding bunch . For example, with a bunch of azaleas (id ), begonias (id ), and carnations (id ), the azaleas must be left of the begonias, and the begonias must be left of the carnations. If there are more vases than bunches, the extra vases are left empty. Each vase holds at most one bunch.
Every vase has its own character, so placing a particular bunch in a particular vase yields an aesthetic value, given as an integer. Let be the aesthetic value of putting bunch into vase . Leaving a vase empty contributes .
For example, the aesthetic values might be:
Here azaleas look great in vase 2 but awful in vase 4.
Place every bunch so that the required order is respected and the total aesthetic value is as large as possible.
Input
- The first line contains two integers and .
- Each of the next lines contains integers; the -th integer on the -th line is , the aesthetic value of placing bunch into vase .
Output
- On the first line, print the maximum possible total aesthetic value.
- On the second line, print integers: the -th integer is the vase that holds bunch in an arrangement achieving that maximum. If several arrangements reach the maximum, print the lexicographically smallest one — comparing the sequences of vase numbers for bunches , the sequence that is smaller at the first position where they differ.
Constraints
- , where is the number of bunches (numbered through ).
- , where is the number of vases.
- , where is the aesthetic value of putting bunch into vase .