Necklaces
Time limit2sMemory limit1024 MB
Given n beads with values, find the k-th subset ordered by total value, with ties broken by the lexicographic order of bead numbers.
- Level
Hard8 of 10
- Topics
- Sorting, Binary search, Dynamic programming
- Solved
- No attempts yet
Statement
Bajtyna has beads numbered from 1 to . All beads are distinct. Each bead has a value in bajtalars.
Bajtyna wants to make a necklace from some of her beads. There are many ways to do this. Two ways are different if the sets of beads they use are different. Bajtyna wants to sort all the ways using these rules:
- A way with a smaller total value comes earlier.
- If two ways have the same total value, compare the sorted lists of bead numbers they use in lexicographic order.∗
For example, suppose there are four beads with values 3, 7, 4, and 3, in order. Then a necklace can be made in 16 ways. The table below shows these ways in Bajtyna's order.
Bajtyna wants the necklace that is in position in this order. Find it.
∗ Sequence is smaller than if either the first sequence is a prefix of the second one (so and ), or at the first position where they differ, (with ).
Input
The first line contains two positive integers and , separated by a single space. Here is the number of beads, and is the position of the wanted way in the order described above.
The second line contains positive integers , separated by single spaces. Here is the value of bead .
You may assume that Bajtyna did not make a mistake, so there are at least different ways to make her necklace.
Output
Print one integer on the first line: the total value of the beads in the necklace you found.
Print the bead numbers used in the necklace on the second line, in increasing order, separated by single spaces.