https://codeforces.com/contest/1066/problem/D
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Maksim has n objects and mm boxes, each box has size exactly k. Objects are numbered from 1 to nn in order from left to right, the size of the i-th object is ai.
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 ai), he puts it in the box, and the remaining size of the box decreases by ai. 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≤n,m≤2⋅105, 1≤k≤1091≤k≤109) — the number of objects, the number of boxes and the size of each box.
The second line of the input contains n integers a1,a2,…,an (1≤ai≤k), where ai 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
Copy
5 2 6 5 2 1 4 2
output
Copy
4
input
Copy
5 1 4 4 2 3 4 1
output
Copy
1
input
Copy
5 3 3 1 2 3 1 1
output
Copy
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].
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
using namespace std;
typedef long long ll;
const int N=2e5+10;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k;
int a[N];
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//while(scanf("%d%d%d",&n,&m,&k)!=EOF){
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int cnt=0;
int sum=0;
int ans=0;
int i=0;
for(i=n;i>=1;i--){
sum+=a[i];
if(sum>k){
sum=a[i];
cnt++;
if(cnt==m){
ans=n-i;
break;
}
}
}
if(i<=0){
ans=n;
}
cout << ans << endl;
//}
//cout << "Hello world!" << endl;
return 0;
}