Maksim has $n$ objects and $m$ boxes, each box has size exactly $k$. Objects are numbered from $1$ to $n$ in order from left to right, the size of the $i$-th object is $a_i$.

Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $i$-th object fits in the current box (the remaining size of the box is greater than or equal to $a_i$), he puts it in the box, and the remaining size of the box decreases by $a_i$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.

Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.

Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).


-----Input-----

The first line of the input contains three integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the number of objects, the number of boxes and the size of each box.

The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the size of the $i$-th object.


-----Output-----

Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.


-----Examples-----
Input
5 2 6
5 2 1 4 2

Output
4

Input
5 1 4
4 2 3 4 1

Output
1

Input
5 3 3
1 2 3 1 1

Output
5



-----Note-----

In the first example Maksim can pack only $4$ objects. Firstly, he tries to pack all the $5$ objects. Distribution of objects will be $[5], [2, 1]$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $[2, 1], [4, 2]$. So the answer is $4$.

In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $[4]$), but he can pack the last object ($[1]$).

In the third example Maksim can pack all the objects he has. The distribution will be $[1, 2], [3], [1, 1]$.
[{"code": "n, m, k = [int(_) for _ in input().split()]\na = [int(_) for _ in input().split()]\n\nb = k\ncount = 0\nfor obj in a[::-1]:\n    if obj > k:\n        break\n    if obj > b:\n        if m > 1:\n            m -= 1\n            b = k - obj\n            count += 1\n        else:\n            break\n    else:\n        b -= obj\n        count += 1\n\nprint(count)\n\n", "passed": true, "time": 0.15, "memory": 14200.0, "status": "done"}, {"code": "n,m,k = [int(x) for x in input().split()]\na = [int(x) for x in input().split()]\na = a[::-1]\nboxes = 0\nans = 0\nloc = 0\nfor i in range(n):\n    loc += a[i]\n    if loc > k:\n        boxes += 1\n        loc = a[i]\n    if boxes == m:\n        break\n    ans += 1\n    # print(ans,boxes,loc)\nprint(ans)\n\n", "passed": true, "time": 0.15, "memory": 14344.0, "status": "done"}, {"code": "n,m,k = list(map(int, input().split()))\nlist_a = list(map(int, input().split()))\nlist_a.reverse()\ncount_b = 0\nsum_now = 0\nans = 0\ni = 0\nwhile i < n:\n\tif count_b >= m:\n\t\tans = i - 1\n\t\tbreak\n\tsum_now += list_a[i]\n\tif sum_now > k:\n\t\tsum_now = 0\n\t\tcount_b +=1\n\telse:\n\t\ti += 1\nelse:\n\tans = n-1\nprint(ans+1)\n", "passed": true, "time": 0.15, "memory": 14320.0, "status": "done"}, {"code": "n, m, k = map(int, input().split())\na = list(map(int, input().split()))\n\n# left = -1\n# right = n-1\n\n# while right - left > 1:\n# \tmid = (right+left)//2\n\t\n# \tcount = 0\n# \tcurr = 0\n# \tfor i in range(mid, n):\n# \t\tif curr + a[i] <= k:\n# \t\t\tcurr += a[i]\n# \t\telse:\n# \t\t\tcount += 1\n# \t\t\tcurr = a[i]\n# \tcount += 1\n\t\n# \tif count > m:\n# \t\tleft = mid\n# \telse:\n# \t\tright = mid\n\n# print(n-right)\n\ncount = 0\ncurr = 0\ni = n\nwhile count < m and i >= 0:\n\ti -= 1\n\tif curr + a[i] <= k:\n\t\tcurr += a[i]\n\telse:\n\t\tcount += 1\n\t\tcurr = a[i]\n\nprint(n-i-1)", "passed": true, "time": 0.14, "memory": 14276.0, "status": "done"}, {"code": "def fit(a, m, k, res):\n\tk1 = k\n\tfor ai in a[-res:]:\n\t\tif ai <= k1:\n\t\t\tk1 -= ai\n\t\telse:\n\t\t\tk1 = k-ai\n\t\t\tif m == 1:\n\t\t\t\treturn False\n\t\t\telse:\n\t\t\t\tm -= 1\n\treturn True\n\t# \u0421 \u043d\u0435\u043a. \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043c\u043e\u0451 \u0432\u043e\u0441\u043f\u043e\u043c\u0438\u043d\u0430\u043d\u0438\u0435 \u043e\u0431 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0435, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u043c \u0432 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c (\u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043e)\n\n\ndef bisect(n, m, k, a):\n\tleft, right = 0, n+1\n\twhile left + 1 < right:\n\t\tmiddle = (left + right) // 2\n\t\tif fit(a, m, k, middle):\n\t\t\tleft = middle\n\t\telse:\n\t\t\tright = middle\n\treturn left\n\n\ndef main():\n\tn, m, k = list(map(int, input().split()))\n\ta = tuple(map(int, input().split()))\n\tprint(bisect(n, m, k, a))\nmain()\n", "passed": true, "time": 0.31, "memory": 14092.0, "status": "done"}, {"code": "n,m,k = [int(s) for s in input().split()]\na = [int(s) for s in input().split()]\nans = 0\nboxi = 0\nrem_size = k\nfor i in range(n-1, -1, -1):\n    if a[i] > k:\n        break\n    if a[i] > rem_size:\n        if boxi >= m-1:\n            break\n        else:\n            boxi += 1\n            rem_size = k\n    rem_size -= a[i]\n    ans += 1\n\nprint(ans)", "passed": true, "time": 0.16, "memory": 14348.0, "status": "done"}, {"code": "from sys import stdin\nn,m,k=list(map(int,stdin.readline().strip().split()))\ns=tuple(map(int,stdin.readline().strip().split()))\nx=0\ny=0\nans=0\nfor i in range(n-1,-1,-1):\n    if x==m:\n        break\n    if y+s[i]>k:\n        if s[i]>k:\n            break\n        x+=1\n        if x==m:\n            break\n        ans+=1\n        y=s[i]\n    else:\n        y+=s[i]\n        ans+=1\n    \nprint(ans)\n", "passed": true, "time": 0.14, "memory": 14276.0, "status": "done"}, {"code": "n,m,k=[int(x)for x in input().split()]\nns=[int(x)for x in input().split()]\nns.reverse()\nrem=k\nnum=0\nans=[]\nfor i in range(n):\n    if rem>=ns[i]:\n        rem-=ns[i]\n        num+=1\n    else:\n        ans.append(num)\n        rem=k-ns[i]\n        num=1\nif num>0:\n    ans.append(num)\n\n# print(ans)\n\nif len(ans)<=m:\n    print(sum(ans))\nelse:\n    a=[sum(ans[:m])]\n    # for i in range(m,len(ans)):\n    #     a.append(a[-1]+ans[i]-ans[i-m])\n    print(max(a))", "passed": true, "time": 0.25, "memory": 14328.0, "status": "done"}, {"code": "def main():\n    nobj, nbox, boxsize = list(map(int, input().split()))\n    obj = list(map(int, input().split()))\n    box = boxsize\n    curr = 0\n    ans = 0\n    i = nobj - 1\n    while i >= 0:\n        o = obj[i]\n        #print('box: ' + str(box) + ' i = ' + str(i) + ', o = ' + str(o))\n        if o <= box:\n            box -= o\n            ans += 1\n        else:\n            curr += 1\n            box = boxsize\n            if curr >= nbox:\n                break\n            continue\n        i -= 1\n    print(ans)\n\ndef __starting_point():\n    main()\n\n__starting_point()", "passed": true, "time": 0.14, "memory": 14252.0, "status": "done"}, {"code": "# coding=utf-8\n\nn, m, k = map(int, input().split())\n\na = [int(i) for i in input().split()]\n\ncur = 0\nans = 0\nbox = k\ni = n - 1\n\nwhile i >= 0:\n    obj = a[i]\n    if obj <= box:\n        box -= obj\n        ans += 1\n    else:\n        cur += 1\n        box = k\n        if cur >= m:\n            break\n        continue\n    i -= 1\n\nprint(ans)", "passed": true, "time": 0.15, "memory": 14096.0, "status": "done"}, {"code": "# coding=utf-8\n\nn, m, k = map(int, input().split())\n\na = [int(i) for i in input().split()]\n\ncur = 0\nans = 0\nbox = k\ni = n - 1\n\nwhile i >= 0:\n    obj = a[i]\n    if obj <= box:\n        box -= obj\n        ans += 1\n    else:\n        cur += 1\n        box = k\n        if cur >= m:\n            break\n        continue\n    i -= 1\n\nprint(ans)", "passed": true, "time": 0.16, "memory": 14200.0, "status": "done"}, {"code": "n, m, k = list(map(int, input().split()))\na = list(map(int, input().split()))\n\na = a[::-1]\n\nres = 0\nweight = 0\n\nfor i in range(n):\n    weight += a[i]\n    if weight > k:\n        weight = a[i]\n        m -= 1\n    if m <= 0:\n        break\n    res += 1\n\nprint(res)\n", "passed": true, "time": 0.23, "memory": 14224.0, "status": "done"}, {"code": "import sys\n\ndef minp():\n\treturn sys.stdin.readline().strip()\n\nn,mm,k = map(int,minp().split())\na = list(map(int,minp().split()))\ndef tt(x):\n\tm = mm-1\n\tc = 0\n\tfor j in range(x,n):\n\t\ti = a[j]\n\t\tif c + i > k:\n\t\t\tif m == 0 or i > k:\n\t\t\t\treturn False\n\t\t\tc = i\n\t\t\tm -= 1\n\t\t\t#if i == k:\n\t\t\t#\tif m == 0:\n\t\t\t#\t\treturn False\n\t\t\t#\tm -= 1\n\t\t\t#\tc = 0\n\t\telse:\n\t\t\tc += i\n\treturn True\nl = 0\nr = n\nwhile l < r:\n\tc = (l+r)//2\n\tif tt(c):\n\t\tr = c\n\telse:\n\t\tl = c+1\nprint(n-r)", "passed": true, "time": 0.24, "memory": 14344.0, "status": "done"}, {"code": "import sys\n\ndef minp():\n\treturn sys.stdin.readline().strip()\n\nn,mm,k = list(map(int,minp().split()))\na = list(map(int,minp().split()))\ndef tt(x):\n\tm = mm-1\n\tc = 0\n\tres = 0\n\tfor i in reversed(a):\n\t\tif c + i > k:\n\t\t\tif m == 0 or i > k:\n\t\t\t\treturn res\n\t\t\tc = i\n\t\t\tm -= 1\n\t\t\t#if i == k:\n\t\t\t#\tif m == 0:\n\t\t\t#\t\treturn False\n\t\t\t#\tm -= 1\n\t\t\t#\tc = 0\n\t\telse:\n\t\t\tc += i\n\t\tres += 1\n\treturn res\nprint(tt(0))\n#l = 0\n#r = n\n#while l < r:\n#\tc = (l+r)//2\n#\tif tt(c):\n#\t\tr = c\n#\telse:\n#\t\tl = c+1\n#print(n-r)\n", "passed": true, "time": 0.24, "memory": 14304.0, "status": "done"}, {"code": "import sys\ninput = sys.stdin.readline\n\ndef main():\n    n, m, k = map(int, input().split())\n    a = list(map(int, input().split()))\n    if min(a) > k:\n        return 0\n    a = a[::-1]\n    current = 0\n    answer = 0\n    for i in range(n):\n        if current + a[i] <= k:\n            current += a[i]\n            answer += 1\n        else:\n            current = 0\n            m -= 1\n            if m == 0:\n                break\n            else:\n                if current + a[i] < k:\n                    current += a[i]\n                    answer += 1\n                elif current + a[i] == k:\n                    answer += 1\n                    m -= 1\n                    if m == 0:\n                        break\n                else:\n                    continue\n    return answer\ndef __starting_point():\n    print(main())\n__starting_point()", "passed": true, "time": 0.14, "memory": 14236.0, "status": "done"}, {"code": "n,m,k=list(map(int,input().split()))\narr=list(map(int,input().split()))\narr1=[0]*n\ni=n-1\nprevval=0\nans=0\nwhile(i>=0 and m>0):\n\tif(prevval+arr[i]<=k):\n\t\tprevval+=arr[i]\n\telse:\n\t\tm-=1\n\t\tprevval=arr[i]\n\ti-=1\n\tans+=1\nif(m==0):\n\tans-=1\nprint(ans)\n", "passed": true, "time": 0.15, "memory": 14308.0, "status": "done"}, {"code": "n,m,k=list(map(int,input().split()))#object,\u7bb1,size,\nA=list(map(int,input().split()))#size\n\n\nA.reverse()\ninb=0\nbox=0\ni=0\nwhile box<m and i<n:\n    if inb+A[i]<=k:\n       inb+=A[i]\n       i+=1\n    else:\n        box+=1\n        inb=0\n    \nprint(i)\n", "passed": true, "time": 0.14, "memory": 14136.0, "status": "done"}, {"code": "import sys\nimport math\n\nn,m,k=map(int,input().split())\njobs=[int(x) for x in input().strip().split()]\nsteve=jobs[:]\nnade=steve[::-1]\nsuma,con,kik=0,0,0\nfor i in range(n):\n    if(nade[i]>k):\n        break\n    if(suma+nade[i]<k):\n        #print(\"A\")\n        suma+=nade[i]\n        kik+=1\n        if(i<n-1):\n            if(suma+nade[i+1]>k):\n                suma=0\n                con+=1\n    elif(suma+nade[i]==k):\n        #print(\"B\")\n        #print(\"SPC CASE\",suma,nade[i],suma+nade[i])\n        con+=1\n        kik+=1\n        suma=0\n    elif(suma+nade[i]>k):\n        #print(\"C\")\n        con+=1\n        suma=0\n    #print(suma,kik)\n    if(con>=m):\n        break\nprint(kik)", "passed": true, "time": 0.14, "memory": 14396.0, "status": "done"}, {"code": "def bS(alist, item):\n    first = 0\n    last = len(alist)-1\n    found = False\n    posi=-1\t\n    while (first<=last+1) and (not found):\n        midpoint = (first + last)//2\n        #print(midpoint,alist[midpoint-1] )\n        if (alist[midpoint]>=item):#and (alist[midpoint+1] <= item):\n            found = True\n            posi=midpoint\n        else:\n            if item < alist[midpoint]:\n                last = midpoint-1\n            else:\n                first = midpoint+1\n\t\n    return posi\n\n\nn,m,k=list(map(int,input().split()))\na=list(input().split())\nmaxi=0\ncc=0\nnow=-1\nalr=0\nalfa=[0]*n\nlast=0\nfor ii in range(n):\n    i=n-ii-1\n    alfa[ii]=int(a[i])+last\n    last=alfa[ii]\n\n#print(alfa)\nif alfa[-1]>m*k:\n    pos=bS(alfa,m*k)\nelse:\n    pos=n-1\n\n#print(pos)\nii=n-pos-1\n#b=a[ii+1:].copy()\nb=a\nb.reverse()\nmaxi=0\nstart=0\n#for start in range(n):\nif (1==1):\n    #if n-start<maxi:\n    #    break\n    alr=0\n    cc=0\n    for i in b[start:]:\n        alr+=int(i)\n        cc+=1\n        \n        if alr>k:\n            alr=int(i)\n            if alr>k:\n                cc-=1\n            m-=1\n            if m==0:\n                cc-=1\n                break\n    if cc>maxi:\n        maxi=cc\n        \n    \nprint(maxi)   \n\n\n\n\n   \n\n    \n", "passed": true, "time": 0.14, "memory": 14188.0, "status": "done"}, {"code": "def go():\n    n, m, k = [int(i) for i in input().split(' ')]\n    a = [int(i) for i in input().split(' ')]\n    current_box = k\n    m -= 1\n    c = 0\n    for i in range(n - 1, -1, -1):\n        if a[i] <= current_box:\n            current_box -= a[i]\n            c += 1\n        elif a[i] > current_box:\n            current_box = k - a[i]\n            if m == 0:\n                break\n            m -= 1\n            c += 1\n    return c\n\nprint(go())\n", "passed": true, "time": 0.14, "memory": 14120.0, "status": "done"}, {"code": "n, m, k = list(map(int, input().split()))\na = list(map(int, input().split()))\na.reverse()\nlast = 0\nfor i in range(n):\n    if a[i] > last:\n        if m == 0:\n            print(i)\n            return\n        else:\n            m -= 1\n            last = k - a[i]\n    else:\n        last -= a[i]\nprint(n)\n", "passed": true, "time": 0.14, "memory": 14396.0, "status": "done"}, {"code": "n,m,k=list(map(int,input().split()))\ncnt=0\ncount=0\nsum=0\na=[int(s) for s in input().split()]\nfor i in range(n-1,-1,-1):\n    sum+=a[i]\n    if sum>k:\n        sum=a[i]\n        count+=1\n        if count>=m:\n            break\n    cnt+=1\nprint(cnt)\n", "passed": true, "time": 0.14, "memory": 14096.0, "status": "done"}, {"code": "\nn,k,m=list(map(int,input().split()))\nl=[int(i) for i in input().split()]\ndef ispzbl(l,n,m,k,mid):\n    z=l[mid:]\n    if not z:\n        return True \n    s=0\n    box=1\n    for i in z:\n        s+=i \n        if s>m:\n            box+=1 \n            s=i\n    return box<=k\nlow=0 \nhigh=n-1 \nwhile low<=high:\n    mid=(low+high)//2\n   # print(mid)\n    if ispzbl(l,n,m,k,mid):\n        ans=mid \n        high=mid-1 \n    else:\n        low=mid+1 \n#print(ans)\nprint(n-ans)\n", "passed": true, "time": 0.95, "memory": 14124.0, "status": "done"}]