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;
}

  

CodeForces Round #515 Div.3 D. Boxes Packing的更多相关文章

  1. Codeforces Round #515 (Div. 3)

    Codeforces Round #515 (Div. 3) #include<bits/stdc++.h> #include<iostream> #include<cs ...

  2. Codeforces Round #515 (Div. 3) 解题报告(A~E)

    题目链接:http://codeforces.com/contest/1066 1066 A. Vova and Train 题意:Vova想坐火车从1点到L点,在路上v的整数倍的点上分布着灯笼,而在 ...

  3. Codeforces Round #515 (Div. 3) B. Heaters【 贪心 区间合并细节 】

    任意门:http://codeforces.com/contest/1066/problem/B B. Heaters time limit per test 1 second memory limi ...

  4. Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum

    E. Binary Numbers AND Sum 题目链接:https://codeforces.com/contest/1066/problem/E 题意: 给出两个用二进制表示的数,然后将第二个 ...

  5. CodeForces Round #515 Div.3 C. Books Queries

    http://codeforces.com/contest/1066/problem/C You have got a shelf and want to put some books on it. ...

  6. CodeForces Round #515 Div.3 A. Vova and Train

    http://codeforces.com/contest/1066/problem/A Vova plans to go to the conference by train. Initially, ...

  7. CodeForces Round #515 Div.3 B. Heaters

    http://codeforces.com/contest/1066/problem/B Vova's house is an array consisting of nn elements (yea ...

  8. CodeForces Round #515 DIv.3 F. Yet another 2D Walking

    http://codeforces.com/contest/1066/problem/F Maksim walks on a Cartesian plane. Initially, he stands ...

  9. B. Heaters ( Codeforces Round #515 (Div. 3) )

    题解:对于每个点 i 来说,从 j = i + r - 1 开始往前找,如果找到一个 a [ j ] 是 1 ,那么就把它选上,但是我们需要判断交界处,也就是如果前面选的那个可以让这个点变温暖,就不用 ...

随机推荐

  1. Java分享笔记:File类中常用方法的介绍

    java.io包下的File类用于描述和创建一个文件或文件夹对象,只能对文件或文件夹做一些简单操作,不能修改文件的内容,功能比较有限.下面是对于File类中常用方法的程序演示. [1] 演示程序一 p ...

  2. MySQL工作经验

    以下是根据工作中遇到各种场景用到的一些Mysql用法,比较实用,基本是语法之外的一些东西. 修改账户密码 1.打开Mysql控制台,输入原密码: 2.输入以下语法:mysql> set pass ...

  3. Docker学习之镜像操作

    使用Docker镜像 以下都是Docker镜像的一系列重要名操作,包括获取.查看.搜索.删除.创建.存出或载入.上传等.可使用docker image help命令查看帮助. 1.获取镜像(pull) ...

  4. pwn的一些环境搭建

    <1>pwntools库安装 pwntools是一个CTF框架和漏洞利用开发库,用Python开发,由rapid设计,旨在让使用者简单快速的编写exploit. 本文将基于KUbuntu ...

  5. linux特殊权限位suid

    特殊权限位基本说明(了解): linux系统基本权限位为9位权限,但还有额外3位权限位,共12位权限: suid       s(x)     S     4     用户对应的权限位(用户对应的3位 ...

  6. 神经网络系列学习笔记(一)——神经网络之ANN学习资料汇总

    ANN tutorial: http://adventuresinmachinelearning.com/neural-networks-tutorial/ https://www.cs.toront ...

  7. Python代码结构——顺序、分支、循环

    ## 顺序结构 - 按照从上到下的顺序,一条语句一条语句的执行,是最基本的结构 ## 分支结构 if condition: statement statement ... elif condition ...

  8. 我的Hibernate学习

    以下博客均为引用, 侵删 Hibernate初级     HQL和SQL的区别 https://blog.csdn.net/aaa1117a8w5s6d/article/details/7757097 ...

  9. 栈--数据结构与算法Javascript描述(4)

    栈 Stack 概念 栈是一种高效的数据结构,数据只能在栈顶添加或者删除,所以这样的操作很快,而且容易实现.栈的使用遍布程序语言的方方面面,从表达式求值到处理函数调用. 栈是一种特殊的列表,栈内的元素 ...

  10. P2344 奶牛抗议

    P2344 奶牛抗议 题目背景 Generic Cow Protests, 2011 Feb 题目描述 约翰家的N 头奶牛正在排队游行抗议.一些奶牛情绪激动,约翰测算下来,排在第i 位的奶牛的理智度为 ...