Pudding and Coins

Pudding is playing a game that has nnn levels. There are a total of n(n+1)/2n(n+1)/2n(n+1)/2 coins spread out among the levels. Specifically, iiith level has pip_ipi​ coins where p1,p2,⋯ ,pnp_1, p_2, \cdots, p_np1​,p2​,⋯,pn​ form a permutation of 1,2,⋯ ,n1, 2, \cdots, n1,2,⋯,n. Pudding wants to pick all the coins in each level. However, her game character can only carry BBB coins in a level where BBB is the capacity of her bag. In level 111, B=1B=1B=1 and after completing each level, she stores the collected coins in a bank. Thus, the bag is emptied after each level and the capacity BBB increases by 111. So if n=4n=4n=4 and the pip_ipi​ are 2,1,4,32, 1, 4, 32,1,4,3, she will be able to take 1+1+3+3=81+1+3+3=81+1+3+3=8 coins from the game.

This is a companion discussion topic for the original entry at https://toph.co/p/something-something-permutation