Books time limit per test 2 seconds memory limit per test megabytes input standard input output standard output
When Valera had got some free time, he goes to the library to read some books. Today he ' s got t free minutes to read. That's why Valera took N books in the library and for each book he estimated the time he was going to need to read it. Let's number the books by integers from 1 to N. Valera needs AI minutes to read the i-th book.
Valera decided to choose a arbitrary book with number I and read the books one by one, starting from this book. In the other words, he would first read book number I, then book number i + 1, then book number i + 2 and so on. He continues the process until he either runs out of the "free time" or finishes reading the n-th book. Valera reads, the end, that's, he doesn ' t start reading the book if he doesn ' t has enough free time to fi Nish reading it.
Print the maximum number of books Valera can read. Input
The first line contains-integers n and t (1≤n≤105; 1≤t≤109)-the number of books and the number of free minut Es Valera ' s got. The second line contains a sequence of n integers a1, a2, ..., an (1≤ai≤104), where number AI shows the number of Minu TES, the boy, needs to read the i-th book. Output
Print a single integer-the maximum number of books Valera can read. Sample Test (s) input
4 5
3 1 2 1
Output
3
Input
3 3
2 2 3
Output
1
just sweep it back and forth with two pointers.
#include
#include
using namespace std;
Long Long a[110000];
int main()
{
int n;
Long Long T;
scanf ("%d%i64d", &n, &t);
for (int i = 1; I <= n; i++)
scanf ("%i64d", &a[i]);
Long Long now = 0;
int nown = 0;
int max = 0;
int r = N;
for (int l = n; l > 0; l--)
{
Now + = A[l];
nown++;
if (now <= t)
{
if (Nown > Max) max = Nown;
}
Else
while (now > t && r>0)// Note that while statements are to judge the boundary, although they do not think there is a problem, but what wonderful data have ...
{
Now-= A[r];
nown--;
r--;
}
}
printf ("%d\n", Max);
return 0;
}