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]$.

Difficulty: MEDIUM_HARD
Tags: Sorting

Implementation
Skill types: Sorting

Starter code:

Solutions:
from sys import stdin, stdout
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
from collections import deque
from math import floor, sqrt, ceil
from collections import Counter as C, defaultdict as dd
(n, m, k) = mp()
l = mp()
i = n - 1
ans = 0
p = k
while i >= 0 and m > 0:
	if l[i] <= p:
		ans += 1
		p -= l[i]
		i -= 1
	else:
		p = k
		m -= 1
print(ans)


import sys

class Input(object):

	def __init__(self):
		self.fh = sys.stdin

	def next_line(self):
		while True:
			line = sys.stdin.readline()
			if line == '\n':
				continue
			return line

	def next_line_ints(self):
		line = self.next_line()
		return [int(x) for x in line.split()]

	def next_line_strs(self):
		line = self.next_line()
		return line.split()

def get_max_objects(s, m, k):
	low = 0
	high = len(s)
	while low < high:
		mid = (low + high) // 2
		if can_fit(s, mid, m, k):
			high = mid
		else:
			low = mid + 1
	return len(s) - low

def can_fit(s, start, m, k):
	left = 0
	for i in range(start, len(s)):
		if left < s[i]:
			if m == 0:
				return False
			m -= 1
			left = k
		left -= s[i]
	return True

def main():
	input = Input()
	while True:
		nums = input.next_line_ints()
		if not nums:
			break
		(n, m, k) = nums
		s = input.next_line_ints()
		max_num = get_max_objects(s, m, k)
		print('{}'.format(max_num))
main()


import sys
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7

def bisearch_min(mn, mx, func):
	ok = mx
	ng = mn
	while ng + 1 < ok:
		mid = (ok + ng) // 2
		if func(mid):
			ok = mid
		else:
			ng = mid
	return ok
(N, M, K) = MAP()
A = LIST()

def check(m):
	box = [0] * M
	i = m
	j = 0
	while i < N:
		if box[j] + A[i] <= K:
			box[j] += A[i]
			i += 1
		else:
			j += 1
			if j == M:
				return False
	return True
res = bisearch_min(-1, N, check)
ans = N - res
print(ans)


from sys import stdin
(n, m, k) = map(int, stdin.readline().strip().split())
s = tuple(map(int, stdin.readline().strip().split()))
x = 0
y = 0
ans = 0
for i in range(n - 1, -1, -1):
	if x == m:
		break
	if y + s[i] > k:
		if s[i] > k:
			break
		x += 1
		if x == m:
			break
		ans += 1
		y = s[i]
	else:
		y += s[i]
		ans += 1
print(ans)


import sys

def test(pos):
	current_pack = k
	count = 1
	for i in range(pos, n):
		if a[i] > current_pack:
			count += 1
			current_pack = k - a[i]
		else:
			current_pack -= a[i]
	if count > m:
		return False
	else:
		return True

def bs(l, r):
	l1 = l
	r1 = r
	res = 0
	while l1 <= r1:
		mid = (l1 + r1) // 2
		if test(mid) == True:
			res = mid
			r1 = mid - 1
		else:
			l1 = mid + 1
	return res
(n, m, k) = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
print(n - bs(0, n - 1))


def main():
	(nobj, nbox, boxsize) = list(map(int, input().split()))
	obj = list(map(int, input().split()))
	box = boxsize
	curr = 0
	ans = 0
	i = nobj - 1
	while i >= 0:
		o = obj[i]
		if o <= box:
			box -= o
			ans += 1
		else:
			curr += 1
			box = boxsize
			if curr >= nbox:
				break
			continue
		i -= 1
	print(ans)

def __starting_point():
	main()
__starting_point()


import os
import sys
from io import BytesIO, IOBase

def check(a, m, k, mid):
	z = 0
	for i in range(len(a) - mid, len(a)):
		z += a[i]
		if z > k:
			m -= 1
			z = a[i]
		if m == 0:
			return 0
	return 1

def main():
	(n, m, k) = map(int, input().split())
	a = list(map(int, input().split()))
	(lo, hi, ans) = (1, n, 0)
	while lo <= hi:
		mid = (lo + hi) // 2
		if check(a, m, k, mid):
			ans = mid
			lo = mid + 1
		else:
			hi = mid - 1
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def get_input_list():
	return list(map(int, input().split()))
(n, m, k) = get_input_list()
a = get_input_list()
b = 0
c = k
i = n - 1
r = 0
while True:
	if c < a[i]:
		b += 1
		if b == m:
			break
		c = k
	c -= a[i]
	r += 1
	i -= 1
	if i < 0:
		break
print(r)


input1 = input().split()
num_objects = int(input1[0])
num_boxes = int(input1[1])
box_size = int(input1[2])
objects = [int(x) for x in input().split()]
max_objects_packed = 0
current_box_size = box_size
remaining_boxes = num_boxes
objects_packed = 0
i = num_objects - 1
while i >= 0 and remaining_boxes != 0:
	if objects[i] < current_box_size:
		current_box_size -= objects[i]
		objects_packed += 1
		i -= 1
	elif objects[i] == current_box_size:
		current_box_size = box_size
		objects_packed += 1
		remaining_boxes -= 1
		i -= 1
	elif objects[i] > current_box_size:
		current_box_size = box_size - objects[i]
		remaining_boxes -= 1
		i -= 1
		if remaining_boxes != 0:
			objects_packed += 1
print(objects_packed)


def go():
	(n, m, k) = [int(i) for i in input().split(' ')]
	a = [int(i) for i in input().split(' ')]
	current_box = k
	m -= 1
	c = 0
	for i in range(n - 1, -1, -1):
		if a[i] <= current_box:
			current_box -= a[i]
			c += 1
		elif a[i] > current_box:
			current_box = k - a[i]
			if m == 0:
				break
			m -= 1
			c += 1
	return c
print(go())


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from heapq import *
from math import gcd, factorial, floor, ceil
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 998244353

def chck(a, m, k, mid):
	cnt = 1
	rem = k
	for i in range(mid, len(a)):
		if rem >= a[i]:
			rem -= a[i]
		else:
			cnt += 1
			rem = k - a[i]
	return cnt <= m

def solve(case):
	(n, m, k) = sep()
	a = lis()
	lo = 0
	hi = n
	ans = n
	while lo <= hi:
		mid = (lo + hi) // 2
		if chck(a, m, k, mid):
			ans = min(ans, mid)
			hi = mid - 1
		else:
			lo = mid + 1
	print(n - ans)
testcase(1)


str = input().strip().split(' ')
(n, m, k) = (int(str[0]), int(str[1]), int(str[2]))
a = [int(x) for x in input().strip().split(' ')]
a.reverse()
sum = 0
counti = 0
for x in a:
	if sum + x <= k:
		sum += x
	else:
		m -= 1
		if m > 0:
			sum = x
		else:
			break
	counti += 1
print(counti)


(n, k, m) = map(int, input().split())
l = [int(i) for i in input().split()]

def ispzbl(l, n, m, k, mid):
	z = l[mid:]
	if not z:
		return True
	s = 0
	box = 1
	for i in z:
		s += i
		if s > m:
			box += 1
			s = i
	return box <= k
low = 0
high = n - 1
while low <= high:
	mid = (low + high) // 2
	if ispzbl(l, n, m, k, mid):
		ans = mid
		high = mid - 1
	else:
		low = mid + 1
print(n - ans)


(n, m, k) = list(map(int, input().split()))
list_a = list(map(int, input().split()))
list_a.reverse()
count_b = 0
sum_now = 0
ans = 0
i = 0
while i < n:
	if count_b >= m:
		ans = i - 1
		break
	sum_now += list_a[i]
	if sum_now > k:
		sum_now = 0
		count_b += 1
	else:
		i += 1
else:
	ans = n - 1
print(ans + 1)


(w, m, k) = map(int, input().split())
a = list(map(int, input().split()))
i = w
s = k
while i > 0 and m > 0:
	i -= 1
	if s < a[i]:
		m -= 1
		s = k
	s -= a[i]
if m == 0:
	print(w - i - 1)
else:
	print(w - i)


import sys

def minp():
	return sys.stdin.readline().strip()
(n, mm, k) = list(map(int, minp().split()))
a = list(map(int, minp().split()))

def tt(x):
	m = mm - 1
	c = 0
	res = 0
	for i in reversed(a):
		if c + i > k:
			if m == 0 or i > k:
				return res
			c = i
			m -= 1
		else:
			c += i
		res += 1
	return res
print(tt(0))


(n, m, k) = [int(_) for _ in input().split()]
a = [int(_) for _ in input().split()]
b = k
count = 0
for obj in a[::-1]:
	if obj > k:
		break
	if obj > b:
		if m > 1:
			m -= 1
			b = k - obj
			count += 1
		else:
			break
	else:
		b -= obj
		count += 1
print(count)


ar = []
read = input().split(' ')
n = int(read[0])
m = int(read[1])
k = int(read[2])
read = input().split(' ')
for i in range(n):
	ar.append(int(read[i]))
cap = k
i = n - 1
while m > 0 and i >= 0:
	if cap - ar[i] >= 0:
		cap -= ar[i]
		i -= 1
	elif m > 0:
		m -= 1
		cap = k
	else:
		break
print(n - i - 1)


import atexit, io, sys, collections, math, heapq, fractions, copy, os, functools
import sys
import random
import collections
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(n, m, k) = map(int, input().split())
ais = list(map(int, input().split()))
(lo, hi) = (0, len(ais) - 1)

def check(i, ais, k, allowed):
	r = [0]
	for j in range(i, len(ais)):
		r[-1] += ais[j]
		if r[-1] > k:
			r[-1] -= ais[j]
			r.append(ais[j])
		if r[-1] > k:
			return False
	return len(r) <= allowed
while lo < hi:
	med = lo + hi >> 1
	if check(med, ais, k, m):
		hi = med
	else:
		lo = med + 1
print(len(ais) - lo)


(n, k, m) = map(int, input().split())
l = list(map(int, input().split()))
tot = 0
ans = 1
s = 0
for i in range(n - 1, -1, -1):
	s += l[i]
	if s > m:
		s = l[i]
		ans += 1
		if ans > k:
			break
	tot += 1
print(tot)


def fit(a, m, k, res):
	k1 = k
	for ai in a[-res:]:
		if ai <= k1:
			k1 -= ai
		else:
			k1 = k - ai
			if m == 1:
				return False
			else:
				m -= 1
	return True

def bisect(n, m, k, a):
	(left, right) = (0, n + 1)
	while left + 1 < right:
		middle = (left + right) // 2
		if fit(a, m, k, middle):
			left = middle
		else:
			right = middle
	return left

def main():
	(n, m, k) = list(map(int, input().split()))
	a = tuple(map(int, input().split()))
	print(bisect(n, m, k, a))
main()


(n, m, k) = map(int, input().split())
obj = list(map(int, input().split()))
obj = list(reversed(obj))
r = 0
for i in range(n):
	if r - obj[i] < 0:
		if m == 0:
			print(i)
			exit(0)
		else:
			m -= 1
			r = k - obj[i]
	else:
		r -= obj[i]
print(n)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
c = 0
curr = 0
r = 0
for i in range(n - 1, -1, -1):
	if curr + a[i] > k:
		curr = a[i]
		c += 1
	else:
		curr += a[i]
	if c == m:
		break
	r += 1
print(r)


(n, m, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
a.reverse()
last = 0
for i in range(n):
	if a[i] > last:
		if m == 0:
			print(i)
			return
		else:
			m -= 1
			last = k - a[i]
	else:
		last -= a[i]
print(n)


from bisect import bisect_right
from cmath import inf
from collections import defaultdict
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
graph = defaultdict(list)
(l, r) = (0, 0)
s = 0
for i in range(2 * n):
	if r < n:
		if s + a[r] <= k:
			s += a[r]
			r += 1
		else:
			graph[r].append(l)
			s -= a[l]
			l += 1
lvls = list(map(lambda x: inf, range(n)))
for i in range(n)[::-1]:
	if lvls[i] == inf:
		lvls[i] = 1
	for j in graph[i]:
		lvls[j] = min(lvls[j], lvls[i] + 1)
print(bisect_right(lvls[::-1], m))


(n, m, k) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a = a[::-1]
boxes = 0
ans = 0
loc = 0
for i in range(n):
	loc += a[i]
	if loc > k:
		boxes += 1
		loc = a[i]
	if boxes == m:
		break
	ans += 1
print(ans)


R = lambda : map(int, input().split())
(n, m, k) = R()
a = [*R()]
(i, s) = (n, k)
while i and m:
	i -= 1
	if s < a[i]:
		m -= 1
		s = k
	s -= a[i]
print(n - i - (m == 0))


(n, m, k) = map(int, input().split())
arr = list(map(int, input().split()))
arr1 = [0] * n
i = n - 1
prevval = 0
ans = 0
while i >= 0 and m > 0:
	if prevval + arr[i] <= k:
		prevval += arr[i]
	else:
		m -= 1
		prevval = arr[i]
	i -= 1
	ans += 1
if m == 0:
	ans -= 1
print(ans)


(n, m, k) = map(int, input().split())
u = list(map(int, input().split()))

def chek(M):
	p = k
	t = m
	for i in range(M, n):
		if p - u[i] >= 0:
			p -= u[i]
		elif t > 1:
			t -= 1
			p = k - u[i]
			if p < 0:
				return False
		else:
			return False
	return True
L = 0
R = n
while R - L > 1:
	M = (L + R) // 2
	if chek(M):
		R = M
	else:
		L = M
if chek(L):
	print(n - L)
else:
	print(n - R)


def check(a, m, mx):
	t = mx
	for i in a:
		if t - i >= 0:
			t -= i
		elif m - 1 > 0:
			m -= 1
			t = mx - i
		else:
			return False
	return True

def f(m, mx, a):
	hi = len(a)
	lo = 0
	res = 0
	while lo <= hi:
		mid = (lo + hi) // 2
		if check(a[mid:], m, mx):
			hi = mid - 1
			res = len(a[mid:])
		else:
			lo = mid + 1
	return res
(n, m, mx) = map(int, input().strip().split())
lst = list(map(int, input().strip().split()))
print(f(m, mx, lst))


(obj, n_caixas, size) = input().split()
(n_caixas, size) = (int(n_caixas), int(size))
objts = [int(x) for x in input().split()]
objts.reverse()
current_box_size = size
count_obj = 0
for o in objts:
	if n_caixas < 1:
		break
	if o <= current_box_size:
		current_box_size -= o
		count_obj += 1
		continue
	n_caixas -= 1
	if n_caixas < 1 or size < o:
		break
	current_box_size = size - o
	count_obj += 1
print(count_obj)


(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
ls = 0
a.reverse()
ans = 0
ind = 0
while ind < n:
	if m <= 0:
		break
	if a[ind] > k:
		ans = ind
		break
	if ls + a[ind] > k:
		ans = ind
		ls = 0
		m -= 1
	else:
		ls += a[ind]
		ind += 1
	if ind == n:
		ans = ind
print(ans)


(n, m, k) = map(int, input().split())
t = list(map(int, input().split()))
tmp_sum = 0
wyn = 0
z = 0
for x in range(len(t) - 1, -1, -1):
	if m == 0:
		break
	tmp_sum += t[x]
	if x != 0:
		wyn += 1
		if tmp_sum + t[x - 1] > k:
			m -= 1
			tmp_sum = 0
	elif tmp_sum <= k:
		wyn += 1
print(wyn)


(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
a.reverse()
curr = 0
boxesused = 0
index = 0
done = False
while boxesused < m:
	if index == n:
		done = True
		print(n)
		break
	j = a[index]
	if j + curr <= k:
		curr += j
		index += 1
	else:
		boxesused += 1
		curr = j
		index += 1
if not done:
	print(index - 1)


(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
count = 0
curr = 0
i = n
while count < m and i >= 0:
	i -= 1
	if curr + a[i] <= k:
		curr += a[i]
	else:
		count += 1
		curr = a[i]
print(n - i - 1)


import math
import sys
(n, m, k) = map(int, sys.stdin.readline().split())
a = [int(x) for x in sys.stdin.readline().split()]
a.reverse()
ans = 0
bag = m - 1
size = k
for x in a:
	if size < x:
		if bag > 0:
			bag -= 1
			size = k - x
		else:
			break
	else:
		size -= x
	ans += 1
sys.stdout.write(str(ans))


(n, m, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
summ = []
cur = 0
for item in a[::-1]:
	cur += item
	summ.append(cur)

def helper(x, summ, l, r):
	while l < r:
		mid = l + (r - l + 1) // 2
		if summ[mid] > x:
			r = mid - 1
		else:
			l = mid
	if summ[l] <= x:
		return l
	else:
		return l - 1
res = []
l = 0
z = k
while len(res) < m and l < n:
	x = helper(z, summ, l, n - 1)
	res.append(x)
	z = summ[x] + k
	l = x + 1
print(res[-1] + 1)


a = input().split()
(n, m, size) = (int(a[0]), int(a[1]), int(a[2]))
a = input().split()

def f():
	temp = 0
	count_box = 0
	count_things = 0
	for i in reversed(a):
		temp += int(i)
		if temp > size:
			temp = int(i)
			count_box += 1
			if count_box == m:
				return count_things
		elif temp == size:
			temp = 0
			count_box += 1
			if count_box == m:
				count_things += 1
				return count_things
		count_things += 1
	return count_things
print(f())


from sys import stdin, stdout
(en, m, k) = [int(x) for x in stdin.readline().split()]
a = [int(x) for x in stdin.readline().split()]
n = []
c = k
boxes = 1
for x in a:
	if c >= x:
		c -= x
	else:
		boxes += 1
		c = k - x
	n.append(boxes)
totalB = n[-1]
if totalB <= m:
	print(en)
else:
	low = n.index(totalB - m)
	high = n.index(totalB - m + 1)
	while low <= high:
		mid = (low + high) // 2
		n = []
		c = k
		boxes = 1
		for x in a[mid:]:
			if c >= x:
				c -= x
			else:
				boxes += 1
				c = k - x
			n.append(boxes)
		if n[-1] <= m:
			high = mid - 1
		else:
			low = mid + 1
	print(en - low)


import sys
import math
(n, m, k) = map(int, input().split())
jobs = [int(x) for x in input().strip().split()]
steve = jobs[:]
nade = steve[::-1]
(suma, con, kik) = (0, 0, 0)
for i in range(n):
	if nade[i] > k:
		break
	if suma + nade[i] < k:
		suma += nade[i]
		kik += 1
		if i < n - 1:
			if suma + nade[i + 1] > k:
				suma = 0
				con += 1
	elif suma + nade[i] == k:
		con += 1
		kik += 1
		suma = 0
	elif suma + nade[i] > k:
		con += 1
		suma = 0
	if con >= m:
		break
print(kik)


(n, m, k) = map(int, input().split())
ar = [int(i) for i in input().split()]
(l, r) = (0, n + 1)
while r - l > 1:
	c = (l + r) // 2
	cur = 0
	ans = 1
	for i in range(n - c, n):
		if cur + ar[i] > k:
			ans += 1
			cur = ar[i]
		else:
			cur += ar[i]
	if ans <= m:
		l = c
	else:
		r = c
print(l)


def check(mid):
	tmp = lis[mid:]
	if suf[mid] > m * k or ma[mid] > k:
		return False
	cap = k
	box = m
	for i in range(len(tmp)):
		if tmp[i] <= cap:
			cap -= tmp[i]
		else:
			if box == 1:
				return False
			box -= 1
			cap = k
			if tmp[i] > cap:
				return False
			else:
				cap -= tmp[i]
	return True
(n, m, k) = map(int, input().split())
lis = list(map(int, input().split()))
suf = [0] * (n + 3)
ma = [0] * (n + 3)
for i in range(n - 1, -1, -1):
	suf[i] += suf[i + 1] + lis[i]
	ma[i] = max(lis[i], ma[i + 1])
l = 0
r = n
while l <= r:
	mid = l + (r - l) // 2
	if check(mid):
		r = mid - 1
	else:
		l = mid + 1
print(n - l)


(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
n = n - 1
count = 0
while m > 0 and n >= 0:
	ktemp = k
	while ktemp >= a[n] and n >= 0:
		ktemp = ktemp - a[n]
		n = n - 1
		count = count + 1
	m = m - 1
print(count)


import sys
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))
(n, m, k) = inpl()
a = inpl()
a = a[::-1]
now = k
for (i, x) in enumerate(a):
	if x > k:
		break
	if now >= x:
		now -= x
	else:
		m -= 1
		if m == 0:
			break
		now = k - x
else:
	print(n)
	quit()
print(i)


(n, m, k) = map(int, input().split())
A = list(map(int, input().split()))
A.reverse()
inb = 0
box = 0
i = 0
while box < m and i < n:
	if inb + A[i] <= k:
		inb += A[i]
		i += 1
	else:
		box += 1
		inb = 0
print(i)


(n, m, k) = map(int, input().split())
a = [int(x) for x in input().split()]
i = n - 1
tec = k
res = 0
while i >= 0 and m > 0:
	if a[i] <= tec:
		tec -= a[i]
		res += 1
		i -= 1
	else:
		m -= 1
		tec = k
print(res)


(n, m, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
a = a[::-1]
res = 0
weight = 0
for i in range(n):
	weight += a[i]
	if weight > k:
		weight = a[i]
		m -= 1
	if m <= 0:
		break
	res += 1
print(res)


import re
import sys
exit = sys.exit
from bisect import bisect_left as bsl, bisect_right as bsr
from collections import Counter, defaultdict as ddict, deque
from functools import lru_cache
cache = lru_cache(None)
from heapq import *
from itertools import *
from math import inf
from pprint import pprint as pp
enum = enumerate
ri = lambda : int(rln())
ris = lambda : list(map(int, rfs()))
rln = sys.stdin.readline
rl = lambda : rln().rstrip('\n')
rfs = lambda : rln().split()
mod = 1000000007
d4 = [(0, -1), (1, 0), (0, 1), (-1, 0)]
d8 = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]

def possible(t, n, m, k):
	cur = k
	m -= 1
	for i in range(n - t, n):
		if a[i] > cur:
			if m == 0:
				return 0
			m -= 1
			cur = k
		cur -= a[i]
	return 1
(n, m, k) = ris()
a = ris()
ans = 0
(lo, hi) = (0, n)
while lo <= hi:
	mid = lo + hi >> 1
	if possible(mid, n, m, k):
		ans = mid
		lo = mid + 1
	else:
		hi = mid - 1
print(ans)


import sys
input = sys.stdin.readline

def main():
	(n, m, k) = map(int, input().split())
	a = list(map(int, input().split()))
	if min(a) > k:
		return 0
	a = a[::-1]
	current = 0
	answer = 0
	for i in range(n):
		if current + a[i] <= k:
			current += a[i]
			answer += 1
		else:
			current = 0
			m -= 1
			if m == 0:
				break
			elif current + a[i] < k:
				current += a[i]
				answer += 1
			elif current + a[i] == k:
				answer += 1
				m -= 1
				if m == 0:
					break
			else:
				continue
	return answer

def __starting_point():
	print(main())
__starting_point()


(n, b, k) = map(int, input().split())
a = [int(i) for i in input().split()]
(j, m, s, ans) = (0, 1, 0, 0)
for i in range(len(a) - 1, -1, -1):
	if m <= b and s + a[i] <= k:
		ans += 1
		s += a[i]
	elif m + 1 <= b and a[i] <= k:
		ans += 1
		s = a[i]
		m += 1
	else:
		break
print(ans)


(n, m, k) = map(int, input().split())
A = list(map(int, input().split()))

def is_ok(x):
	cnt = 1
	cur = 0
	for i in range(n - x, n):
		if cur + A[i] <= k:
			cur += A[i]
		else:
			cnt += 1
			cur = A[i]
	if cnt <= m:
		return True
	else:
		return False
ok = 0
ng = n + 1
while ok + 1 < ng:
	c = (ok + ng) // 2
	if is_ok(c):
		ok = c
	else:
		ng = c
print(ok)


kk = input().split()
n = int(kk[0])
m = int(kk[1])
k = int(kk[2])
objs = list(map(int, input().split()))
if m >= n:
	print(n)
else:
	objs = objs[::-1]
	sum_ = 0
	items = 0
	l = list()
	for obj in objs:
		if sum_ + obj <= k:
			sum_ += obj
			items += 1
		else:
			l.append(items)
			sum_ = obj
			items = 1
		if len(l) == m:
			break
	l.append(items)
	s = 0
	for i in range(m):
		if i < len(l):
			s += l[i]
		else:
			break
	print(s)


(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
a = list(reversed(a))
cnt = 0
K = k
ans = 0
for i in range(n):
	if a[i] <= K:
		K -= a[i]
	else:
		cnt += 1
		K = k - a[i]
	if cnt == m:
		break
	ans += 1
print(ans)


(n, m, k) = list(map(int, input().split()))
cnt = 0
count = 0
sum = 0
a = [int(s) for s in input().split()]
for i in range(n - 1, -1, -1):
	sum += a[i]
	if sum > k:
		sum = a[i]
		count += 1
		if count >= m:
			break
	cnt += 1
print(cnt)


(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
t = k
c = 0
for i in range(n - 1, -1, -1):
	if a[i] > t:
		c += 1
		if c == m or a[i] > k:
			print(n - i - 1)
			break
		t = k
	t -= a[i]
else:
	print(n)


import sys
input = sys.stdin.readline

def judge(x):
	cnt = 1
	remain = k
	for i in range(n - x, n):
		if remain - a[i] >= 0:
			remain -= a[i]
		else:
			if a[i] > k:
				return False
			cnt += 1
			remain = k - a[i]
	return cnt <= m

def binary_search():
	(l, r) = (0, n)
	while l <= r:
		mid = (l + r) // 2
		if judge(mid):
			l = mid + 1
		else:
			r = mid - 1
	return r
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
print(binary_search())


import sys
input = sys.stdin.readline
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
a = a[::-1]
cnt_box = 1
ca = 0
s = 0
for i in range(n):
	if s + a[i] <= k:
		s += a[i]
		ca += 1
	else:
		s = 0
		cnt_box += 1
		if cnt_box <= m:
			s += a[i]
			ca += 1
	if cnt_box > m:
		break
print(ca)


s = [int(i) for i in input().split()]
(n, m, k) = (s[0], s[1], s[2])
arr = [int(i) for i in input().split()]
present = 0
box = 1
index = -1
for o in range(n - 1, -1, -1):
	if present + arr[o] > k:
		present = arr[o]
		box += 1
	else:
		present += arr[o]
	if box > m:
		index = o
		break
print(n - 1 - index)


import sys

def minp():
	return sys.stdin.readline().strip()
(n, mm, k) = map(int, minp().split())
a = list(map(int, minp().split()))

def tt(x):
	m = mm - 1
	c = 0
	for j in range(x, n):
		i = a[j]
		if c + i > k:
			if m == 0 or i > k:
				return False
			c = i
			m -= 1
		else:
			c += i
	return True
l = 0
r = n
while l < r:
	c = (l + r) // 2
	if tt(c):
		r = c
	else:
		l = c + 1
print(n - r)


(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))

def cal(x):
	now = 0
	cnt = 0
	global n, a, k, m
	for i in range(x, n):
		if now + a[i] <= k:
			now += a[i]
		else:
			now = a[i]
			cnt += 1
		if cnt > m:
			return False
	return cnt < m
(l, r) = (0, n - 1)
while l < r:
	mid = (l + r) // 2
	if cal(mid):
		r = mid
	else:
		l = mid + 1
print(n - r)


(N, M, K) = map(int, input().split())
A = [int(x) for x in input().split()]

def check(val):
	if val == 0:
		return True
	totalBox = 1
	currentBox = K
	for i in range(len(A) - val, len(A)):
		if currentBox >= A[i]:
			currentBox -= A[i]
		else:
			totalBox += 1
			currentBox = K - A[i]
		if totalBox > M:
			return False
	return totalBox <= M
(l, h) = (0, N + 1)
while l < h:
	m = (l + h) // 2
	if check(m):
		l = m + 1
	else:
		h = m
print(l - 1)


(n, m, k) = map(int, input().split())
nums = list(map(int, input().split()))
act = 0
cjs = 1
i = n - 1
while i >= 0 and cjs <= m:
	if act + nums[i] > k:
		if cjs < m:
			cjs += 1
			act = nums[i]
			i -= 1
		else:
			break
	else:
		act += nums[i]
		i -= 1
print(n - i - 1)


def bS(alist, item):
	first = 0
	last = len(alist) - 1
	found = False
	posi = -1
	while first <= last + 1 and (not found):
		midpoint = (first + last) // 2
		if alist[midpoint] >= item:
			found = True
			posi = midpoint
		elif item < alist[midpoint]:
			last = midpoint - 1
		else:
			first = midpoint + 1
	return posi
(n, m, k) = list(map(int, input().split()))
a = list(input().split())
maxi = 0
cc = 0
now = -1
alr = 0
alfa = [0] * n
last = 0
for ii in range(n):
	i = n - ii - 1
	alfa[ii] = int(a[i]) + last
	last = alfa[ii]
if alfa[-1] > m * k:
	pos = bS(alfa, m * k)
else:
	pos = n - 1
ii = n - pos - 1
b = a
b.reverse()
maxi = 0
start = 0
if 1 == 1:
	alr = 0
	cc = 0
	for i in b[start:]:
		alr += int(i)
		cc += 1
		if alr > k:
			alr = int(i)
			if alr > k:
				cc -= 1
			m -= 1
			if m == 0:
				cc -= 1
				break
	if cc > maxi:
		maxi = cc
print(maxi)


def main():
	(n, m, k) = [int(c) for c in input().split()]
	a = [int(c) for c in input().split()]
	box = 0
	b_id = 0
	count = 0
	for i in range(len(a) - 1, -1, -1):
		if box + a[i] <= k:
			box += a[i]
			count += 1
		else:
			if b_id + 1 >= m:
				break
			b_id += 1
			box = a[i]
			count += 1
	print(count)
main()


(n, m, k) = map(int, input().split())
a = [int(i) for i in input().split()]

def cond(x):
	box_cnt = m
	capacity = k
	for i in range(n - x - 1, n):
		if capacity >= a[i]:
			capacity -= a[i]
		else:
			box_cnt -= 1
			capacity = k - a[i]
	return box_cnt <= 0
(lo, hi) = (0, n)
while lo < hi:
	mi = (lo + hi) // 2
	if cond(mi):
		hi = mi
	else:
		lo = mi + 1
print(lo)


(n, m, k) = [int(x) for x in input().split()]
ns = [int(x) for x in input().split()]
ns.reverse()
rem = k
num = 0
ans = []
for i in range(n):
	if rem >= ns[i]:
		rem -= ns[i]
		num += 1
	else:
		ans.append(num)
		rem = k - ns[i]
		num = 1
if num > 0:
	ans.append(num)
if len(ans) <= m:
	print(sum(ans))
else:
	a = [sum(ans[:m])]
	print(max(a))


def check(num):
	left = m
	curr = k
	for i in range(n - num, n):
		if curr >= a[i]:
			curr -= a[i]
		else:
			if left == 1:
				return False
			left -= 1
			curr = k - a[i]
	return True
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
low = 0
high = n
while low < high:
	mid = (low + high) // 2
	if check(mid):
		low = mid + 1
	else:
		high = mid - 1
if check(low):
	print(low)
else:
	print(low - 1)


(n, m, k) = map(int, input().split())
a = [int(i) for i in input().split()]
cur = 0
ans = 0
box = k
i = n - 1
while i >= 0:
	obj = a[i]
	if obj <= box:
		box -= obj
		ans += 1
	else:
		cur += 1
		box = k
		if cur >= m:
			break
		continue
	i -= 1
print(ans)


(n, m, k) = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
ans = 0
boxi = 0
rem_size = k
for i in range(n - 1, -1, -1):
	if a[i] > k:
		break
	if a[i] > rem_size:
		if boxi >= m - 1:
			break
		else:
			boxi += 1
			rem_size = k
	rem_size -= a[i]
	ans += 1
print(ans)


(n, k, m) = map(int, input().split())
l = list(map(int, input().split()))

def check(i):
	tot = 0
	ans = 1
	s = 0
	for i in range(n - i, n):
		s += l[i]
		if s > m:
			s = l[i]
			ans += 1
			if ans > k:
				return False
	return True
s = 0
e = n
while s < e:
	mid = (s + e + 1) // 2
	if check(mid):
		s = mid
	else:
		e = mid - 1
print(s)


from collections import defaultdict
from string import ascii_lowercase
from sys import stdin, stdout
input = stdin.readline

def main():
	(n, m, k) = map(int, input().split())
	a = list(map(int, input().split()))

	def check(start):
		boxes = 1
		box = 0
		for i in range(start, n):
			if box + a[i] <= k:
				box += a[i]
			else:
				boxes += 1
				box = a[i]
		return boxes <= m
	L = 0
	R = n - 1
	while L + 1 < R:
		mid = (L + R) // 2
		if check(mid):
			R = mid
		else:
			L = mid
	ans = R
	if check(L):
		ans = L
	print(n - ans)
main()


import sys, os, io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import math
alphabets = list('abcdefghijklmnopqrstuvwxyz')

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	c = dict(Counter(l))
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def sieveForSmallestPrimeFactor():
	MAXN = 100001
	spf = [0 for i in range(MAXN)]
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, math.ceil(math.sqrt(MAXN))):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
	return spf

def getPrimeFactorizationLOGN(x):
	spf = sieveForSmallestPrimeFactor()
	ret = list()
	while x != 1:
		ret.append(spf[x])
		x = x // spf[x]
	return ret

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def si():
	return input()

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def check(a, mid, m, k):
	cur = k
	for i in range(mid, len(a)):
		if cur >= a[i]:
			cur -= a[i]
		else:
			if m <= 1:
				return False
			m -= 1
			cur = k - a[i]
	return True

def solve(_):
	(n, m, k) = li()
	a = li()
	l = 0
	r = n - 1
	ans = -1
	while l <= r:
		mid = (l + r) // 2
		if check(a, mid, m, k):
			r = mid - 1
			ans = mid
		else:
			l = mid + 1
	print(n - ans)
t = 1
for _ in range(t):
	solve(_)


(n, m, k) = map(int, input().split())
arr = list(reversed(list(map(int, input().split()))))
has = cnt = 0
for v in arr:
	if v + has <= k:
		has += v
	elif v > k:
		print(0)
		exit(0)
	else:
		has = v
		m -= 1
	if m == 0:
		break
	else:
		cnt += 1
print(cnt)


(n, m, k) = map(int, input().split())
a = input().split()
a = [int(x) for x in a]
a.reverse()
box = 0
fruits = 0
m -= 1
for (i, x) in enumerate(a):
	if box + x <= k:
		box += x
		continue
	m -= 1
	if m < 0:
		fruits = i
		break
	box = x
else:
	print(n)
	exit()
print(i)


import collections

def valid(n, m, k, arr, x):
	used = 0
	while used < m and x < n:
		capacity = k
		while x < n and capacity >= arr[x]:
			capacity -= arr[x]
			x += 1
		used += 1
	return x == n

def solve(n, m, k, arr):
	(l, r) = (0, n)
	ans = 0
	while l <= r:
		mid = (l + r) // 2
		if valid(n, m, k, arr, mid):
			ans = max(ans, n - mid)
			r = mid - 1
		else:
			l = mid + 1
	return ans
[n, m, k] = list(map(int, input().strip().split()))
arr = list(map(int, input().strip().split()))
print(solve(n, m, k, arr))


(n, m, k) = list(map(int, input().split()))
arr = list(map(int, input().split()))
(low, high) = (0, n - 1)
while low < high:
	mid = (low + high) // 2
	ans = [k for i in range(m)]
	flag = 0
	ind = 0
	for j in range(mid, n):
		cur = arr[j]
		prev = ind
		while True:
			if ans[ind] < cur:
				ind += 1
			else:
				ans[ind] -= cur
				break
			if ind >= m:
				flag = 1
				break
		if flag == 1:
			break
	if flag == 1:
		low = mid
		flag = 0
	else:
		high = mid
		flag = 0
	if high - low == 1:
		break
ans = [k for i in range(m)]
flag = 0
ind = 0
for j in range(low, n):
	cur = arr[j]
	prev = ind
	while True:
		if ans[ind] < cur:
			ind += 1
		else:
			ans[ind] -= cur
			break
		if ind >= m:
			flag = 1
			break
	if flag == 1:
		break
if flag == 1:
	print(n - high)
else:
	print(n - low)


(n, M, K) = map(int, input().split())
l = list(map(int, input().split()))
num = 0
k = K
m = M
for j in range(n - 1, -1, -1):
	if m <= 0:
		break
	elif k >= l[j]:
		k -= l[j]
		num += 1
	else:
		m -= 1
		if m <= 0:
			break
		else:
			k = K
			k -= l[j]
			num += 1
print(num)