Note we need to count permutations such that min(1,p1)+min(2,p2)++min(n,pn)=s\min(1, p_1)+\min(2, p_2) + \cdots + \min(n, p_n)=s

We formulate a dp solution. Assume we are iterating over positions 1,2,,n1, 2, \cdots, n and while iterating, we can keep positions empty or fill them with some non-larger value and we can put current value in already iterated positions or leave them unassigned for now. Thus we can define dp[n][o][s]dp[n][o][s] to mean number of prefixes 1,2,,n1, 2, \cdots, n with oo positions unfilled (and thus oo numbers unassigned) and ss confirmed sum. Then when at position ii, five things can happen

  1. We can put ii at position ii

  2. We can put ii at an earlier unfilled position and leave position iiunfilled

  3. We can put ii at an earlier unfilled position and put some earlier unassigned value at position ii

  4. We can leave ii unassigned and position ii unfilled

  5. We can leave ii unassigned and put an earlier unassigned value at position iiB

By considering this cases we can come up with transitions. Note contribution to sum are confirmed in case 1 or when a position is left unfilled for future or when a value is left unassigned for future.

Statistics

73% Solution Ratio
NirjhorEarliest, Apr '22
serotoninFastest, 0.0s
adnan_tokyLightest, 8.4 MB
serotoninShortest, 659B
Toph uses cookies. By continuing you agree to our Cookie Policy.