http://codeforces.com/contest/1066/problem/D

Maksim has nn objects and mm boxes, each box has size exactly kk. Objects are numbered from 11 to nn in order from left to right, the size of the ii-th object is aiai.

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 ii-th object fits in the current box (the remaining size of the box is greater than or equal to aiai), he puts it in the box, and the remaining size of the box decreases by aiai. 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 nn, mm, kk (1≤n,m≤2⋅1051≤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 nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤k1≤ai≤k), where aiai is the size of the ii-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 44 objects. Firstly, he tries to pack all the 55 objects. Distribution of objects will be [5],[2,1][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][2,1],[4,2]. So the answer is 44.

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][4]), but he can pack the last object ([1][1]).

In the third example Maksim can pack all the objects he has. The distribution will be [1,2],[3],[1,1][1,2],[3],[1,1].

代码:

#include <bits/stdc++.h>
using namespace std; const int maxn = 2e5 + 10;
int N, M, K;
int a[maxn]; int main() {
scanf("%d%d%d", &N, &M, &K);
for(int i = 1; i <= N; i ++)
scanf("%d", &a[i]);
int cnt = 0;
int ans = K;
for(int i = N; i >= 1; i --) {
if(a[i] <= ans) {
ans -= a[i];
cnt ++;
} else {
M --;
if(!M) break;
ans = K - a[i];
cnt ++;
}
}
printf("%d\n", cnt);
return 0;
}

  

最新文章

  1. pair correlation ggpair ggmatrix
  2. POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]
  3. linux后台运行和关闭、查看后台任务(转)
  4. Linux(Debian)+Apache+Django 配置
  5. OpenJudge计算概论-求出e的值
  6. 用C语言写个程序推算出是星期几?(用泰勒公式实现)
  7. Unity定时器
  8. swift 关于闭包和函数
  9. js加载优化三
  10. ORACLE中date类型字段的处理
  11. powerdesign
  12. MySQL--CREATE INDEX在各版本的优化
  13. MR运动静止用户区分
  14. 通用程序返回结果类 ApplicationResult.cs
  15. 怎么从bam文件中提取出比对OR没比对上的paired reads | bamToFastq | STAR
  16. python scrapy 把cookie并转化为字典的形式
  17. centos7 部署LNMP
  18. 标签传播算法(Label Propagation)及Python实现
  19. 查询测试程序中的selectOne和selectList函数
  20. 谈谈 Java 线程状态相关的几个方法

热门文章

  1. MacBookPro 存储空间优化
  2. linux连接FreeBSD虚拟机的mysql
  3. python__基础 : 类的__new__方法与实现一个单例
  4. php 利用composer引用第三方类库构建项目
  5. PHP 二维数组按某一个键值排序
  6. Numpy 索引及切片
  7. GPIO基础知识
  8. FCS校验 C语言简单实现
  9. mongo创建数据库和用户
  10. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column &#39;s.areaname&#39; in &#39;field list&#39;错误