CF2040C Ordered Permutations
Description
Let us consider all permutations of length $ n $ with the maximum possible value of $ S(p) $ . Output the $ k $ -th of them in lexicographical $ ^{\\text{‡}} $ order, or report that there are less than $ k $ of them.
$ ^{\\text{∗}} $ A permutation of length $ n $ is an array consisting of $ n $ distinct integers from $ 1 $ to $ n $ in arbitrary order. For example, $ \[2,3,1,5,4\] $ is a permutation, but $ \[1,2,2\] $ is not a permutation ( $ 2 $ appears twice in the array), and $ \[1,3,4\] $ is also not a permutation ( $ n=3 $ but there is $ 4 $ in the array).
$ ^{\\text{†}} $ For example:
- For the permutation $ \[1, 2, 3\] $ the value of $ S(p) $ is equal to $ \\min(1) + \\min(1, 2) + \\min(1, 2, 3) + \\min(2) + \\min(2, 3) + \\min(3) = $ $ 1 + 1 + 1 + 2 + 2 + 3 = 10 $
- For the permutation $ \[2, 4, 1, 3\] $ the value of $ S(p) $ is equal to $ \\min(2) + \\min(2, 4) + \\min(2, 4, 1) + \\min(2, 4, 1, 3) \\ + $ $ \\min(4) + \\min(4, 1) + \\min(4, 1, 3) \\ + $ $ \\min(1) + \\min(1, 3) \\ + $ $ \\min(3) = $ $ 2 + 2 + 1 + 1 + 4 + 1 + 1 + 1 + 1 + 3 = 17 $ .
$ ^{\\text{‡}} $ An array $ a $ is lexicographically smaller than an array $ b $ if and only if one of the following holds:
- $ a $ is a prefix of $ b $ , but $ a \\ne b $ ; or
- in the first position where $ a $ and $ b $ differ, the array $ a $ has a smaller element than the corresponding element in $ b$$$.