[Question]
  [
There is a boy named Peter. Peter is a picky eater and doesn't like to eat the same kind of cookie again on the same day.
Peter gets a present, The present is a box of cookies! The box contains \$K\$ types of cookies. Also, it contains different amount of each type of cookie.
Peter, being the picky eater he is, Wants to eat \$n\$ cookies, on each \$n^{th}\$ day. Also, he does not want to eat more than 1 cookie of the same kind on any given day.
i.e. He eats \$1\$ cookie on the \$1^{st}\$ day, \$2\$ cookies on the \$2^{nd}\$ day and so on.
If there is no possible way for Peter to eat \$n\$ cookies at day \$n\$, he gives up and dumps the box. He also dumps the box if it becomes empty.
Find the maximum number of days that the box will last for Peter.
## Input
\$K\$ Positive Integers, specifying the number of cookies for each different kind.
## Output
An Integer corresponding to how many days Peter keeps the box.
## Test Cases
```
INPUT -> OUTPUT
1 2 -> 2
1 2 3 -> 3
1 2 3 4 4 -> 4
1 2 3 3 -> 3
2 2 2 -> 3
1 1 1 1 -> 2
11 22 33 44 55 66 77 88 99 -> 9
10 20 30 40 50 60 70 80 -> 8
3 3 3 3 3 3 3 3 3 3 3 -> 7
3 3 3 3 3 3 3 3 3 -> 6
9 5 6 8 7 -> 5
1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10 -> 35
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 -> 32
```
[Gist for test cases](https://gist.githubusercontent.com/EliteDaMyth/19479d4d3bc1f285dcfb8dc4d4ebfa3f/raw/132ca67a3e239b0a0e26ea0341e591493ec7dc22/testcases.txt)
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so the shortest code wins.
  
[Answer]
# [Perl 5](https://www.perl.org/), ~~79~~ ~~77~~ ~~76~~ ~~75~~ ~~69 (rip)~~ ~~68~~ ~~64~~ 62 bytes
Credit to **@Lynn** for improving my answer by 4 bytes (68 to 64) and the explanation about the comma operator in scalar context here <https://docstore.mik.ua/orelly/perl2/prog/ch03_18.htm>
Thanks **@Nahuel Fouilleul** for the tip using anonymous functions to save 2 bytes (64 to 62) but if it's not allowed I'll revert back
```
sub{$^=0;++$^while(@_=sort{$b-$a}@_),!grep--$_[$_]<0,0..$^;$^}
```
**Test Cases Here:** [Try it online!](https://tio.run/##1ZPLToNQEIb3PMVIZgHp33rkThHlAXRpYmJaYvWoJE1LgEYjYenOR9CX80XwQOslUeJavgNnzvxzSWDIZbF0Wz6P23KzqHkei2g04vn9XbaURpLG5bqoal6M@bJJUhN7t4XMx2NOLzidHQqIyYTnEc@bNtqUkk6ysppOT9eFPKuyZak/ZrmnR5qWVLKsSorJuDiANQN1G@xPAw6cr0Pvt5S5i@zpTaWrABXuwHXhefB9BAHCsFcFLAFbwBFwBTwBXyAQnWTjF34VOmcIVRsB/G/tf2ANYA/gDOAO4A3gDxAMEA6g3tZf6/vncfvWwS7xHztnZqQl8iGXV5W87ibSAtkgp3@qpY4hKAD5IA/kKmd3WypNu1kXZHQzbWwHGvRZyTSp1khdBncSiLeKqVoknEa9xper7ifg8/GRkfRx5lbIi2xV0Yh0rlVIE3O9zW5Ixy4r/qhIx6S/vTzpNFX767MO3o@0pm3fAQ)
### Expanded:
```
sub {                       # Declare subroutine
    $^ = 0;                 # Set scalar caret variable to 0 (you can put this variable right next to if statements and for loops etc)
    ++$^ while              # Pre increment caret while the condition below is true
        (                   # Bracket used so that you can declare/reassign values in a conditional
            @_ = sort {     # Sort the default array and store result back to default array
                $b - $a     # Sort from greatest to least (scalar variables a and b are default variables that come with sort)
            }@_             # Add the default array as a parameter for the sort
        ),                  # End of bracket (in scalar context like in this solution it will only grab the right most argument of the array so the while loop is entirely dependent on the condition below)
        !grep               # Only save values if the below statement is true for each item in default array then reverse the boolean (will return false if the size of the array after grep is greater than 0)
            --$_[$_] < 0,   # Check whether the pre decrement of item (default is $_) at the index of default array is less than 0. If the caret is out of range of the array, it will default to undef which is treated as 0 in math operations)
        0 .. $^             # Add an array of range 0 to caret inclusive to the grep
    ;                       # End of the while loop statement modifier
    $^                      # Return caret variable
}                           # End of subroutine
```
[Answer]
# [Python 3](https://docs.python.org/3/), ~~96~~ ~~92~~ 91 bytes
```
f=lambda l,n=1:n<=len(l)and-~f([v-(e<n)for e,v in enumerate(sorted(l)[::-1])if(e<n)-v],n+1)
```
[Try it online!](https://tio.run/##1ZPLboMwEEX3fMXswOpF4v1S6I9QFpQYBZU4CJyobZT@OrWttI3UoK7LMTCPa0bCM@Ob3B1EuCxdOTT7521DA0TpF2JTDlw4A2vE1v3onOrkOnwjWHeYiONEvSAujns@NZI782GSfKvEVVG4fs36zmjdUw3x4LNF8lnOVJJT@QhqkH4h/DYQIfpxTDxQ5lVpMKbKK4GSR4hjJAnSFFmGPDdZD4GH0EPkIfaQeEg9ZJ5OhbjD3YQO5lDfRob0pvwvghXCFaIV4hWSFdIVshXyFdTf@mvdHk9sSmfXjf84WDOLv468Ve2q@zEAhaDIPNVSbg7KQCkoAcUqqO@AWe2Oty980nvsp2OQRq0NMpYf2szSUyHB9VS896Nj@h30VYkVFqlLT0DnSGacceqFdDr7LC/kPtJ5vhCdr1UqXpZzfbHZ8gk "Python 3 – Try It Online")
Simply "eats" \$1\$ each of the \$n\$ cookies with the largest amount each day, starting with \$n=1\$, until we don't have \$n\$ different cookies left. Returns the number of days this was successful.
### Explanation
```
f=lambda l,n=1:                   # function taking the list of  
                                  # cookies l and day n  
  n<=len(l)                       # if don't we have enough cookies for  
                                  # today return False (0)  
           and                    # if we do have enough cookies  
              -~                  # return 1 plus 
                f([...],n+1)      # our function called reclusively for 
                                  # day n+1 with updated list:
         sorted(l)[::-1]          # of cookies sorted from most to least  
       for e,v in enumerate(..)   # over the position and number of the 
                                  # cookies  
v-(e<n)                           # subtract 1 from the n largest 
                                  # amount of cookies  
                        if(e<n)-v # discarding empty amounts  
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~82~~ ~~77~~ 76 bytes
-5 bytes inspired by [x1Mike7x](https://codegolf.stackexchange.com/users/45323/x1mike7x)'s [answer](https://codegolf.stackexchange.com/a/225978/64121). (Adding a `0` to the cookie list at every iteration)  
-1 byte thanks to [dingledooper](https://codegolf.stackexchange.com/users/88546/dingledooper)!
```
l=input()
i=0
while l.sort()<l>[0]:i-=1;l[i:]=[x-1for x in[1]+l[i:]]
print~i
```
[Try it online!](https://tio.run/##1ZFPT8QgEMXvfIq5dY2zG0pLodV6W69evDUctGKW2LQNsrF78atX6K5/Epd4Nj8CL@8NMwmMB7cbeja3w5OGGpIkmbva9OPerS6IqSl525lOQ7d5Hay3rrubhqrKrOv0qmtMpepmWqfPg4UJTN@k6nJxFRmt6d27mX1Dcupxb/e6IgDOHsIBoCfdQhhMgm716GB7d7u1drDHgkerH17mJkWmSNgx@zwxx/xLB5d5daxaCMqHPvWlOXKORYFCoJRYliGkyChmFHOKnGJBUVCU1CcZnuGc770SfV@UKL4H/4JFyCLkEXiEIoKIICOUEfxL/bV@fApfJsvTvX9sqg8 "Python 2 – Try It Online")
[Answer]
# JavaScript (ES6), 70 bytes
```
f=(a,c=d=0)=>c?d-1:f(a.sort((a,b)=>b-a).map(x=>c*x?x-!!c--:x,c=++d),c)
```
[Try it online!](https://tio.run/##1VFLbpxAEN1zimfLUnfHA@rhz1jMrJJlNl7Gkcw0jYOFYQI4IrLmBpGy8dK@R87jC/gIk@oZNJEio6yjh@iqeu9VFfRt9i3rVFtuertucr3bFSnPZirNUynSpVrl9nxR8MzpmrbnxKypurYz4dxlGz6Q4t2wGuyTE2Xbi4F85@e5mCmxa/XX@7LVnBUdE06rs/xDWenL77XiUjh9c9m3ZX3DhdNtqrLnp1f1qXCKpn2fqS@8Q7rEgwV8QlV2/Qx62GjV6xyfkaIbPQz2Ekxc7DVUN8eRYocNP97frXUrLqhXq6ktCm5k@4Jq6q6ptFM1N/z67IH4LfYH0vTPxBXY66@fL0@P9GZYgL08/2Dba@qwFbs5XLOEa5nAM6E3hj6BUn9MR86Fe3AY2QEHO6lIRjYfQYAwRBQhjpEkhk@suYQr4Un4EoFEKBFJxNKQseXhLRAVWW@WQysBzUCMyKTBcZW/4U7Am4A/gWAC4QSiCcQTSCZAf@2fj7mL4HhnwX56PHr/56L5Lvc3 "JavaScript (Node.js) – Try It Online")
### Commented
```
f = (                  // f is a recursive function taking:
  a,                   //   a[] = list of cookies
  c =                  //   c   = cookies that were not eaten the day before
  d = 0                //   d   = day counter
) =>                   //
  c ?                  // if we failed to eat all the cookies the day before:
    d - 1              //   stop and return d - 1
  :                    // else:
    f(                 //   do a recursive call:
      a.sort((a, b) => //     sort the list of cookies ...
        b - a          //       ... from highest to lowest
      )                //
      .map(x =>        //     for each value x in the sorted list:
        c * x ?        //       if both c and x are not equal to 0:
          x - !!c--    //         decrement x and c
        :              //       else:
          x,           //         leave x unchanged
        c = ++d        //       start by incrementing d and setting c to d
      ),               //     end of map()
      c                //     pass c
    )                  //   end of recursive call
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~15~~ 14 bytes
```
‘ɼ-€+NÞƲAƑ¿ṛ®’
```
[Try it online!](https://tio.run/##y0rNyan8//9Rw4yTe3QfNa3R9js879gmx2MTD@1/uHP2oXWPGmb@//8/2lDHSMdYx0THVMdMx1zHQsdSx9BAZygLxgIA "Jelly – Try It Online")
[All testcases](https://tio.run/##1ZExCsIwGIV3r@ITftOkSUYv4AVKRxfpBdyKi@DmpgieQBxcVCgILRR6jPYiMQ1FCzY4yxfIy/9e8pNkuUiSlTFNuq@ySbM@j@fFqbzOyl3@rB/H/NKkB1Pfb1Rlxabc2oAx0SiagsVwE4K3AAf/LFydWdklHU5a3wZsnEMIhCGkhFLQ2rkERggInCAIIUESFLVWgAEGjbaoYc@Gguy1/4J5CDxwD8JD6EF6UB60B/tav0b/e4RrrbqNf1y0l4pf "Jelly – Try It Online")
## Explanation
```
‘ɼ-€+NÞƲAƑ¿ṛ®’  Main monadic link
          ¿     While
         Ƒ        invariant under
        A           absolute value
                  (that is, no numbers are negative)
                do
       Ʋ        (
 ɼ                Apply this to the register:
‘                   Increment
   €              Map [over the range 1..register]:
  -                 -1
    +             Add
                    the list
      Þ             sorted by
     N              negation
       Ʋ        )
           ṛ    Replace with
            ®   the register
             ’  Decrement
```
[Answer]
# [Python 3.8](https://docs.python.org/3.8/), ~~88~~ ~~82~~ ~~81~~ 80 bytes
```
f=lambda a,n=-1:-1in(a:=sorted([0]+a))and-n-2or f([x-1for x in a[n:]]+a[:n],n-1)
```
[Try it online!](https://tio.run/##1VLbaoQwFHz3K6YPi0mrJcZbFOyPiJSUVSrsRtEUtl9vE3W3S2nocx0f5pyZyYkm46d@H1QsxmlZuuokz29HCRmoKozKMOoVkWU1D5Nuj6RmzZOkVKpjqEI@TOhIfQmjzrALegVZq7IxlrpUTaDCiC5W0u2srTqMrSKMlh7MY5uvvQo2MnxoVCt9nsdTr4mP8AU@/bZKNRuHmfd4liPplQ6uS@wJSpvNLue5nfRdqjLDNbnOoQGI35HDTPFQ4XD0ccAqBrcIpUsEbnfAPUtiS@OdJgamTPZy1zj4lrC2DVvcuIzNxBKkKbIMeQ4hUBRWL7yIgTPEDAlDypAx5AyCWVF4MX6DkXLv13bmFTAzIJDbMr1t5Se4A7EDiQOpA5kDuQPCgcIB89f@fO1ZpLczS9fpYs/@56b9Lu6tdy26u3Zf "Python 3.8 (pre-release) – Try It Online")
Unwrapped version:
```
def f(a, n=-1):
    a = sorted([0] + a)
    if -1 in a:
        return -n - 2
    p = [x - 1 for x in a[n:]]
    q = a[:n]
    return f(p + q, n - 1)
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 14 bytes
```
v{āNÌ‹R-Wds}\O
```
[Try it online!](https://tio.run/##yy9OTMpM/f@/rPpIo9/hnkcNO4N0w1OKa2P8//@PNtQx0jHWMdExiQUA "05AB1E – Try It Online") or [Try all cases!](https://tio.run/##S0oszvifnFiiYKeQnJ@SqpdfnJiUmapgY6Pg6u/2v6z6SKPf4Z5HDTuDdMNTimtj/P8Dhbm4yjMyc1IVilITUxQy87hS8rkUFPTzC0r0IbqhFJqBNgoqYLV5qf@jDXWMYrlApI4xjNYx0TGBs0GiRkAWRBUYglhASaAsUKmJjqmpjpmZjrm5joWFjqUlSNJAx8hAx9hAx8RAx9RAx8xAx9xAx8IAKGOsgwViEweKWeoAzdWx0DFHWIwBjXBAYxzQBAc0xQHNcEBzHNACB7TEAYEhRQghRYop2GYLqL4hLBjLBQA)
**Commented:**
```
v          }     # for N in range(len(input))
 {               # sort the current cookie list (initially the input)
  ā              # push the range [1..len(list)]
   NÌ            # push N+2
     ‹           # is each value in the range less than N+2?
                 # this yields a list with N+1 leading ones
      R          # reverse the list of 1's and 0's
       -         # subtract this from the current cookie list
        Wds      # push min(list) >= 0 under the current list
            \    # after the loop: discard the final cookie list
             O   # sum the stack: this counts the number of times
                 #                the cookie counts were non-negative
```
[Answer]
# [K (ngn/k)](https://codeberg.org/ngn/k), 32 bytes
```
{#(|/'0>)_x{x[y#>x]-:1;x}\1+!#x}
```
[Try it online!](https://ngn.bitbucket.io/k#eJzVkc8OgjAMxu97is9wEEKMZX/YgIQXQeKNJ/Awg7y7ddOIkcWz+TVZ169dt3Vq5yy/HffUF2c/++Ga9X48tFXnl1NV7jK/CHFp82moIEeBsEK9PWjo1S4qkv1XdiD6nMI5XKJhDOoa1sI5NE2UCZKgCJpgCDXBEhwFTWGDbSVEG/D5cLDrS3whE2x2i0/dxCSoE9gELkGTgH/sl30MyoTe7ln5x8GxEKLML11UHqORHOdxc46BYpPFHZYKhNk=)
* `x{...}\1+!#x` set up a scan, seeded with `x` (the number of cookies of each type), run over `1..(number of types of cookies + 1)` (variable `y`)
	+ `y#>x` identify the `n` cookies to eat (i.e., the `y` indices containing the largest values)
	+ `{x[...]-:1;x}` eat those cookies, feeding the result into the next iteration of the scan
* `(|/'0>)_...` remove scan iteration results where there was any negative value
* `#...` return the count of the filtered results
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 19 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
ⁿ┘ª1µiZ&Xû¢♣◙2╜8æ≥/
```
[Run and debug it](https://staxlang.xyz/#p=fcd9a631e6695a2658969b050a32bd3891f22f&i=%5B1+2%5D%0A%0A%5B1+2+3%5D%0A%0A%5B1+2+3+4+4%5D%0A%0A%5B1+2+3+3%5D%0A%0A%5B2+2+2%5D%0A%0A%5B1+1+1+1%5D%0A%0A%5B11+22+33+44+55+66+77+88+99%5D%0A%0A%5B10+20+30+40+50+60+70+80%5D%0A%0A%5B3+3+3+3+3+3+3+3+3+3+3%5D%0A%0A%5B3+3+3+3+3+3+3+3+3%5D%0A%0A%5B9+5+6+8+7%5D%0A%0A%5B1+1+1+1+1+1+1+1+1+1+1+1+2+2+2+2+2+2+2+2+2+2+2+2+3+3+3+3+3+3+3+3+3+3+3+3+4+4+4+4+4+4+4+4+4+4+4+4+5+5+5+5+5+5+5+5+5+5+5+5+6+6+6+6+6+6+6+6+6+6+6+6+7+7+7+7+7+7+7+7+7+7+7+7+8+8+8+8+8+8+8+8+8+8+8+8+9+9+9+9+9+9+9+9+9+9+9+9+10+10+10+10+10+10+10+10+10+10+10+10%5D%0A%0A%5B1+2+3+4+5+6+7+8+9+10+1+2+3+4+5+6+7+8+9+10+1+2+3+4+5+6+7+8+9+10+1+2+3+4+5+6+7+8+9+10+1+2+3+4+5+6+7+8+9+10+1+2+3+4+5+6+7+8+9+10+1+2+3+4+5+6+7+8+9+10+1+2+3+4+5+6+7+8+9+10+1+2+3+4+5+6+7+8+9+10+1+2+3+4+5+6+7+8+9+10%5D%0A&m=1)
A direct implementation. Takes in golfscript arrays, but normal ones are supported as well.
## Explanation
```
{{No{i|i^<-mcc:@i^>wL%v
{                  w    do-while top of stack is true:
 {No                    sort descending
    {      m            map each element to:
     i|i^<              iteration index <= outer loop iteration index?
          -             subtract that form the element
            cc          copy twice
              :@        count of truthy elements
                i^<     < index+1?
                    L   wrap all arrays in stack
                     %v length decremented
```
[Answer]
# [J](http://jsoftware.com/), 38 bytes
```
_1+1#.0*/"1@:<:[(-~\:~)/\.@|.@,(#$1:)\
```
[Try it online!](https://tio.run/##1ZHLCsIwEEX3fsVFhbY@4qRNmjQqFARXrtxacCE@cOMPiL9eo9iq1cG1nAlk5t5hwuRYtkWww9QhwAAE589QYLZczMu17MuOoN6oLXM3catweCncJRoVIj@LfBB2utJFRRm19lM33rW2m8MJe0jEL1ckbwkU1Huh1uMbT@1OnXqvN/t2Ba2RpjAG1iLLagchJiQERdCElGAIlio5@QYrVkIGPwsWpvGsD2KGhEExaIaUwTBYhozBb@9XNL9R38fbR/MfF8sr "J – Try It Online")
For `1 2 2`:
```
(#$1:)\
1 0 0
1 1 0
1 1 1
[ … |.@,
1 1 1
1 1 0
1 0 0
1 2 2
   f   /\.    for each suffix, fold from bottom to top with f
(  /:~)       sort the remaining cookies (highest first),
 -~           subtract the needed cookies for the day:
0 0 _1
1 0  1
1 2  1
1 2  2
0*/"1@:<: does the row only contain numbers >= 0?
0 1 1 1
   1#.    count the 1s
_1+       minus one (the initial cookie row)
-> 2
```
[Answer]
# [Pip](https://github.com/dloscutoff/pip), 20 bytes
```
TvNgg:RSNg+:vRL UiDi
```
Takes inputs as command-line arguments. [Try it here!](https://replit.com/@dloscutoff/pip)
The equivalent program in Pip Classic is **22 bytes**:
```
TvNgg:RVSNg+:vRL++i--i
```
Verify all test cases: [Try it online!](https://tio.run/##1dG7CsIwGIbh/b@KF9dSSZukSTu4uohKFd2ETqVbRXARr73G41KDszw58eUESd/1w56mOnIR9nNhRcOUCemMibDmMmzPy7at6t1m2SbVuV4kSZem3cCVHc3hJNchI5dQ0c8Wg3mNtOShv88@SBbikIclBmspCpzDe8pSMkWu0AqjsIpC4RReieaLcSol4Tw87n3ZSB6hI0yEjSgiXISPKCPCC/0qnw@wj3v9a9cfhzc "Pip – Try It Online")
### Explanation
```
TvNgg:RSNg+:vRL UiDi
                      g is list of cmdline args; v is -1; i is 0 (implicit)
T                     Loop until
 vNg                  there is a -1 in the cookie counts list:
      R                The reverse of
       SNg             the cookie counts list, sorted ascending
                       (in other words: the cookie counts list, sorted descending)
                Ui     Increment i and
            vRL        create a list of that many -1's
          +:           Add those two lists itemwise
    g:                 Assign the result back to g
                  Di  Decrement i and autoprint it
```
---
Here are two different approaches, also 20 bytes each:
```
Wi<#R:SN:FI:gDg@<Uii   Filter 0's out of the list at each step instead of checking for -1
L#gI(g:RSNgi)Dg@<Uii   Loop len(input) times; increment i only if there are enough cookies
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 62 bytes
```
f=(c,d=0)=>c.sort((x,y)=>y-x)[d]?f(c,++d,c.map(x=>--c[--d])):d
```
[Try it online!](https://tio.run/##1ZFLTuNAEIb3PsUvhORuEVuN3wY5rJjlbFiGSHjabcYjY2faBiVCuQESG5ZwD87DBThCpjqxgoSwWI8@y11Vf73s/pPf5Z3U1aJ3mrZQm02ZMTkpMsGzqXS7VveMLScr8lbOks@K@VlJ@tFRMZHuTb5gy2zqOHLmOMWc85Nio9Xf20orZpedzV2t8uJHVauLVSOZ4G7fXvS6aq4Zd7tFXfXs4LI54G7Z6vNc/mYdsinuLWCGuur6CdRyoWSvCsyRoRtqbDhT2Px0m0Nxc@wlGmrW@nl780tpfkq9tKK2KJlJ2wZk23Rtrdy6vWZXh/ekr7E9kGUfE89gv78@vj0/0dvGCey3lwd7fUUd1nxzDM8s4VnG8I3pD2ZAkBsM7qB58HYVJm3HrpyyKI3KAoQhoghxjCRBmho9tY4FPAFfIBAIBSKBWCARRkwsH19BUmx9GY6sFDQDCWLjhvtVPuON4I8QjBCOEI0Qj5CMkI5Af@3bx9xFuL@zcDs9GWr/56D5Lu8f "JavaScript (Node.js) – Try It Online")
The TIO setup is based on Arnauld's answer.
Seems greedy eating cookies with most copies works, though I don't know how to prove it (but that doesn't matter).
```
f = (
        c, // cookies (array of integers)
        d = 0 // day (integer)
    ) =>
    c.sort((x, y) => y - x) // sort cookies (desc order)
[d] ? // Is there enough kinds of cookie for today?
f(c, ++d, // Next iteration (day + 1)
    c.map(x => --c[--d]) // Eat 1 per each
    // greedy eat kinds with most copies
): d
```
[Answer]
# [Haskell](https://www.haskell.org/), 71 bytes
```
(0?)
n?a=maximum$n:[(n+1)?b|b<-mapM(\x->x:[x-1|x>0])a,sum b+n+1==sum a]
```
[Try it online!](https://tio.run/##LY1NbsIwEIX3OcUskLBF3BLzU0rj5AKtegBgMSYJtYgdKw5qFpy9rg3R23xv5r2ZH3TXum19I46eLEuamBKFxlHpm56Z/YGYRUZLeZc502i/yHFkxbg/jCy7j8XyRDF1Nw1yEWJCRMST16iMqLoEiEo7mrMmNEnVdxY4fXEWDXkVczan@ay41MOnMnUCbT0Air7GKkx/u75yoD7kYwDhku2VGUgTIkKmmEqaQPziM@DACuBJhFXE1YTroGDXk512HPizEWNPPervsIEt7OAt2s3fuWnx4jw7W@vZN/8H "Haskell – Try It Online")
The relevant function is `(0?)`, which takes the list `a` of cookie quantities and returns the number of days.
Due to Haskell requiring `import Data.List` for sorting, I think bruteforce is the way to go.
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), ~~31~~ 29 bytes
```
.+
$*
m{O^`^1*
1_
_
}1`1$
_
_
```
[Try it online!](https://tio.run/##1dE7DsIwDAbg3ecoEiq/kPNOTsHA3paBgQEGxIZ69mBKYSkWM/qSOHIelpLr8Xa6HOpqvR9QtxtqWjrfd93QmZZMTz2NZjCNxL5WA0vS4V4jPPw8c2QlPlcnZCQtedniEQJiRErIGaWQYViGY3hGYERGYmQmhy@WWSqQ@5CR3sUWrMIpvCIooiIpsqIo5IV@tc8HhKlunk/9cfIB "Retina 0.8.2 – Try It Online") Takes a newline-separated list, but link is to test suite that converts from comma-separated list for convenience. Explanation: Performs a slightly different calculation with the same result: on day `m` he eats up to `m` cookies, then when all the cookies have run out the desired answer is the most cookies he ate in any given day.
```
.+
$*
```
Convert to unary.
```
m{`
}`
```
Repeat until all the cookies have been eaten, and also evaluate the script in multiline mode.
```
O^`^1*
```
Sort the cookies in descending order.
```
1_
_
```
Try to eat the same number of cookies that were eaten the previous day.
```
1`1$
1_
```
Try to eat a new different cookie each day.
```
_
```
Count the maximum number of cookies eaten on any given day.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 42 bytes
```
≔⁰ζＷ›Ｌθζ«≦⊕ζ≔⟦⟧ηＷ⌈⁻θηＦ№θκ⊞ηκ≔ΦＥη⁻κ‹λζκθ»Ｉζ
```
[Try it online!](https://tio.run/##bY49C8IwEIbn9ldkvEIEwbGTFBShhe6lQ6hnE5qmNh8qir89JrbgIrfc3cP73HWc6W5i0vu9MaJXsKXkmeXpnQuJBI4amUUNJarecpizSDPySpOKXdfESXUaR1QWz0s2WUHTUsLjvMoq9hCjG6ESyhmYIwyuy6QJFJNTNq6GsKmd4cBj/3MdhIx/hKuRLIaBkhKNAfl9KosBSuYQeqe1FsFXMGMhoNz7ptnRP9W2fnOTHw "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
≔⁰ζ
```
Start with `n=0`.
```
Ｗ›Ｌθζ«
```
While there are enough different cookies:
```
≦⊕ζ
```
Increment `n`.
```
≔⟦⟧ηＷ⌈⁻θηＦ№θκ⊞ηκ
```
Sort the cookies into descending order.
```
≔ΦＥη⁻κ‹λζκθ
```
Decrement the first `n` cookies, removing entries that become zero.
```
»Ｉζ
```
Print the final value of `n`.
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~97 91 88 84 69~~ 63 bytes
```
f=(n,d=0)=>n.sort((a,b)=>b-a)[d]?f(n.map((e,i)=>e-=d>=i),d+1):d
```
[Try it online!](https://tio.run/##1VPBjpswEL3zFXOqbQVYB0Ig2TpRtcqhPWQP2UuVjbRObBYqFlLstFpV/fbUDk6kZhf1XD0EnnnzZsZ4/I3/4GrXlnsd1I2Qx2POcO0LRgmb1aFqWo0x97fG2gacrMVmnuM6fOF7jKVfGrcMmJixkvhiMCRTcfRubkAExUc6l1P7lfN1MZCB8AsmNmu6meJiwCT5QI2DerumVhq0VPqOK6mAwdMQIghmEHl2Edtl7JYjA2OOnOm4CKJOYcM6dHITZcKMbARJAuMxpClkGUwmlp94QwoRhZjCiEJCYUwhpZBRS2ZeDO/BUKn3rnvsTcDUgAxSayaXVq4R9SDuwagHSQ/GPUh7kPVg0gPz1/752LNILmeWnKpnTvs/O@2@oqdbN7RqX5VaS2Fm9jK/4cmJ0WONyK13jgjzpl3wXYFlBWwGv1yCqjQvBrI6q2wBq@vost4fLG/DzL25BCFyun6SzZaHl61ssSQXTSvVobIiR520w43jm0qGVfOM0cNi9XD3abWYomvms4IcI//L6n4ZKt2W9XOZv@JTK8RHBOT3A69AN8jvSvlobnLAX0nWaHmPfEBfFyu0WQ9w7vTAGHMdkrctFRL4TtvsbhM/uTJZzuKreGP@Jsc/ "JavaScript (Node.js) – Try It Online")
## How it works
### Arguments
* `n` [init: none] - the array of cookie types
* `d` [init: 0] - the day number
### Function body
First, we check if there are enough truthy entries to "pick" cookies. If not we simply return the day count.
If there are enough, then from each element of the sorted list (descending) we subtract 1 if the index is low enough and the element is strictly positive. If not, leave the element as it is. Call `f` with the obtained array and an incremented day number.
I also realized that since `0` is falsey, we do not need to filter. PLEASE don't make the 69 joke, it's getting boring.
*-2 bytes thanks to @A username*
*-6 bytes thanks to @tsh*
[Answer]
# [R](https://www.r-project.org/), 86 bytes
```
function(b){while({b=-sort(-b);b=c(b[1:T]-1,b[-1:-T],0);T<=sum(b|1)&b[T]+1})T=T+1
T-1}
```
[Try it online!](https://tio.run/##1ZJBasMwEEX3PoWhUCQyAkmWLDmpewrtjDGV49LQNg62Q1vSnN01ocqmmTjb8rQS/8@fkaYb109ffbVrhqarXptm11e@/czH5/22Hjbtlnh6@HjZvDXk4HPWt91AmKcrn9fEF2LpSibAF0wsmSuB05V7yPv9O/Hfgt77wpULcaQudwsROSaOF8NITQRISuO7mD3GMsI1kARVclUFClRQquvK@Ypy0skbck/MTzHFTrlTiwq0hjQFY8BayLJgzVArB8kh4aA4aA4pB8PB8uCzmC@BCwSXudkVHCnmyGCaByyYoNQzj/UHiZAgKASNkCIYBIuQIUw/NHfOi6TnNlifGrO/Zf/x5XlkGY0/ "R – Try It Online")
```
days_peter_keeps_box=
function(b){
  while({                       # Loop for each day peter has the box:
    b=-sort(-b)                 # sort the cookies with most-abundant types first,
    b=c(b[1:T]-1,b[-1:-T],0)    # take out 1 cookie from each of the first T types (initially 1),
    T<=length(b)                # now check: if T is greater than the number of cookie types
      &b[T]>-1                  # or any of the types have less than zero cookies left
                                # then we've gone one day too many... stop the loop;
  })T=T+1                       # otherwise increment T.
T-1}                            # When the loop is finished, output T-1.  
```
[Answer]
# [Core Maude](http://maude.cs.illinois.edu/w/index.php/The_Maude_System), 257 bytes
```
fmod P is pr SORTABLE-LIST{Nat<}*(sort List{Nat<}to L). var L : L . var X
N :[L]. op b : L -> Nat . op _,_ : L Nat -> Nat . eq b(L)= L,0 . eq L,N =
d(sort(L),s N),s N . eq X,s N = N[owise]. op d : L Nat ~> L . eq d(L,0)=
L . eq d(L s N,s X)= d(L,X)N . endfm
```
### Example Session
```
             \||||||||||||||||||/
           --- Welcome to Maude ---
             /||||||||||||||||||\
         Maude 3.1 built: Oct 12 2020 20:12:31
         Copyright 1997-2020 SRI International
           Tue May 18 23:37:06 2021
Maude> fmod P is pr SORTABLE-LIST{Nat<}*(sort List{Nat<}to L). var L : L . var X
> N :[L]. op b : L -> Nat . op _,_ : L Nat -> Nat . eq b(L)= L,0 . eq L,N =
> d(sort(L),s N),s N . eq X,s N = N[owise]. op d : L Nat ~> L . eq d(L,0)=
> L . eq d(L s N,s X)= d(L,X)N . endfm
Maude> red b(1 2) .
reduce in P : b(1 2) .
rewrites: 37 in 0ms cpu (0ms real) (~ rewrites/second)
result NzNat: 2
Maude> red b(1 2 3) .
reduce in P : b(1 2 3) .
rewrites: 99 in 0ms cpu (0ms real) (~ rewrites/second)
result NzNat: 3
Maude> red b(2 2 2) .
reduce in P : b(2 2 2) .
rewrites: 96 in 0ms cpu (0ms real) (~ rewrites/second)
result NzNat: 3
Maude> red b(11 22 33 44 55 66 77 88 99) .
reduce in P : b(11 22 33 44 55 66 77 88 99) .
rewrites: 1045 in 0ms cpu (0ms real) (~ rewrites/second)
result NzNat: 9
Maude> red b(1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10) .
reduce in P : b(1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 6 6
    6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10) .
rewrites: 82081 in 27ms cpu (27ms real) (2952129 rewrites/second)
result NzNat: 35
```
### Ungolfed
```
fmod P is
    pr SORTABLE-LIST{Nat<} * (sort List{Nat<} to L) .
    var L : L .
    var X N : [L] .
    op b : L -> Nat .
    op _,_ : L Nat -> Nat .
    eq b(L) = L, 0 .
    eq L, N = d(sort(L), s N), s N .
    eq X, s N = N [owise] .
    op d : L Nat ~> L .
    eq d(L, 0) = L .
    eq d(L s N, s X) = d(L, X) N .
endfm
```
This program accepts input via a function `b` which takes a list of natural numbers. The function `d` will "eat" cookies for each step, and it the program runs until the list term is no longer a well-formed list of natural numbers, then returns the index of the previous step.
[Answer]
# [Haskell](https://www.haskell.org/), 114 bytes
```
import Data.List;p i k|i<length(filter(>0)k)=p(i+1).zipWith(-)(sort k)$replicate(length k-i-1)0++repeat 1|0<1=i---
```
[Try it online!](https://tio.run/##vVJNb5tAEL3zK0aWpbBi1lm@QQk59ZieemikaBURd12vwBgta1uu/N/dMU6U2C7NJQoPBPtm3psHy7zsKlXX@71etEtj4Vtpy8m97uxNCxqqnb6tVfPbzt2Zrq0y7p1gFStaV3s@m/zR7U9NNc7c7iCu2NiottbT0ir3qIOKa@4z4XlUUaUFfydu/UJzzvecA6zXa7Bz1Sl43lrVwUYZBdPlqrHqF5QNXbQmqaGWnnvewhWJr5wZFNCCcByHdBZqMLCDGd2Lgh4PxZX9Yc19A2MYTWAEngfdfLmhDnoaAb97xxkHXo8dLCmQ2WjKdGry8IGJS9PZkb8uTsydRakbcluU7fcncFfNdGXMFg7BGYz72Y/gPvoYSAxYv8bjGkOJ4RmDEUYSo3P2tDMgLjjT9jidQFLSkmWEcYxJgmmKWYZ5LjF/1yYwEBgKjATGAhOBqcBMSMzeekL8BySm/@mQmLxVc6T5mGEqMb4IfYFgAOEAogHEA0iO6GMkZ0gHkA0gHwB91Y9O2sD4cvfjPkT2YvFlZB/j033pFV9@SLn/Cw "Haskell – Try It Online")
---
Uses an algorithm of order \$\mathcal O(m\cdot n\ln n)\$ where \$m\$ is the maximum number of cookies of one kind and \$n\$ is the input list's size: On day \$d\$, sort all remaining cookies by their quantity and remove the \$d\$ cookies of whose kinds there are the most, if possible. Else, this day is the one sought after. If on the other hand the removal was successful, go on to day \$d+1\$.
]