[Problem Statement](http://s3-ap-northeast-1.amazonaws.com/data.cms.ioi-jp.org/open-2016/2016-open-skyscraper-en.pdf)↵
↵
Abridged Problem Statement : Given $a_1, a_2, ..., a_n$, find the number of permutations of these numbers such that $|a_1 - a_2| + |a_2 - a_3| + ... + |a_{n - 1} - a_{n}| \le L$ where $L$ is a given integer.↵
↵
The [editorial](http://s3-ap-northeast-1.amazonaws.com/data.cms.ioi-jp.org/open-2016/2016-open-skyscraper-sol-en.pdf) given is quite brief and the sample code is nearly unreadable. I have no idea how to do the dp.↵
↵
Can anyone explain the solution? Thanks.↵
↵
UPD : Thanks to [user:ffao,2016-06-25] for the hint! I finally got how the dp works. The unobfuscated code with comments is [here](http://ideone.com/bj9pm9).
↵
Abridged Problem Statement : Given $a_1, a_2, ..., a_n$, find the number of permutations of these numbers such that $|a_1 - a_2| + |a_2 - a_3| + ... + |a_{n - 1} - a_{n}| \le L$ where $L$ is a given integer.↵
↵
The [editorial](http://s3-ap-northeast-1.amazonaws.com/data.cms.ioi-jp.org/open-2016/2016-open-skyscraper-sol-en.pdf) given is quite brief and the sample code is nearly unreadable. I have no idea how to do the dp.↵
↵
Can anyone explain the solution? Thanks.↵
↵
UPD : Thanks to [user:ffao,2016-06-25] for the hint! I finally got how the dp works. The unobfuscated code with comments is [here](http://ideone.com/bj9pm9).